-
Notifications
You must be signed in to change notification settings - Fork 1
/
setnshost.c
61 lines (45 loc) · 1.06 KB
/
setnshost.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Tool setting the hostname in an uts_namespace
Author: R. Koucha
Date: 31-Jan-2020
*/
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include <fcntl.h>
#include <unistd.h>
#include "util.h"
int main(int ac, char *av[])
{
int fd, rc;
char tpath[256];
if (ac != 3) {
fprintf(stderr, "Usage: %s pid hostname\n", basename(av[0]));
return 1;
}
if (!is_pid(av[1])) {
fprintf(stderr, "First parameter '%s' must be a pid\n", av[1]);
return 1;
}
// Build the pathname of the uts_ns
snprintf(tpath, sizeof(tpath), "/proc/%s/ns/uts", av[1]);
// Open the target uts_ns symbolic link
fd = open(tpath, O_RDONLY);
// Enter into the target namespace
rc = setns(fd, CLONE_NEWUTS);
if (rc != 0) {
ERR("Unable to enter into network_ns\n");
return 1;
}
close(fd);
rc = sethostname(av[2], strlen(av[2]));
if (rc != 0) {
ERR("sethostname(%s): %m (%d)\n", av[2], errno);
return 1;
}
return 0;
} // main