]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/caps.h
Merge pull request #2324 from simos/fix-resource-leak-cid1248106
[mirror_lxc.git] / src / lxc / caps.h
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef __LXC_CAPS_H
25 #define __LXC_CAPS_H
26
27 #include "config.h"
28 #include <stdbool.h>
29
30 #if HAVE_LIBCAP
31 #include <linux/types.h> /* workaround for libcap < 2.17 bug */
32 #include <sys/capability.h>
33
34 extern int lxc_caps_down(void);
35 extern int lxc_caps_up(void);
36 extern int lxc_ambient_caps_up(void);
37 extern int lxc_ambient_caps_down(void);
38 extern int lxc_caps_init(void);
39 extern int lxc_caps_last_cap(void);
40 extern bool lxc_proc_cap_is_set(cap_value_t cap, cap_flag_t flag);
41 extern bool lxc_file_cap_is_set(const char *path, cap_value_t cap,
42 cap_flag_t flag);
43 #else
44 static inline int lxc_caps_down(void)
45 {
46 return 0;
47 }
48
49 static inline int lxc_caps_up(void)
50 {
51 return 0;
52 }
53
54 static inline int lxc_ambient_caps_up(void)
55 {
56 return 0;
57 }
58
59 static inline int lxc_ambient_caps_down(void)
60 {
61 return 0;
62 }
63
64 static inline int lxc_caps_init(void)
65 {
66 return 0;
67 }
68
69 static inline int lxc_caps_last_cap(void)
70 {
71 return 0;
72 }
73
74 typedef int cap_value_t;
75 typedef int cap_flag_t;
76 static inline bool lxc_proc_cap_is_set(cap_value_t cap, cap_flag_t flag)
77 {
78 return false;
79 }
80
81 static inline bool lxc_file_cap_is_set(const char *path, cap_value_t cap,
82 cap_flag_t flag)
83 {
84 return false;
85 }
86 #endif
87
88 #define lxc_priv(__lxc_function) \
89 ({ \
90 __label__ out; \
91 int __ret, __ret2, ___errno = 0; \
92 __ret = lxc_caps_up(); \
93 if (__ret) \
94 goto out; \
95 __ret = __lxc_function; \
96 if (__ret) \
97 ___errno = errno; \
98 __ret2 = lxc_caps_down(); \
99 out: \
100 __ret ? errno = ___errno, __ret : __ret2; \
101 })
102
103 #define lxc_unpriv(__lxc_function) \
104 ({ \
105 __label__ out; \
106 int __ret, __ret2, ___errno = 0; \
107 __ret = lxc_caps_down(); \
108 if (__ret) \
109 goto out; \
110 __ret = __lxc_function; \
111 if (__ret) \
112 ___errno = errno; \
113 __ret2 = lxc_caps_up(); \
114 out: \
115 __ret ? errno = ___errno, __ret : __ret2; \
116 })
117 #endif