summaryrefslogtreecommitdiff
path: root/src/platform/nmp-netns.c
blob: 0c84bb4019ecedf35081e35b61388a3db4219861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* nm-platform.c - Handle runtime kernel networking configuration
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2016 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nmp-netns.h"

#include <fcntl.h>
#include <errno.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pthread.h>

/*****************************************************************************/

/* NOTE: NMPNetns and all code used here must be thread-safe! */

/* we may not call logging functions from the main-thread alone. Hence, we
 * require locking from nm-logging. Indicate that by setting NM_THREAD_SAFE_ON_MAIN_THREAD
 * to zero. */
#undef NM_THREAD_SAFE_ON_MAIN_THREAD
#define NM_THREAD_SAFE_ON_MAIN_THREAD 0

/*****************************************************************************/

#define PROC_SELF_NS_MNT "/proc/self/ns/mnt"
#define PROC_SELF_NS_NET "/proc/self/ns/net"

#define _CLONE_NS_ALL    ((int) (CLONE_NEWNS | CLONE_NEWNET))
#define _CLONE_NS_ALL_V          CLONE_NEWNS , CLONE_NEWNET

NM_UTILS_FLAGS2STR_DEFINE_STATIC (_clone_ns_to_str, int,
	NM_UTILS_FLAGS2STR (CLONE_NEWNS,  "mnt"),
	NM_UTILS_FLAGS2STR (CLONE_NEWNET, "net"),
);

static const char *
__ns_types_to_str (int ns_types, int ns_types_already_set, char *buf, gsize len)
{
	const char *b = buf;
	char bb[200];

	nm_utils_strbuf_append_c (&buf, &len, '[');
	if (ns_types & ~ns_types_already_set) {
		nm_utils_strbuf_append_str (&buf, &len,
		                            _clone_ns_to_str (ns_types & ~ns_types_already_set, bb, sizeof (bb)));
	}
	if (ns_types & ns_types_already_set) {
		if (ns_types & ~ns_types_already_set)
			nm_utils_strbuf_append_c (&buf, &len, '/');
		nm_utils_strbuf_append_str (&buf, &len,
		                            _clone_ns_to_str (ns_types & ns_types_already_set, bb, sizeof (bb)));
	}
	nm_utils_strbuf_append_c (&buf, &len, ']');
	return b;
}
#define _ns_types_to_str(ns_types, ns_types_already_set, buf) \
	__ns_types_to_str (ns_types, ns_types_already_set, buf, sizeof (buf))

/*****************************************************************************/

