Commit 796d4ceb authored by Andrey Vagin's avatar Andrey Vagin Committed by Pavel Emelyanov

socket: switch netns for collecting sockets

All sockets should be collected in a target net name-space when the -n net
is specified.
Signed-off-by: 's avatarAndrey Vagin <avagin@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 1e1837a8
......@@ -33,7 +33,7 @@ static int check_sock_diag(void)
{
int ret;
ret = collect_sockets();
ret = collect_sockets(getpid());
if (!ret)
return 0;
......
......@@ -1515,7 +1515,7 @@ int cr_dump_tasks(pid_t pid, const struct cr_options *opts)
if (mntns_collect_root(root_item->pid.real))
goto err;
collect_sockets();
collect_sockets(pid);
glob_fdset = cr_glob_fdset_open(O_DUMP);
if (!glob_fdset)
......
......@@ -31,7 +31,7 @@ extern int restore_socket_opts(int sk, SkOptsEntry *soe);
extern void show_socket_opts(SkOptsEntry *soe);
extern int sk_collect_one(int ino, int family, struct socket_desc *d);
extern int collect_sockets(void);
extern int collect_sockets(int pid);
extern int collect_inet_sockets(void);
extern int collect_unix_sockets(void);
extern int fix_external_unix_sockets(void);
......
#include <unistd.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
......@@ -10,6 +11,8 @@
#include "files.h"
#include "util-net.h"
#include "sk-packet.h"
#include "namespaces.h"
#include "crtools.h"
#ifndef NETLINK_SOCK_DIAG
#define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
......@@ -170,9 +173,10 @@ static int inet6_udplite_receive_one(struct nlmsghdr *h, void *arg)
return inet_collect_one(h, AF_INET6, SOCK_DGRAM, IPPROTO_UDPLITE);
}
int collect_sockets(void)
int collect_sockets(int pid)
{
int err = 0, tmp;
int rst = -1;
int nl;
struct {
struct nlmsghdr hdr;
......@@ -182,10 +186,18 @@ int collect_sockets(void)
} r;
} req;
if (opts.namespaces_flags & CLONE_NEWNET) {
pr_info("Switching to %d's net for collecting sockets\n", pid);
if (switch_ns(pid, CLONE_NEWNET, "net", &rst))
return -1;
}
nl = socket(PF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
if (nl < 0) {
pr_perror("Can't create sock diag socket");
return -1;
err = -1;
goto out;
}
memset(&req, 0, sizeof(req));
......@@ -261,6 +273,9 @@ int collect_sockets(void)
err = tmp;
close(nl);
out:
if (rst > 0 && restore_ns(rst, CLONE_NEWNET) < 0)
err = -1;
return err;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment