virli/tutorial/4/setns-samples.md
Pierre-Olivier Mercier 44723afa52 Fix typos
Thanks-to: <mathieu-ghirlanda@epita.fr>
2022-06-26 21:08:01 +02:00

58 lines
914 B
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#### 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>