#define _NMLOG_DOMAIN        LOGD_PLATFORM
#define _NMLOG_PREFIX_NAME   "netns"
#define _NMLOG(level, netns, ...) \
    G_STMT_START { \
        NMLogLevel _level = (level); \
        \
        if (nm_logging_enabled (_level, _NMLOG_DOMAIN)) { \
            NMPNetns *_netns = (netns); \
            char _sbuf[20]; \
            \
            _nm_log (_level, _NMLOG_DOMAIN, 0, NULL, NULL, \
                     "%s%s: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
                     _NMLOG_PREFIX_NAME, \
                     (_netns ? nm_sprintf_buf (_sbuf, "[%p]", _netns) : "") \
                     _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
        } \
    } G_STMT_END

/*****************************************************************************/

NM_GOBJECT_PROPERTIES_DEFINE_BASE (
	PROP_FD_NET,
	PROP_FD_MNT,
);

typedef struct {
	int fd_net;
	int fd_mnt;
} NMPNetnsPrivate;

struct _NMPNetns {
	GObject parent;
	NMPNetnsPrivate _priv;
};

struct _NMPNetnsClass {
	GObjectClass parent;
};

G_DEFINE_TYPE (NMPNetns, nmp_netns, G_TYPE_OBJECT);

#define NMP_NETNS_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMPNetns, NMP_IS_NETNS)

/*****************************************************************************/

typedef struct {
	NMPNetns *netns;
	int count;
	int ns_types;
} NetnsInfo;

static void _stack_push (NMPNetns *netns, int ns_types);
static NMPNetns *_netns_new (GError **error);

/*****************************************************************************/

static _nm_thread_local GArray *netns_stack = NULL;

static void
_netns_stack_clear_cb (gpointer data)
{
	NetnsInfo *info = data;

	nm_assert (NMP_IS_NETNS (info->netns));
	g_object_unref (info->netns);
}

static GArray *
_netns_stack_get_impl (void)
{
	gs_unref_object NMPNetns *netns = NULL;
	gs_free_error GError *error = NULL;
	pthread_key_t key;
	GArray *s;

	s = g_array_new (FALSE, FALSE, sizeof (NetnsInfo));
	g_array_set_clear_func (s, _netns_stack_clear_cb);
	netns_stack = s;

	/* at the bottom of the stack we must try to create a netns instance
	 * that we never pop. It's the base to which we need to return. */
	netns = _netns_new (&error);
	if (!netns) {
		_LOGE (NULL, "failed to create initial netns: %s", error->message);
		return s;
	}

	/* we leak this instance inside the stack. */
	_stack_push (netns, _CLONE_NS_ALL);

	/* finally, register a destructor function to cleanup the array. If we fail
	 * to do so, we will leak NMPNetns instances (and their file descriptor) when the
	 * thread exits. */
	if (pthread_key_create (&key, (void (*) (void *)) g_array_unref) != 0)
		_LOGE (NULL, "failure to initialize thread-local storage");
	else if (pthread_setspecific (key, s) != 0)
		_LOGE (NULL, "failure to set thread-local storage");

	return s;
}

#define _netns_stack_get() \
	({ \
		GArray *_s = netns_stack; \
		\
		if (G_UNLIKELY (!_s)) \
			_s = _netns_stack_get_impl (); \
		_s; \
	})

#define _stack_ensure_init() \
	G_STMT_START { \
		(void) _netns_stack_get (); \
	} G_STMT_END

/*****************************************************************************/

static NMPNetns *
_stack_current_netns (int ns_types)
{
	guint j;

	nm_assert (netns_stack && netns_stack->len > 0);

	/* we search the stack top-down to find the netns that has
	 * all @ns_types set. */
	for (j = netns_stack->len; ns_types && j >= 1; ) {
		NetnsInfo *info;

		info = &g_array_index (netns_stack, NetnsInfo, --j);

		if (NM_FLAGS_ALL (info->ns_types, ns_types))
			return info->netns;
	}

	g_return_val_if_reached (NULL);
}

static int
_stack_current_ns_types (NMPNetns *netns, int ns_types)
{
	const int ns_types_check[] = { _CLONE_NS_ALL_V };
	guint i, j;
	int res = 0;

	nm_assert (netns);
	nm_assert (netns_stack && netns_stack->len > 0);

	/* we search the stack top-down to check which of @ns_types
	 * are already set to @netns. */
	for (j = netns_stack->len; ns_types && j >= 1; ) {
		NetnsInfo *info;

		info = &g_array_index (netns_stack, NetnsInfo, --j);
		if (info->netns != netns) {
			ns_types = NM_FLAGS_UNSET (ns_types, info->ns_types);
			continue;
		}

		for (i = 0; i < G_N_ELEMENTS (ns_types_check); i++) {
			if (   NM_FLAGS_ANY (ns_types, ns_types_check[i])
			    && NM_FLAGS_ANY (info->ns_types, ns_types_check[i])) {
				res = NM_FLAGS_SET (res, ns_types_check[i]);
				ns_types = NM_FLAGS_UNSET (ns_types, ns_types_check[i]);
			}
		}
	}

	return res;
}

static NetnsInfo *
_stack_peek (void)
{
	nm_assert (netns_stack);

	if (netns_stack->len > 0)
		return &g_array_index (netns_stack, NetnsInfo, (netns_stack->len - 1));
	return NULL;
}

static NetnsInfo *
_stack_bottom (void)
{
	nm_assert (netns_stack);

	if (netns_stack->len > 0)
		return &g_array_index (netns_stack, NetnsInfo, 0);
	return NULL;
}

static void
_stack_push (NMPNetns *netns, int ns_types)
{
	NetnsInfo *info;

	nm_assert (netns_stack);
	nm_assert (NMP_IS_NETNS (netns));
	nm_assert (NM_FLAGS_ANY (ns_types, _CLONE_NS_ALL));
	nm_assert (!NM_FLAGS_ANY (ns_types, ~_CLONE_NS_ALL));

	g_array_set_size (netns_stack, netns_stack->len + 1);

	info = &g_array_index (netns_stack, NetnsInfo, (netns_stack->len - 1));
	*info = (NetnsInfo) {
		.netns    = g_object_ref (netns),
		.ns_types = ns_types,
		.count    = 1,
	};
}

static void
_stack_pop (void)
{
	NetnsInfo *info;

	nm_assert (netns_stack);
	nm_assert (netns_stack->len > 1);

	info = &g_array_index (netns_stack, NetnsInfo, (netns_stack->len - 1));

	nm_assert (NMP_IS_NETNS (info->netns));
	nm_assert (info->count == 1);

	g_array_set_size (netns_stack, netns_stack->len - 1);
}

static guint
_stack_size (void)
{
	nm_assert (netns_stack);

	return netns_stack->len;
}

/*****************************************************************************/

static NMPNetns *
_netns_new (GError **error)
{
	NMPNetns *self;
	int fd_net, fd_mnt;
	int errsv;

	fd_net = open (PROC_SELF_NS_NET, O_RDONLY | O_CLOEXEC);
	if (fd_net == -1) {
		errsv = errno;
		g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
		             "Failed opening netns: %s",
		             g_strerror (errsv));
		errno = errsv;
		return NULL;
	}

	fd_mnt = open (PROC_SELF_NS_MNT, O_RDONLY | O_CLOEXEC);
	if (fd_mnt == -1) {
		errsv = errno;
		g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
		             "Failed opening mntns: %s",
		             g_strerror (errsv));
		nm_close (fd_net);
		errno = errsv;
		return NULL;
	}

	self = g_object_new (NMP_TYPE_NETNS,
	                     NMP_NETNS_FD_NET, fd_net,
	                     NMP_NETNS_FD_MNT, fd_mnt,
	                     NULL);

	_LOGD (self, "new netns (net:%d, mnt:%d)", fd_net, fd_mnt);

	return self;
}

