2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-30 05:48:05 +00:00

net: add a way to get a network namespace for a socket

Each sockets belongs to one network namespace and operates
in this network namespace.

socket_diag reports informations about sockets from
one network namespace, but it doesn't report sockets which
are not bound or connected to somewhere. So we need to have
a way to get network namespaces for such sockets.

Acked-by: Pavel Emelyanov <xemul@virtuozzo.com>
Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
This commit is contained in:
Andrei Vagin 2017-02-15 02:59:31 +03:00
parent a123cbab65
commit 0f5eb79a3f
3 changed files with 26 additions and 5 deletions

View File

@ -187,5 +187,6 @@ extern int __userns_call(const char *func_name, uns_call_t call, int flags,
extern int add_ns_shared_cb(int (*actor)(void *data), void *data);
extern struct ns_id *get_socket_ns(int lfd);
extern struct ns_id *lookup_ns_by_kid(unsigned int kid, struct ns_desc *nd);
#endif /* __CR_NS_H__ */

View File

@ -324,7 +324,7 @@ int rst_add_ns_id(unsigned int id, struct pstree_item *i, struct ns_desc *nd)
return 0;
}
static struct ns_id *lookup_ns_by_kid(unsigned int kid, struct ns_desc *nd)
struct ns_id *lookup_ns_by_kid(unsigned int kid, struct ns_desc *nd)
{
struct ns_id *nsid;

View File

@ -2290,11 +2290,31 @@ static struct ns_id *get_root_netns()
*/
struct ns_id *get_socket_ns(int lfd)
{
if (netns_nr == 1)
return get_root_netns();
struct ns_id *ns;
struct stat st;
int ns_fd;
pr_perror("Unable to get a socket net namespace");
return NULL;
ns_fd = ioctl(lfd, SIOCGSKNS);
if (ns_fd < 0) {
/* backward compatiblity with old kernels */
if (netns_nr == 1)
return get_root_netns();
pr_perror("Unable to get a socket net namespace");
return NULL;
}
if (fstat(ns_fd, &st)) {
pr_perror("Unable to stat a network namespace");
return NULL;
}
ns = lookup_ns_by_kid(st.st_ino, &net_ns_desc);
if (ns == NULL) {
pr_err("Unable to dump a socket from an external network namespace\n");
return NULL;
}
return ns;
}
int kerndat_socket_netns(void)