-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchname.c
38 lines (31 loc) · 823 Bytes
/
chname.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <string.h>
#include <errno.h>
#ifndef CLONE_NEWUTS
#define CLONE_NEWUTS 0x04000000
#endif
void usage() {
fprintf(stderr, "Usage: chname <hostname> <program> [args]\n");
}
int main(int argc, char** argv) {
if (argc < 3) {
usage();
exit(1);
}
// Use 'syscall' instead of unshare incase glibc doesn't have it.
// unshare(CLONE_NEWUTS);
if (syscall(__NR_unshare, CLONE_NEWUTS)) {
fprintf(stderr, "chname: New utsname namespace failed: %s\n", strerror(errno));
exit(1);
}
if (sethostname(argv[1], strlen(argv[1])+1)) {
fprintf(stderr, "chname: Set hostname failed: %s\n", strerror(errno));
exit(1);
}
execvp(argv[2], &argv[2]);
fprintf(stderr, "chname: exec failed: %s\n", strerror(errno));
return 1;
}