Tuto 4 ready
This commit is contained in:
parent
fb994050db
commit
c960136430
18 changed files with 536 additions and 340 deletions
57
tutorial/4/setns-samples.md
Normal file
57
tutorial/4/setns-samples.md
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#### 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 sheel active to perform nexts steps, in another shell.
|
||||
|
||||
42sh# hostname
|
||||
chaton
|
||||
42sh# nsenter --uts=/proc/42/ns/uts /bin/bash
|
||||
inutsns# hostname
|
||||
jument
|
||||
```
|
||||
</div>
|
||||
Loading…
Add table
Add a link
Reference in a new issue