static int
_setns (NMPNetns *self, int type)
{
	char buf[100];
	int fd;
	NMPNetnsPrivate *priv = NMP_NETNS_GET_PRIVATE (self);

	nm_assert (NM_IN_SET (type, _CLONE_NS_ALL_V));

	fd = (type == CLONE_NEWNET) ? priv->fd_net : priv->fd_mnt;

	_LOGt (self, "set netns(%s, %d)", _ns_types_to_str (type, 0, buf), fd);

	return setns (fd, type);
}

static gboolean
_netns_switch_push (NMPNetns *self, int ns_types)
{
	int errsv;

	if (   NM_FLAGS_HAS (ns_types, CLONE_NEWNET)
	    && !_stack_current_ns_types (self, CLONE_NEWNET)
	    && _setns (self, CLONE_NEWNET) != 0) {
		errsv = errno;
		_LOGE (self, "failed to switch netns: %s", g_strerror (errsv));
		return FALSE;
	}
	if (   NM_FLAGS_HAS (ns_types, CLONE_NEWNS)
	    && !_stack_current_ns_types (self, CLONE_NEWNS)
	    && _setns (self, CLONE_NEWNS) != 0) {
		errsv = errno;
		_LOGE (self, "failed to switch mntns: %s", g_strerror (errsv));

		/* try to fix the mess by returning to the previous netns. */
		if (   NM_FLAGS_HAS (ns_types, CLONE_NEWNET)
	        && !_stack_current_ns_types (self, CLONE_NEWNET)) {
			self = _stack_current_netns (CLONE_NEWNET);
			if (   self
			    && _setns (self, CLONE_NEWNET) != 0) {
				errsv = errno;
				_LOGE (self, "failed to restore netns: %s", g_strerror (errsv));
			}
		}
		return FALSE;
	}

	return TRUE;
}

