Branch data Line data Source code
1 : : #include <unistd.h>
2 : : #include <fcntl.h>
3 : : #include <sys/utsname.h>
4 : : #include <string.h>
5 : :
6 : : #include "util.h"
7 : : #include "syscall.h"
8 : : #include "namespaces.h"
9 : : #include "sysctl.h"
10 : : #include "uts_ns.h"
11 : :
12 : : #include "protobuf.h"
13 : : #include "protobuf/utsns.pb-c.h"
14 : :
15 : 212 : int dump_uts_ns(int ns_pid, int ns_id)
16 : : {
17 : : int ret, img_fd;
18 : : struct utsname ubuf;
19 : 212 : UtsnsEntry ue = UTSNS_ENTRY__INIT;
20 : :
21 : 212 : img_fd = open_image(CR_FD_UTSNS, O_DUMP, ns_id);
22 [ + - ]: 212 : if (img_fd < 0)
23 : : return -1;
24 : :
25 : 212 : ret = switch_ns(ns_pid, &uts_ns_desc, NULL);
26 [ + - ]: 212 : if (ret < 0)
27 : : goto err;
28 : :
29 : 212 : ret = uname(&ubuf);
30 [ - + ]: 212 : if (ret < 0) {
31 : 0 : pr_perror("Error calling uname");
32 : 0 : goto err;
33 : : }
34 : :
35 : 212 : ue.nodename = ubuf.nodename;
36 : 212 : ue.domainname = ubuf.domainname;
37 : :
38 : 212 : ret = pb_write_one(img_fd, &ue, PB_UTSNS);
39 : : err:
40 : 212 : close(img_fd);
41 [ + - ]: 212 : return ret < 0 ? -1 : 0;
42 : : }
43 : :
44 : 4 : int prepare_utsns(int pid)
45 : : {
46 : : int fd, ret;
47 : : UtsnsEntry *ue;
48 : 4 : struct sysctl_req req[3] = {
49 : : { "kernel/hostname" },
50 : : { "kernel/domainname" },
51 : : { },
52 : : };
53 : :
54 : 4 : fd = open_image(CR_FD_UTSNS, O_RSTR, pid);
55 [ + - ]: 4 : if (fd < 0)
56 : : return -1;
57 : :
58 : 4 : ret = pb_read_one(fd, &ue, PB_UTSNS);
59 [ + - ]: 4 : if (ret < 0)
60 : : goto out;
61 : :
62 : 4 : req[0].arg = ue->nodename;
63 : 4 : req[0].type = CTL_STR(strlen(ue->nodename));
64 : 4 : req[1].arg = ue->domainname;
65 : 4 : req[1].type = CTL_STR(strlen(ue->domainname));
66 : :
67 : 4 : ret = sysctl_op(req, CTL_WRITE);
68 : 4 : utsns_entry__free_unpacked(ue, NULL);
69 : : out:
70 : 4 : close(fd);
71 : 4 : return ret;
72 : : }
73 : :
74 : : struct ns_desc uts_ns_desc = NS_DESC_ENTRY(CLONE_NEWUTS, "uts");
|