2012-01-31 11:29:23 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/utsname.h>
|
2012-05-29 20:11:00 +04:00
|
|
|
#include <string.h>
|
2012-01-31 23:25:36 +04:00
|
|
|
|
2012-01-31 11:29:23 +03:00
|
|
|
#include "util.h"
|
|
|
|
#include "syscall.h"
|
|
|
|
#include "namespaces.h"
|
2012-02-02 19:55:48 +04:00
|
|
|
#include "sysctl.h"
|
2013-01-15 23:24:01 +04:00
|
|
|
#include "uts_ns.h"
|
2012-01-31 11:29:23 +03:00
|
|
|
|
2012-07-19 14:52:30 +04:00
|
|
|
#include "protobuf.h"
|
|
|
|
#include "protobuf/utsns.pb-c.h"
|
2012-01-31 11:29:23 +03:00
|
|
|
|
2013-09-30 17:16:47 +04:00
|
|
|
int dump_uts_ns(int ns_pid, int ns_id)
|
2012-01-31 11:29:23 +03:00
|
|
|
{
|
2013-09-30 17:16:47 +04:00
|
|
|
int ret, img_fd;
|
2012-01-31 11:29:23 +03:00
|
|
|
struct utsname ubuf;
|
2012-07-19 14:52:30 +04:00
|
|
|
UtsnsEntry ue = UTSNS_ENTRY__INIT;
|
2012-01-31 11:29:23 +03:00
|
|
|
|
2013-09-30 17:16:47 +04:00
|
|
|
img_fd = open_image(CR_FD_UTSNS, O_DUMP, ns_id);
|
|
|
|
if (img_fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-01-15 23:24:01 +04:00
|
|
|
ret = switch_ns(ns_pid, &uts_ns_desc, NULL);
|
2012-01-31 11:29:23 +03:00
|
|
|
if (ret < 0)
|
2013-09-30 17:16:47 +04:00
|
|
|
goto err;
|
2012-01-31 11:29:23 +03:00
|
|
|
|
|
|
|
ret = uname(&ubuf);
|
|
|
|
if (ret < 0) {
|
2012-01-31 15:13:05 +04:00
|
|
|
pr_perror("Error calling uname");
|
2013-09-30 17:16:47 +04:00
|
|
|
goto err;
|
2012-01-31 11:29:23 +03:00
|
|
|
}
|
2012-08-11 21:34:35 +04:00
|
|
|
|
2012-07-19 14:52:30 +04:00
|
|
|
ue.nodename = ubuf.nodename;
|
|
|
|
ue.domainname = ubuf.domainname;
|
2012-01-31 11:29:23 +03:00
|
|
|
|
2013-09-30 17:16:47 +04:00
|
|
|
ret = pb_write_one(img_fd, &ue, PB_UTSNS);
|
|
|
|
err:
|
|
|
|
close(img_fd);
|
|
|
|
return ret < 0 ? -1 : 0;
|
2012-01-31 11:29:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int prepare_utsns(int pid)
|
|
|
|
{
|
|
|
|
int fd, ret;
|
2012-07-19 14:52:30 +04:00
|
|
|
UtsnsEntry *ue;
|
|
|
|
struct sysctl_req req[3] = {
|
|
|
|
{ "kernel/hostname" },
|
|
|
|
{ "kernel/domainname" },
|
2012-02-02 19:55:48 +04:00
|
|
|
{ },
|
|
|
|
};
|
2012-01-31 11:29:23 +03:00
|
|
|
|
2013-04-09 11:13:51 +04:00
|
|
|
fd = open_image(CR_FD_UTSNS, O_RSTR, pid);
|
2012-01-31 11:29:23 +03:00
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
2012-08-07 02:42:58 +04:00
|
|
|
ret = pb_read_one(fd, &ue, PB_UTSNS);
|
2012-02-02 19:55:48 +04:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
2012-07-19 14:52:30 +04:00
|
|
|
req[0].arg = ue->nodename;
|
|
|
|
req[0].type = CTL_STR(strlen(ue->nodename));
|
|
|
|
req[1].arg = ue->domainname;
|
|
|
|
req[1].type = CTL_STR(strlen(ue->domainname));
|
2012-01-31 11:29:23 +03:00
|
|
|
|
2012-02-02 19:55:48 +04:00
|
|
|
ret = sysctl_op(req, CTL_WRITE);
|
2012-07-19 14:52:30 +04:00
|
|
|
utsns_entry__free_unpacked(ue, NULL);
|
2012-02-02 19:55:48 +04:00
|
|
|
out:
|
2012-01-31 11:29:23 +03:00
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-20 13:30:17 +04:00
|
|
|
struct ns_desc uts_ns_desc = NS_DESC_ENTRY(CLONE_NEWUTS, "uts");
|