static gboolean
_netns_switch_pop (NMPNetns *self, int ns_types)
{
	int errsv;
	NMPNetns *current;
	int success = TRUE;

	if (   NM_FLAGS_HAS (ns_types, CLONE_NEWNET)
	    && (!self || !_stack_current_ns_types (self, CLONE_NEWNET))) {
		current = _stack_current_netns (CLONE_NEWNET);
		if (!current) {
			g_warn_if_reached ();
			success = FALSE;
		} else if (_setns (current, CLONE_NEWNET) != 0) {
			errsv = errno;
			_LOGE (self, "failed to switch netns: %s", g_strerror (errsv));
			success = FALSE;
		}
	}
	if (   NM_FLAGS_HAS (ns_types, CLONE_NEWNS)
	    && (!self || !_stack_current_ns_types (self, CLONE_NEWNS))) {
		current = _stack_current_netns (CLONE_NEWNS);
		if (!current) {
			g_warn_if_reached ();
			success = FALSE;
		} else if (_setns (current, CLONE_NEWNS) != 0) {
			errsv = errno;
			_LOGE (self, "failed to switch mntns: %s", g_strerror (errsv));
			success = FALSE;
		}
	}

	return success;
}

/*****************************************************************************/

int
nmp_netns_get_fd_net (NMPNetns *self)
{
	g_return_val_if_fail (NMP_IS_NETNS (self), 0);

	return NMP_NETNS_GET_PRIVATE (self)->fd_net;
}

int
nmp_netns_get_fd_mnt (NMPNetns *self)
{
	g_return_val_if_fail (NMP_IS_NETNS (self), 0);

	return NMP_NETNS_GET_PRIVATE (self)->fd_mnt;
}

/*****************************************************************************/

static gboolean
_nmp_netns_push_type (NMPNetns *self, int ns_types)
{
	NetnsInfo *info;
	char sbuf[100];

	_stack_ensure_init ();

	info = _stack_peek ();
	g_return_val_if_fail (info, FALSE);

	if (info->netns == self && info->ns_types == ns_types) {
		info->count++;
		_LOGt (self, "push#%u* %s (increase count to %d)",
		       _stack_size () - 1,
		       _ns_types_to_str (ns_types, ns_types, sbuf), info->count);
		return TRUE;
	}

	_LOGD (self, "push#%u %s",
	       _stack_size (),
	       _ns_types_to_str (ns_types,
	                         _stack_current_ns_types (self, ns_types),
	                         sbuf));

	if (!_netns_switch_push (self, ns_types))
		return FALSE;

	_stack_push (self, ns_types);
	return TRUE;
}

gboolean
nmp_netns_push (NMPNetns *self)
{
	g_return_val_if_fail (NMP_IS_NETNS (self), FALSE);

	return _nmp_netns_push_type (self, _CLONE_NS_ALL);
}

gboolean
nmp_netns_push_type (NMPNetns *self, int ns_types)
{
	g_return_val_if_fail (NMP_IS_NETNS (self), FALSE);
	g_return_val_if_fail (!NM_FLAGS_ANY (ns_types, ~_CLONE_NS_ALL), FALSE);

	return _nmp_netns_push_type (self, ns_types == 0 ? _CLONE_NS_ALL : ns_types);
}

