58 lines
914 B
Markdown
58 lines
914 B
Markdown
#### Exemple C
|
||
|
||
Voici un exemple de code C utilisant `setns(2)` :
|
||
|
||
<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>
|
||
|
||
|
||
#### Exemple shell
|
||
|
||
Dans un shell, on utilisera la commande `nsenter(1)` :
|
||
|
||
<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.
|
||
|
||
42sh# hostname
|
||
chaton
|
||
42sh# nsenter --uts=/proc/42/ns/uts /bin/bash
|
||
inutsns# hostname
|
||
jument
|
||
```
|
||
</div>
|