virli/tutorial/4/setns-samples.md

58 lines
914 B
Markdown
Raw Normal View History

2022-04-09 00:50:14 +00:00
#### Exemple C
2021-10-31 19:51:17 +00:00
2022-02-24 19:43:43 +00:00
Voici un exemple de code C utilisant `setns(2)` :
2021-10-31 19:51:17 +00:00
<div lang="en-US">
```c
#define _GNU_SOURCE
#include <fcntl.h>
#include <sched.h>
#include <stdlib.h>
// ./a.out /proc/PID/ns/FILE cmd args...
int
main(int argc, char *argv[])
{
int fd = open(argv[1], O_RDONLY);
if (fd == -1)
{
perror("open");
return EXIT_FAILURE;
}
if (setns(fd, 0) == -1)
{
perror("setns");
return EXIT_FAILURE;
}
execvp(argv[2], &argv[2]);
perror("execve");
return EXIT_FAILURE;
}
```
</div>
2022-04-09 00:50:14 +00:00
#### Exemple shell
2021-10-31 19:51:17 +00:00
2022-02-24 19:43:43 +00:00
Dans un shell, on utilisera la commande `nsenter(1)` :
2021-10-31 19:51:17 +00:00
<div lang="en-US">
```bash
42sh# unshare --uts /bin/bash
inutsns# echo $$
42
inutsns# hostname jument
# Keep this shell active to perform nexts steps, in another shell.
2021-10-31 19:51:17 +00:00
42sh# hostname
chaton
42sh# nsenter --uts=/proc/42/ns/uts /bin/bash
inutsns# hostname
jument
```
</div>