2012-01-31 11:29:23 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/utsname.h>
|
2012-01-31 23:25:36 +04:00
|
|
|
|
2012-01-31 11:29:23 +03:00
|
|
|
#include "util.h"
|
|
|
|
#include "crtools.h"
|
|
|
|
#include "syscall.h"
|
|
|
|
#include "namespaces.h"
|
2012-02-02 19:55:48 +04:00
|
|
|
#include "sysctl.h"
|
2012-01-31 11:29:23 +03:00
|
|
|
|
|
|
|
static int dump_uts_string(int fd, char *str)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u32 len;
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
ret = write_img(fd, &len);
|
2012-01-31 23:29:24 +04:00
|
|
|
if (!ret)
|
2012-01-31 11:29:23 +03:00
|
|
|
ret = write_img_buf(fd, str, len);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dump_uts_ns(int ns_pid, struct cr_fdset *fdset)
|
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
struct utsname ubuf;
|
|
|
|
|
|
|
|
ret = switch_ns(ns_pid, CLONE_NEWUTS, "uts");
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = uname(&ubuf);
|
|
|
|
if (ret < 0) {
|
2012-01-31 15:13:05 +04:00
|
|
|
pr_perror("Error calling uname");
|
2012-01-31 11:29:23 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = fdset->fds[CR_FD_UTSNS];
|
|
|
|
|
|
|
|
ret = dump_uts_string(fd, ubuf.nodename);
|
|
|
|
if (!ret)
|
|
|
|
ret = dump_uts_string(fd, ubuf.domainname);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-02 19:55:48 +04:00
|
|
|
static int read_uts_str(int fd, char *n, int size)
|
2012-01-31 11:29:23 +03:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u32 len;
|
|
|
|
|
|
|
|
ret = read_img(fd, &len);
|
2012-02-02 19:55:48 +04:00
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
2012-01-31 11:29:23 +03:00
|
|
|
|
2012-02-02 19:55:48 +04:00
|
|
|
if (len >= size) {
|
|
|
|
pr_err("Corrupted %s\n", n);
|
|
|
|
return -1;
|
2012-01-31 11:29:23 +03:00
|
|
|
}
|
|
|
|
|
2012-02-02 19:55:48 +04:00
|
|
|
ret = read_img_buf(fd, n, len);
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
n[len] = '\0';
|
|
|
|
return 0;
|
2012-01-31 11:29:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int prepare_utsns(int pid)
|
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
u32 len;
|
2012-02-02 19:55:48 +04:00
|
|
|
char hostname[65];
|
|
|
|
char domainname[65];
|
|
|
|
|
|
|
|
struct sysctl_req req[] = {
|
|
|
|
{ "kernel/hostname", hostname, CTL_STR(sizeof(hostname)) },
|
|
|
|
{ "kernel/domainname", domainname, CTL_STR(sizeof(hostname)) },
|
|
|
|
{ },
|
|
|
|
};
|
2012-01-31 11:29:23 +03:00
|
|
|
|
|
|
|
fd = open_image_ro(CR_FD_UTSNS, pid);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
2012-02-02 19:55:48 +04:00
|
|
|
ret = read_uts_str(fd, hostname, sizeof(hostname));
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = read_uts_str(fd, domainname, sizeof(domainname));
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
2012-01-31 11:29:23 +03:00
|
|
|
|
2012-02-02 19:55:48 +04:00
|
|
|
ret = sysctl_op(req, CTL_WRITE);
|
|
|
|
out:
|
2012-01-31 11:29:23 +03:00
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_uts_string(int fd, char *n)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u32 len;
|
|
|
|
char str[65];
|
|
|
|
|
2012-02-02 21:18:32 +04:00
|
|
|
ret = read_img_eof(fd, &len);
|
2012-01-31 11:29:23 +03:00
|
|
|
if (ret > 0) {
|
2012-01-31 23:29:24 +04:00
|
|
|
if (len >= sizeof(str)) {
|
2012-01-31 11:29:23 +03:00
|
|
|
pr_err("Corrupted hostname\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = read_img_buf(fd, str, len);
|
|
|
|
if (ret < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
str[len] = '\0';
|
|
|
|
pr_info("%s: [%s]\n", n, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void show_utsns(int fd)
|
|
|
|
{
|
|
|
|
pr_img_head(CR_FD_UTSNS);
|
|
|
|
show_uts_string(fd, "hostname");
|
|
|
|
show_uts_string(fd, "domainname");
|
|
|
|
pr_img_tail(CR_FD_UTSNS);
|
|
|
|
}
|