NMPNetns *
nmp_netns_new (void)
{
	NMPNetns *self;
	int errsv;
	GError *error = NULL;
	unsigned long mountflags = 0;

	_stack_ensure_init ();

	if (!_stack_peek ()) {
		/* there are no netns instances. We cannot create a new one
		 * (because after unshare we couldn't return to the original one). */
		errno = ENOTSUP;
		return NULL;
	}

	if (unshare (_CLONE_NS_ALL) != 0) {
		errsv = errno;
		_LOGE (NULL, "failed to create new net and mnt namespace: %s", g_strerror (errsv));
		return NULL;
	}

	if (mount ("", "/", "none", MS_SLAVE | MS_REC, NULL) != 0) {
		errsv = errno;
		_LOGE (NULL, "failed mount --make-rslave: %s", g_strerror (errsv));
		goto err_out;
	}

	if (umount2 ("/sys", MNT_DETACH) != 0) {
		errsv = errno;
		_LOGE (NULL, "failed umount /sys: %s", g_strerror (errsv));
		goto err_out;
	}

	if (access ("/sys", W_OK) == -1)
		mountflags = MS_RDONLY;

	if (mount ("sysfs", "/sys", "sysfs", mountflags, NULL) != 0) {
		errsv = errno;
		_LOGE (NULL, "failed mount /sys: %s", g_strerror (errsv));
		goto err_out;
	}

	self = _netns_new (&error);
	if (!self) {
		errsv = errno;
		_LOGE (NULL, "failed to create netns after unshare: %s", error->message);
		g_clear_error (&error);
		goto err_out;
	}

	_stack_push (self, _CLONE_NS_ALL);

	return self;
err_out:
	_netns_switch_pop (NULL, _CLONE_NS_ALL);
	errno = errsv;
	return NULL;
}

gboolean
nmp_netns_pop (NMPNetns *self)
{
	NetnsInfo *info;
	int ns_types;

	g_return_val_if_fail (NMP_IS_NETNS (self), FALSE);

	_stack_ensure_init ();

	info = _stack_peek ();

	g_return_val_if_fail (info, FALSE);
	g_return_val_if_fail (info->netns == self, FALSE);

	if (info->count > 1) {
		info->count--;
		_LOGt (self, "pop#%u* (decrease count to %d)",
		       _stack_size () - 1, info->count);
		return TRUE;
	}
	g_return_val_if_fail (info->count == 1, FALSE);

	/* cannot pop the original netns. */
	g_return_val_if_fail (_stack_size () > 1, FALSE);

	_LOGD (self, "pop#%u", _stack_size () - 1);

	ns_types = info->ns_types;

	_stack_pop ();

	return _netns_switch_pop (self, ns_types);
}

NMPNetns *
nmp_netns_get_current (void)
{
	NetnsInfo *info;

	_stack_ensure_init ();

	info = _stack_peek ();
	return info ? info->netns : NULL;
}

NMPNetns *
nmp_netns_get_initial (void)
{
	NetnsInfo *info;

	_stack_ensure_init ();

	info = _stack_bottom ();
	return info ? info->netns : NULL;
}

gboolean
nmp_netns_is_initial (void)
{
	if (G_UNLIKELY (!netns_stack))
		return TRUE;

	return nmp_netns_get_current () == nmp_netns_get_initial ();
}

/*****************************************************************************/

