platform: add nmp_netns_bind_to_path() helper function
[NetworkManager.git] / src / platform / nmp-netns.h
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* nm-platform.c - Handle runtime kernel networking configuration
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Copyright (C) 2016 Red Hat, Inc.
19  */
20
21 #ifndef __NMP_NETNS_UTILS_H__
22 #define __NMP_NETNS_UTILS_H__
23
24 /*****************************************************************************/
25
26 #define NMP_TYPE_NETNS            (nmp_netns_get_type ())
27 #define NMP_NETNS(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), NMP_TYPE_NETNS, NMPNetns))
28 #define NMP_NETNS_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), NMP_TYPE_NETNS, NMPNetnsClass))
29 #define NMP_IS_NETNS(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NMP_TYPE_NETNS))
30 #define NMP_IS_NETNS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NMP_TYPE_NETNS))
31 #define NMP_NETNS_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), NMP_TYPE_NETNS, NMPNetnsClass))
32
33 #define NMP_NETNS_FD_NET          "fd-net"
34 #define NMP_NETNS_FD_MNT          "fd-mnt"
35
36 struct _NMPNetnsPrivate;
37
38 struct _NMPNetns {
39         GObject parent;
40         struct _NMPNetnsPrivate *priv;
41 };
42
43 typedef struct {
44         GObjectClass parent;
45 } NMPNetnsClass;
46
47 GType nmp_netns_get_type (void);
48
49 NMPNetns *nmp_netns_new (void);
50
51 gboolean nmp_netns_push (NMPNetns *self);
52 gboolean nmp_netns_push_type (NMPNetns *self, int ns_types);
53 gboolean nmp_netns_pop (NMPNetns *self);
54
55 NMPNetns *nmp_netns_get_current (void);
56 NMPNetns *nmp_netns_get_initial (void);
57 gboolean nmp_netns_is_initial (void);
58
59 int nmp_netns_get_fd_net (NMPNetns *self);
60 int nmp_netns_get_fd_mnt (NMPNetns *self);
61
62 static inline void
63 _nm_auto_pop_netns (NMPNetns **p)
64 {
65         if (*p)
66                 nmp_netns_pop (*p);
67 }
68
69 #define nm_auto_pop_netns __attribute__((cleanup(_nm_auto_pop_netns)))
70
71 gboolean nmp_netns_bind_to_path (NMPNetns *self, const char *filename, int *out_fd);
72 gboolean nmp_netns_bind_to_path_destroy (NMPNetns *self, const char *filename);
73
74 #endif /* __NMP_NETNS_UTILS_H__ */