be4468840229d405756502c89ab294f413b6b579
[util-vserver.git] / util-vserver / lib / vserver.h
1 /* $Id$
2
3 *  Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 *   
5 *  This program is free software; you can redistribute it and/or modify
6 *  it under the terms of the GNU General Public License as published by
7 *  the Free Software Foundation; either version 2, or (at your option)
8 *  any later version.
9 *   
10 *  This program is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *  GNU General Public License for more details.
14 *   
15 *  You should have received a copy of the GNU General Public License
16 *  along with this program; if not, write to the Free Software
17 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #ifndef H_VSERVER_SYSCALL_H
21 #define H_VSERVER_SYSCALL_H
22
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <sys/types.h>
27
28 /** the value which is returned in error-case (no ctx found) */
29 #define VC_NOCTX                ((xid_t)(-1))
30 /** the value which means a random (the next free) ctx */
31 #define VC_DYNAMIC_XID          ((xid_t)(-1))
32 /** the value which means the current ctx */
33 #define VC_SAMECTX              ((xid_t)(-2))
34
35 #define VC_LIM_INFINITY         (~0ULL)
36 #define VC_LIM_KEEP             (~1ULL)
37
38   
39 #ifndef S_CTX_INFO_LOCK
40 #  define S_CTX_INFO_LOCK       1
41 #endif
42
43 #ifndef S_CTX_INFO_SCHED
44 #  define S_CTX_INFO_SCHED      2
45 #endif
46
47 #ifndef S_CTX_INFO_NPROC
48 #  define S_CTX_INFO_NPROC      4
49 #endif
50
51 #ifndef S_CTX_INFO_PRIVATE
52 #  define S_CTX_INFO_PRIVATE    8
53 #endif
54
55 #ifndef S_CTX_INFO_INIT
56 #  define S_CTX_INFO_INIT       16
57 #endif
58
59 #ifndef S_CTX_INFO_HIDEINFO
60 #  define S_CTX_INFO_HIDEINFO   32
61 #endif
62
63 #ifndef S_CTX_INFO_ULIMIT
64 #  define S_CTX_INFO_ULIMIT     64
65 #endif
66
67 #ifndef S_CTX_INFO_NAMESPACE
68 #  define S_CTX_INFO_NAMESPACE  128
69 #endif
70
71 #define VC_CAP_CHOWN                     0
72 #define VC_CAP_DAC_OVERRIDE              1
73 #define VC_CAP_DAC_READ_SEARCH           2
74 #define VC_CAP_FOWNER                    3
75 #define VC_CAP_FSETID                    4
76 #define VC_CAP_KILL                      5
77 #define VC_CAP_SETGID                    6
78 #define VC_CAP_SETUID                    7
79 #define VC_CAP_SETPCAP                   8
80 #define VC_CAP_LINUX_IMMUTABLE           9
81 #define VC_CAP_NET_BIND_SERVICE         10
82 #define VC_CAP_NET_BROADCAST            11
83 #define VC_CAP_NET_ADMIN                12
84 #define VC_CAP_NET_RAW                  13
85 #define VC_CAP_IPC_LOCK                 14
86 #define VC_CAP_IPC_OWNER                15
87 #define VC_CAP_SYS_MODULE               16
88 #define VC_CAP_SYS_RAWIO                17
89 #define VC_CAP_SYS_CHROOT               18
90 #define VC_CAP_SYS_PTRACE               19
91 #define VC_CAP_SYS_PACCT                20
92 #define VC_CAP_SYS_ADMIN                21
93 #define VC_CAP_SYS_BOOT                 22
94 #define VC_CAP_SYS_NICE                 23
95 #define VC_CAP_SYS_RESOURCE             24
96 #define VC_CAP_SYS_TIME                 25
97 #define VC_CAP_SYS_TTY_CONFIG           26
98 #define VC_CAP_MKNOD                    27
99 #define VC_CAP_LEASE                    28
100 #define VC_CAP_QUOTACTL                 29
101
102 #define VC_IMMUTABLE_FILE_FL            0x00000010l
103 #define VC_IMMUTABLE_LINK_FL            0x00008000l
104 #define VC_IMMUTABLE_ALL                (VC_IMMUTABLE_LINK_FL|VC_IMMUTABLE_FILE_FL)
105
106 #define VC_IATTR_XID                    0x01000000
107
108 #define VC_IATTR_ADMIN                  0x00000001
109 #define VC_IATTR_WATCH                  0x00000002
110 #define VC_IATTR_HIDE                   0x00000004
111 #define VC_IATTR_FLAGS                  0x00000007
112
113 #define VC_IATTR_BARRIER                0x00010000
114 #define VC_IATTR_IUNLINK                0x00020000
115 #define VC_IATTR_IMMUTABLE              0x00040000
116
117 #ifdef __cplusplus
118 extern "C" {
119 #endif
120
121   struct vc_ip_mask_pair {
122     uint32_t    ip;
123     uint32_t    mask;
124   };
125
126     /** Returns the version of the current kernel API. */
127   int   vc_get_version();
128   
129     /** Puts current process into context <ctx>, removes the given caps and
130      *  sets flags.
131      *  Special values for ctx are
132      *  - VC_SAMECTX  which means the current context (just for changing caps and flags)
133      *  - VC_RANDCTX  which means the next free context; this value can be used by
134      *                ordinary users also
135      *  See http://vserver.13thfloor.at/Stuff/Logic.txt for details */
136   xid_t vc_new_s_context(xid_t ctx, unsigned int remove_cap, unsigned int flags);
137
138     /** Sets the ipv4root information.
139      *  \precondition: nb<16 */
140   int   vc_set_ipv4root(uint32_t  bcast, size_t nb, struct vc_ip_mask_pair const *ips);
141
142     /** Creates a context without starting it.
143      *  When already in a created context, the old context will be discarded.
144      *  Special values of 'xid' are:
145      *  - VC_DYNAMIC_XID which means to create a dynamic context
146      *  \returns the xid of the created context, or VC_NOCTX on errors. errno
147      *           will be set appropriately.*/
148   xid_t vc_create_context(xid_t xid);
149
150     /** Moves the current process into the specified context.
151      *  \returns 0 on success, -1 on errors */
152   int   vc_migrate_context(xid_t xid);
153   
154     /* rlimit related functions */
155   typedef uint_least64_t        vc_limit_t;
156  
157   struct vc_rlimit {
158       vc_limit_t        min;
159       vc_limit_t        soft;
160       vc_limit_t        hard;
161   };
162
163   struct  vc_rlimit_mask {
164       uint_least32_t    min;
165       uint_least32_t    soft;
166       uint_least32_t    hard;
167   };
168
169   int   vc_get_rlimit(xid_t ctx, int resource, struct vc_rlimit *lim);
170   int   vc_set_rlimit(xid_t ctx, int resource, struct vc_rlimit const *lim);
171   int   vc_get_rlimit_mask(xid_t ctx, struct vc_rlimit_mask *lim);
172   bool  vc_parseLimit(char const *, vc_limit_t *);
173
174
175   /** sends a signal to a context/pid
176       Special values for pid are:
177       * -1   which means every process in ctx except the init-process
178       *  0   which means every process in ctx inclusive the init-process */
179   int   vc_ctx_kill(xid_t ctx, pid_t pid, int sig);
180
181
182
183   int           vc_set_iattr(char const *filename, xid_t xid,
184                              uint_least32_t flags, uint_least32_t mask); 
185   int           vc_get_iattr(char const *filename, xid_t * /*@null@*/ xid,
186                              uint_least32_t * /*@null@*/ flags,
187                              uint_least32_t * /*@null@*/ mask);
188
189   struct vc_vx_info {
190       xid_t     xid;
191       pid_t     initpid;
192   };
193   
194     /** Returns the context of the given process. pid==0 means the current process. */
195   xid_t         vc_get_task_xid(pid_t pid);
196   int           vc_get_vx_info(xid_t xid, struct vc_vx_info *info);
197
198
199   typedef enum { vcVHI_CONTEXT, vcVHI_SYSNAME, vcVHI_NODENAME,
200                  vcVHI_RELEASE, vcVHI_VERSION, vcVHI_MACHINE,
201                  vcVHI_DOMAINNAME }             vc_uts_type;
202   
203   int           vc_set_vhi_name(xid_t xid, vc_uts_type type, char const *val, size_t len);
204   int           vc_get_vhi_name(xid_t xid, vc_uts_type type, char *val, size_t len);
205
206
207   int           vc_enter_namespace(xid_t xid);
208   int           vc_set_namespace();
209   int           vc_cleanup_namespace();
210
211   struct  vc_ctx_flags {
212       uint_least64_t    flagword;
213       uint_least64_t    mask;
214   };
215   
216   struct  vc_ctx_caps {
217       uint_least64_t    bcaps;
218       uint_least64_t    ccaps;
219       uint_least64_t    cmask;
220   };
221
222   struct vc_err_listparser {
223       char const        *ptr;
224       size_t            len;
225   };
226  
227   int                   vc_get_flags(xid_t xid, struct vc_ctx_flags *);
228   int                   vc_set_flags(xid_t xid, struct vc_ctx_flags const *);
229
230   int                   vc_get_ccaps(xid_t xid, struct vc_ctx_caps *);
231   int                   vc_set_ccaps(xid_t xid, struct vc_ctx_caps const *);
232
233   uint_least64_t        vc_text2bcap(char const *, size_t len);
234   char const *          vc_lobcap2text(uint_least64_t *);
235   int                   vc_list2bcap(char const *, size_t len,
236                                      struct vc_err_listparser *err,
237                                      struct vc_ctx_caps *);
238
239   uint_least64_t        vc_text2ccap(char const *, size_t len);
240   char const *          vc_loccap2text(uint_least64_t *);
241   int                   vc_list2ccap(char const *, size_t len,
242                                      struct vc_err_listparser *err,
243                                      struct vc_ctx_caps *);
244
245   int                   vc_list2flag(char const *, size_t len,
246                                      struct vc_err_listparser *err,
247                                      struct vc_ctx_flags *flags);
248   uint_least64_t        vc_text2flag(char const *, size_t len);
249   char const *          vc_loflag2text(uint_least64_t *);
250   
251   uint_least32_t        vc_list2flag_compat(char const *, size_t len,
252                                             struct vc_err_listparser *err);
253   uint_least32_t        vc_text2flag_compat(char const *, size_t len);
254   char const *          vc_hiflag2text_compat(uint_least32_t);
255   
256   int           vc_text2cap(char const *);
257   char const *  vc_cap2text(unsigned int);
258
259   inline static uint_least32_t  vc_get_securecaps() {
260     return ( (1<<VC_CAP_LINUX_IMMUTABLE) | (1<<VC_CAP_NET_BROADCAST) |
261              (1<<VC_CAP_NET_ADMIN) | (1<<VC_CAP_NET_RAW) |
262              (1<<VC_CAP_IPC_LOCK) | (1<<VC_CAP_IPC_OWNER) |
263              (1<<VC_CAP_SYS_MODULE) | (1<<VC_CAP_SYS_RAWIO) |
264              (1<<VC_CAP_SYS_PACCT) | (1<<VC_CAP_SYS_ADMIN) |
265              (1<<VC_CAP_SYS_BOOT) | (1<<VC_CAP_SYS_NICE) |
266              (1<<VC_CAP_SYS_RESOURCE) | (1<<VC_CAP_SYS_TIME) |
267              (1<<VC_CAP_MKNOD) | (1<<VC_CAP_QUOTACTL) );
268   }
269
270   inline static int             vc_setfilecontext(char const *filename, xid_t xid) {
271     return vc_set_iattr(filename, xid, 0, VC_IATTR_XID);
272   }
273   
274   inline static xid_t           vc_getfilecontext(char const *filename) {
275     xid_t       res;
276     if (vc_get_iattr(filename, &res, 0,0)==-1) return VC_NOCTX;
277     return res;
278   }
279
280
281   struct vc_set_sched {
282       int32_t   fill_rate;
283       int32_t   period;
284       int32_t   fill_level;
285       int32_t   bucket_size;
286   };
287
288   int           vc_set_sched(xid_t xid, struct vc_set_sched const *);
289   
290   
291   typedef enum { vcFEATURE_VKILL,  vcFEATURE_IATTR,   vcFEATURE_RLIMIT,
292                  vcFEATURE_COMPAT, vcFEATURE_MIGRATE, vcFEATURE_NAMESPACE,
293                  vcFEATURE_SCHED,  vcFEATURE_VINFO,   vcFEATURE_VHI }
294     vcFeatureSet;
295
296   bool          vc_isSupported(vcFeatureSet);
297   bool          vc_isSupportedString(char const *);
298   
299   /* The management part */
300
301 #define VC_LIMIT_VSERVER_NAME_LEN       1024
302   
303   typedef enum { vcCFG_NONE, vcCFG_AUTO,
304                  vcCFG_LEGACY,
305                  vcCFG_RECENT_SHORT,
306                  vcCFG_RECENT_FULL }            vcCfgStyle;
307
308   vcCfgStyle    vc_getVserverCfgStyle(char const *id);
309   
310   /** Resolves the name of the vserver. The result will be allocated and must
311       be freed by the caller. */
312   char *        vc_getVserverName(char const *id, vcCfgStyle style);
313
314   /** Returns the path of the vserver configuration directory. When the given
315    *  vserver does not exist, or when it does not have such a directory, NULL
316    *  will be returned. Else, the result will be allocated and must be freed
317    *  by the caller. */
318   char *        vc_getVserverCfgDir(char const *id, vcCfgStyle style);
319
320   /** Returns the path of the configuration directory for the given
321    *  application. The result will be allocated and must be freed by the
322    *  caller. */
323   char *        vc_getVserverAppDir(char const *id, vcCfgStyle style, char const *app);
324
325   /** Returns the path to the vserver root-directory. The result will be
326    *  allocated and must be freed by the caller. */
327   char *        vc_getVserverVdir(char const *id, vcCfgStyle style, bool physical);
328
329   /** Returns the ctx of the given vserver. When vserver is not running and
330    *  'honor_static' is false, VC_NOCTX will be returned. Else, when
331    *  'honor_static' is true and a static assignment exists, those value will
332    *  be returned. Else, the result will be VC_NOCTX.
333    *
334    *  When 'is_running' is not null, the status of the vserver will be
335    *  assigned to this variable. */
336   xid_t         vc_getVserverCtx(char const *id, vcCfgStyle style,
337                                  bool honor_static, bool /*@null@*/ *is_running);
338
339   /** Resolves the cfg-path of the vserver owning the given ctx. 'revdir' will
340       be used as the directory holding the mapping-links; when NULL, the
341       default value will be assumed.  The result will be allocated and must be
342       freed by the caller. */
343   char *        vc_getVserverByCtx(xid_t ctx, /*@null@*/vcCfgStyle *style,
344                                    /*@null@*/char const *revdir);
345
346 #define vcSKEL_INTERFACES       1u
347 #define vcSKEL_PKGMGMT          2u
348 #define vcSKEL_FILESYSTEM       4u
349   
350   /** Create a basic configuration skeleton for a vserver plus toplevel
351    *  directories for pkgmanagemt and filesystem (when requested). */
352   int           vc_createSkeleton(char const *id, vcCfgStyle style, int flags);
353   
354 #ifdef __cplusplus
355 }
356 #endif
357
358 #endif