gboolean
nmp_netns_bind_to_path (NMPNetns *self, const char *filename, int *out_fd)
{
	gs_free char *dirname = NULL;
	int errsv;
	int fd;
	nm_auto_pop_netns NMPNetns *netns_pop = NULL;

	g_return_val_if_fail (NMP_IS_NETNS (self), FALSE);
	g_return_val_if_fail (filename && filename[0] == '/', FALSE);

	if (!nmp_netns_push_type (self, CLONE_NEWNET))
		return FALSE;
	netns_pop = self;

	dirname = g_path_get_dirname (filename);
	if (mkdir (dirname, 0) != 0) {
		errsv = errno;
		if (errsv != EEXIST) {
			_LOGE (self, "bind: failed to create directory %s: %s",
			       dirname, g_strerror (errsv));
			return FALSE;
		}
	}

	if ((fd = creat (filename, S_IRUSR | S_IRGRP | S_IROTH)) == -1) {
		errsv = errno;
		_LOGE (self, "bind: failed to create %s: %s",
		       filename, g_strerror (errsv));
		return FALSE;
	}
	nm_close (fd);

	if (mount (PROC_SELF_NS_NET, filename, "none", MS_BIND, NULL) != 0) {
		errsv = errno;
		_LOGE (self, "bind: failed to mount %s to %s: %s",
		       PROC_SELF_NS_NET, filename, g_strerror (errsv));
		unlink (filename);
		return FALSE;
	}

	if (out_fd) {
		if ((fd = open (filename, O_RDONLY | O_CLOEXEC)) == -1) {
			errsv = errno;
			_LOGE (self, "bind: failed to open %s: %s", filename, g_strerror (errsv));
			umount2 (filename, MNT_DETACH);
			unlink (filename);
			return FALSE;
		}
		*out_fd = fd;
	}

	return TRUE;
}

gboolean
nmp_netns_bind_to_path_destroy (NMPNetns *self, const char *filename)
{
	int errsv;

	g_return_val_if_fail (NMP_IS_NETNS (self), FALSE);
	g_return_val_if_fail (filename && filename[0] == '/', FALSE);

	if (umount2 (filename, MNT_DETACH) != 0) {
		errsv = errno;
		_LOGE (self, "bind: failed to unmount2 %s: %s", filename, g_strerror (errsv));
		return FALSE;
	}
	if (unlink (filename) != 0) {
		errsv = errno;
		_LOGE (self, "bind: failed to unlink %s: %s", filename, g_strerror (errsv));
		return FALSE;
	}
	return TRUE;
}

/*****************************************************************************/

static void
set_property (GObject *object, guint prop_id,
              const GValue *value, GParamSpec *pspec)
{
	NMPNetns *self = NMP_NETNS (object);
	NMPNetnsPrivate *priv = NMP_NETNS_GET_PRIVATE (self);

	switch (prop_id) {
	case PROP_FD_NET:
		/* construct-only */
		priv->fd_net = g_value_get_int (value);
		g_return_if_fail (priv->fd_net > 0);
		break;
	case PROP_FD_MNT:
		/* construct-only */
		priv->fd_mnt = g_value_get_int (value);
		g_return_if_fail (priv->fd_mnt > 0);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nmp_netns_init (NMPNetns *self)
{
}

static void
dispose (GObject *object)
{
	NMPNetns *self = NMP_NETNS (object);
	NMPNetnsPrivate *priv = NMP_NETNS_GET_PRIVATE (self);

	nm_close (priv->fd_net);
	priv->fd_net = -1;

	nm_close (priv->fd_mnt);
	priv->fd_mnt = -1;

	G_OBJECT_CLASS (nmp_netns_parent_class)->dispose (object);
}

static void
nmp_netns_class_init (NMPNetnsClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->set_property = set_property;
	object_class->dispose = dispose;

	obj_properties[PROP_FD_NET]
	    = g_param_spec_int (NMP_NETNS_FD_NET, "", "",
	                        0, G_MAXINT, 0,
	                        G_PARAM_WRITABLE |
	                        G_PARAM_CONSTRUCT_ONLY |
	                        G_PARAM_STATIC_STRINGS);
	obj_properties[PROP_FD_MNT]
	    = g_param_spec_int (NMP_NETNS_FD_MNT, "", "",
	                        0, G_MAXINT, 0,
	                        G_PARAM_WRITABLE |
	                        G_PARAM_CONSTRUCT_ONLY |
	                        G_PARAM_STATIC_STRINGS);
	g_object_class_install_properties (object_class, _PROPERTY_ENUMS_LAST, obj_properties);
}