5bb170a31b9d2cc4f334971906e9678d56cce85a
[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 /** \file vserver.h
21  *  \brief The public interface of the the libvserver library.
22  */
23
24 #ifndef H_VSERVER_SYSCALL_H
25 #define H_VSERVER_SYSCALL_H
26
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <stdbool.h>
30 #include <sys/types.h>
31
32 #ifndef IS_DOXYGEN
33 #if defined(__GNUC__)
34 #  define VC_ATTR_UNUSED                __attribute__((__unused__))
35 #  define VC_ATTR_NORETURN              __attribute__((__noreturn__))
36 #  define VC_ATTR_CONST                 __attribute__((__const__))
37 #  if __GNUC__>3 || (__GNUC__==3 && __GNUC_MINOR__>=3)
38 #    define VC_ATTR_NONNULL(ARGS)       __attribute__((__nonnull__ ARGS))
39 #    define VC_ATTR_ALWAYSINLINE        __attribute__((__always_inline__))
40 #  else
41 #    define VC_ATTR_NONNULL(ARGS)
42 #    define VC_ATTR_ALWAYSINLINE
43 #  endif
44 #  if __GNUC__>3
45 #    define VC_ATTR_PURE                __attribute__((__pure__))
46 #  else
47 #    define VC_ATTR_PURE
48 #  endif
49 #else
50 #  define VC_ATTR_NONNULL(ARGS)
51 #  define VC_ATTR_UNUSED
52 #  define VC_ATTR_NORETURN
53 #  define VC_ATTR_ALWAYSINLINE
54 #  define VC_ATTR_PURE
55 #  define VC_ATTR_CONST
56 #endif
57 #endif  // IS_DOXYGEN
58
59 /** the value which is returned in error-case (no ctx found) */
60 #define VC_NOCTX                ((xid_t)(-1))
61 /** the value which means a random (the next free) ctx */
62 #define VC_DYNAMIC_XID          ((xid_t)(-1))
63 /** the value which means the current ctx */
64 #define VC_SAMECTX              ((xid_t)(-2))
65
66 #define VC_LIM_INFINITY         (~0ULL)
67 #define VC_LIM_KEEP             (~1ULL)
68
69   
70 #ifndef S_CTX_INFO_LOCK
71 #  define S_CTX_INFO_LOCK       1
72 #endif
73
74 #ifndef S_CTX_INFO_SCHED
75 #  define S_CTX_INFO_SCHED      2
76 #endif
77
78 #ifndef S_CTX_INFO_NPROC
79 #  define S_CTX_INFO_NPROC      4
80 #endif
81
82 #ifndef S_CTX_INFO_PRIVATE
83 #  define S_CTX_INFO_PRIVATE    8
84 #endif
85
86 #ifndef S_CTX_INFO_INIT
87 #  define S_CTX_INFO_INIT       16
88 #endif
89
90 #ifndef S_CTX_INFO_HIDEINFO
91 #  define S_CTX_INFO_HIDEINFO   32
92 #endif
93
94 #ifndef S_CTX_INFO_ULIMIT
95 #  define S_CTX_INFO_ULIMIT     64
96 #endif
97
98 #ifndef S_CTX_INFO_NAMESPACE
99 #  define S_CTX_INFO_NAMESPACE  128
100 #endif
101
102 #define VC_CAP_CHOWN                     0
103 #define VC_CAP_DAC_OVERRIDE              1
104 #define VC_CAP_DAC_READ_SEARCH           2
105 #define VC_CAP_FOWNER                    3
106 #define VC_CAP_FSETID                    4
107 #define VC_CAP_KILL                      5
108 #define VC_CAP_SETGID                    6
109 #define VC_CAP_SETUID                    7
110 #define VC_CAP_SETPCAP                   8
111 #define VC_CAP_LINUX_IMMUTABLE           9
112 #define VC_CAP_NET_BIND_SERVICE         10
113 #define VC_CAP_NET_BROADCAST            11
114 #define VC_CAP_NET_ADMIN                12
115 #define VC_CAP_NET_RAW                  13
116 #define VC_CAP_IPC_LOCK                 14
117 #define VC_CAP_IPC_OWNER                15
118 #define VC_CAP_SYS_MODULE               16
119 #define VC_CAP_SYS_RAWIO                17
120 #define VC_CAP_SYS_CHROOT               18
121 #define VC_CAP_SYS_PTRACE               19
122 #define VC_CAP_SYS_PACCT                20
123 #define VC_CAP_SYS_ADMIN                21
124 #define VC_CAP_SYS_BOOT                 22
125 #define VC_CAP_SYS_NICE                 23
126 #define VC_CAP_SYS_RESOURCE             24
127 #define VC_CAP_SYS_TIME                 25
128 #define VC_CAP_SYS_TTY_CONFIG           26
129 #define VC_CAP_MKNOD                    27
130 #define VC_CAP_LEASE                    28
131 #define VC_CAP_QUOTACTL                 29
132
133 #define VC_IMMUTABLE_FILE_FL            0x00000010l
134 #define VC_IMMUTABLE_LINK_FL            0x00008000l
135 #define VC_IMMUTABLE_ALL                (VC_IMMUTABLE_LINK_FL|VC_IMMUTABLE_FILE_FL)
136
137 #define VC_IATTR_XID                    0x01000000
138
139 #define VC_IATTR_ADMIN                  0x00000001
140 #define VC_IATTR_WATCH                  0x00000002
141 #define VC_IATTR_HIDE                   0x00000004
142 #define VC_IATTR_FLAGS                  0x00000007
143
144 #define VC_IATTR_BARRIER                0x00010000
145 #define VC_IATTR_IUNLINK                0x00020000
146 #define VC_IATTR_IMMUTABLE              0x00040000
147
148
149 // the flags
150 #define VC_VXF_INFO_LOCK                0x00000001
151 #define VC_VXF_INFO_NPROC               0x00000002
152 #define VC_VXF_INFO_PRIVATE             0x00000004
153 #define VC_VXF_INFO_INIT                0x00000008
154
155 #define VC_VXF_INFO_HIDE                0x00000010
156 #define VC_VXF_INFO_ULIMIT              0x00000020
157 #define VC_VXF_INFO_NSPACE              0x00000040
158
159 #define VC_VXF_SCHED_HARD               0x00000100
160 #define VC_VXF_SCHED_PRIO               0x00000200
161 #define VC_VXF_SCHED_PAUSE              0x00000400
162
163 #define VC_VXF_VIRT_MEM                 0x00010000
164 #define VC_VXF_VIRT_UPTIME              0x00020000
165
166 #define VC_VXF_STATE_SETUP              (1ULL<<32)
167 #define VC_VXF_STATE_INIT               (1ULL<<33)
168
169
170 /** \defgroup  syscalls Syscall wrappers
171  *  Functions which are calling the vserver syscall directly. */
172
173 /** \defgroup  helper   Helper functions
174  *  Functions which are doing general helper tasks like parameter parsing. */
175
176 /** \typedef  an_unsigned_integer_type  xid_t
177  *  The identifier of a context. */
178
179 #ifdef IS_DOXYGEN
180 typedef an_unsigned_integer_type        xid_t;
181 #endif
182
183 #ifdef __cplusplus
184 extern "C" {
185 #endif
186
187   struct vc_ip_mask_pair {
188     uint32_t    ip;
189       uint32_t  mask;
190   };
191
192     /** \brief   Returns the version of the current kernel API.
193      *  \ingroup syscalls
194      *  \returns The versionnumber of the kernel API
195      */
196   int   vc_get_version();
197   
198     /** \brief   Moves current process into a context
199      *  \ingroup syscalls
200      *
201      *  Puts current process into context \a ctx, removes the capabilities
202      *  given in \a remove_cap and sets \a flags.
203      *
204      *  \param ctx         The new context; special values for are
205      *  - VC_SAMECTX      which means the current context (just for changing caps and flags)
206      *  - VC_DYNAMIC_XID  which means the next free context; this value can be used by
207      *                    ordinary users also
208      *  \param remove_cap  The linux capabilities which will be \b removed.
209      *  \param flags       Special flags which will be set.
210      *
211      *  \returns  The new context-id, or VC_NOCTX on errors; errno
212      *            will be set appropriately
213      *
214      *  See http://vserver.13thfloor.at/Stuff/Logic.txt for details */
215   xid_t vc_new_s_context(xid_t ctx, unsigned int remove_cap, unsigned int flags);
216
217     /** \brief  Sets the ipv4root information.
218      *  \ingroup syscalls
219      *  \pre    \a nb < NB_IPV4ROOT && \a ips != 0 */
220   int   vc_set_ipv4root(uint32_t  bcast, size_t nb,
221                         struct vc_ip_mask_pair const *ips) VC_ATTR_NONNULL((3));
222
223     /** \brief  Returns the value of NB_IPV4ROOT.
224      *  \ingroup helper
225      *
226      *  This function returns the value of NB_IPV4ROOT which was used when the
227      *  library was built, but \b not the value which is used by the currently
228      *  running kernel. */
229   size_t        vc_get_nb_ipv4root() VC_ATTR_CONST VC_ATTR_PURE;
230
231     /** \brief   Creates a context without starting it.
232      *  \ingroup syscalls
233      *
234      *  This functions initializes a new context. When already in a freshly
235      *  created context, this old context will be discarded.
236      *
237      *  \param xid  The new context; special values are:
238      *  - VC_DYNAMIC_XID which means to create a dynamic context
239      *
240      *  \returns the xid of the created context, or VC_NOCTX on errors. errno
241      *           will be set appropriately. */
242   xid_t vc_create_context(xid_t xid);
243
244     /** \brief   Moves the current process into the specified context.
245      *  \ingroup syscalls
246      *
247      *  \param   xid  The new context
248      *  \returns 0 on success, -1 on errors */
249   int   vc_migrate_context(xid_t xid);
250   
251     /* rlimit related functions */
252   
253     /** \brief  The type which is used for a single limit value.
254      *
255      *  Special values are
256      *  - VC_LIM_INFINITY ... which is the infinite value
257      *  - VC_LIM_KEEP     ... which is used to mark values which shall not be
258      *                        modified by the vc_set_rlimit() operation.
259      *
260      *  Else, the interpretation of the value depends on the corresponding
261      *  resource; it might be bytes, pages, seconds or litres of beer. */
262   typedef uint_least64_t        vc_limit_t;
263
264     /** \brief  The limits of a resources.
265      *
266      *  This is a triple consisting of a minimum, soft and hardlimit. */
267   struct vc_rlimit {
268       vc_limit_t        min;    ///< the guaranted minimum of a resources
269       vc_limit_t        soft;   ///< the softlimit of a resource
270       vc_limit_t        hard;   ///< the absolute hardlimit of a resource
271   };
272
273     /** \brief  Masks describing the supported limits. */
274   struct  vc_rlimit_mask {
275       uint_least32_t    min;    ///< masks the resources supporting a minimum limit
276       uint_least32_t    soft;   ///< masks the resources supporting a soft limit
277       uint_least32_t    hard;   ///< masks the resources supporting a hard limit
278   };
279
280     /** \brief   Returns the limits of \a resource.
281      *  \ingroup syscalls
282      *
283      *  \param  xid       The id of the context
284      *  \param  resource  The resource which will be queried
285      *  \param  lim       The result which will be filled with the limits
286      *
287      *  \returns 0 on success, and -1 on errors. */
288   int   vc_get_rlimit(xid_t xid, int resource,
289                       struct vc_rlimit       /*@out@*/ *lim) VC_ATTR_NONNULL((3));
290     /** \brief   Sets the limits of \a resource.
291      *  \ingroup syscalls
292      *
293      *  \param  xid       The id of the context
294      *  \param  resource  The resource which will be queried
295      *  \param  lim       The new limits
296      *
297      *  \returns 0 on success, and -1 on errors. */
298   int   vc_set_rlimit(xid_t xid, int resource,
299                       struct vc_rlimit const /*@in@*/  *lim) VC_ATTR_NONNULL((3));
300   int   vc_get_rlimit_mask(xid_t xid,
301                            struct vc_rlimit_mask *lim)       VC_ATTR_NONNULL((2));
302     /** \brief   Parses a string describing a limit
303      *  \ingroup helper
304      *
305      *  This function parses \a str and interprets special words like \p "inf"
306      *  or suffixes. Valid suffixes are
307      *  - \p k ... 1000
308      *  - \p m ... 1000000
309      *  - \p K ... 1024
310      *  - \p M ... 1048576
311      *
312      *  \param str  The string which shall be parsed
313      *  \param res  Will be filled with the interpreted value; in errorcase,
314      *              this value is undefined.
315      *
316      *  \returns \a true, iff the string \a str could be parsed. \a res will
317      *  be filled with the interpreted value in this case. 
318      *
319      *  \pre \a str!=0 && \a res!=0
320      */
321   bool  vc_parseLimit(char const /*@in@*/ *str, vc_limit_t /*@out@*/ *res)      VC_ATTR_NONNULL((1,2));
322
323
324   /** \brief    Sends a signal to a context/pid
325    *  \ingroup  syscalls
326    *
327    *  Special values for \a pid are:
328    *  - -1   which means every process in ctx except the init-process
329    *  -  0   which means every process in ctx inclusive the init-process */
330   int   vc_ctx_kill(xid_t ctx, pid_t pid, int sig);
331
332
333
334   int           vc_set_iattr(char const *filename, xid_t xid,
335                              uint_least32_t flags, uint_least32_t mask) VC_ATTR_NONNULL((1));
336   int           vc_get_iattr(char const *filename, xid_t * /*@null@*/ xid,
337                              uint_least32_t * /*@null@*/ flags,
338                              uint_least32_t * /*@null@*/ mask) VC_ATTR_NONNULL((1));
339
340   struct vc_vx_info {
341       xid_t     xid;
342       pid_t     initpid;
343   };
344   
345     /** Returns the context of the given process. pid==0 means the current process. */
346   xid_t         vc_get_task_xid(pid_t pid);
347   int           vc_get_vx_info(xid_t xid, struct vc_vx_info *info) VC_ATTR_NONNULL((2));
348
349
350   typedef enum { vcVHI_CONTEXT, vcVHI_SYSNAME, vcVHI_NODENAME,
351                  vcVHI_RELEASE, vcVHI_VERSION, vcVHI_MACHINE,
352                  vcVHI_DOMAINNAME }             vc_uts_type;
353   
354   int           vc_set_vhi_name(xid_t xid, vc_uts_type type,
355                                 char const *val, size_t len) VC_ATTR_NONNULL((3));
356   int           vc_get_vhi_name(xid_t xid, vc_uts_type type,
357                                 char *val, size_t len)       VC_ATTR_NONNULL((3));
358
359     /** Returns true iff \a xid is a dynamic xid */
360   bool          vc_is_dynamic_xid(xid_t xid);
361
362   int           vc_enter_namespace(xid_t xid);
363   int           vc_set_namespace();
364   int           vc_cleanup_namespace();
365
366   struct  vc_ctx_flags {
367       uint_least64_t    flagword;
368       uint_least64_t    mask;
369   };
370   
371   struct  vc_ctx_caps {
372       uint_least64_t    bcaps;
373       uint_least64_t    ccaps;
374       uint_least64_t    cmask;
375   };
376
377   struct vc_err_listparser {
378       char const        *ptr;
379       size_t            len;
380   };
381  
382   int                   vc_get_flags(xid_t xid, struct vc_ctx_flags *)       VC_ATTR_NONNULL((2));
383   int                   vc_set_flags(xid_t xid, struct vc_ctx_flags const *) VC_ATTR_NONNULL((2));
384
385   int                   vc_get_ccaps(xid_t xid, struct vc_ctx_caps *);
386   int                   vc_set_ccaps(xid_t xid, struct vc_ctx_caps const *);
387
388   uint_least64_t        vc_text2bcap(char const *, size_t len);
389   char const *          vc_lobcap2text(uint_least64_t *);
390   int                   vc_list2bcap(char const *, size_t len,
391                                      struct vc_err_listparser *err,
392                                      struct vc_ctx_caps *);
393
394   uint_least64_t        vc_text2ccap(char const *, size_t len);
395   char const *          vc_loccap2text(uint_least64_t *);
396   int                   vc_list2ccap(char const *, size_t len,
397                                      struct vc_err_listparser *err,
398                                      struct vc_ctx_caps *);
399
400   int                   vc_list2flag(char const *, size_t len,
401                                      struct vc_err_listparser *err,
402                                      struct vc_ctx_flags *flags);
403   uint_least64_t        vc_text2flag(char const *, size_t len);
404   char const *          vc_loflag2text(uint_least64_t *);
405   
406   uint_least32_t        vc_list2flag_compat(char const *, size_t len,
407                                             struct vc_err_listparser *err);
408   uint_least32_t        vc_text2flag_compat(char const *, size_t len);
409   char const *          vc_hiflag2text_compat(uint_least32_t);
410   
411   int           vc_text2cap(char const *);
412   char const *  vc_cap2text(unsigned int);
413
414   inline static uint_least32_t  vc_get_securecaps() {
415     return ( (1<<VC_CAP_LINUX_IMMUTABLE) | (1<<VC_CAP_NET_BROADCAST) |
416              (1<<VC_CAP_NET_ADMIN) | (1<<VC_CAP_NET_RAW) |
417              (1<<VC_CAP_IPC_LOCK) | (1<<VC_CAP_IPC_OWNER) |
418              (1<<VC_CAP_SYS_MODULE) | (1<<VC_CAP_SYS_RAWIO) |
419              (1<<VC_CAP_SYS_PACCT) | (1<<VC_CAP_SYS_ADMIN) |
420              (1<<VC_CAP_SYS_BOOT) | (1<<VC_CAP_SYS_NICE) |
421              (1<<VC_CAP_SYS_RESOURCE) | (1<<VC_CAP_SYS_TIME) |
422              (1<<VC_CAP_MKNOD) | (1<<VC_CAP_QUOTACTL) );
423   }
424
425   inline static int             vc_setfilecontext(char const *filename, xid_t xid) {
426     return vc_set_iattr(filename, xid, 0, VC_IATTR_XID);
427   }
428   
429   inline static xid_t           vc_getfilecontext(char const *filename) {
430     xid_t       res;
431     if (vc_get_iattr(filename, &res, 0,0)==-1) return VC_NOCTX;
432     return res;
433   }
434
435
436   struct vc_set_sched {
437       int32_t   fill_rate;
438       int32_t   interval;
439       int32_t   tokens;
440       int32_t   tokens_min;
441       int32_t   tokens_max;
442       uint64_t  cpu_mask;
443   };
444
445   int           vc_set_sched(xid_t xid, struct vc_set_sched const *);
446   
447   
448   typedef enum { vcFEATURE_VKILL,  vcFEATURE_IATTR,   vcFEATURE_RLIMIT,
449                  vcFEATURE_COMPAT, vcFEATURE_MIGRATE, vcFEATURE_NAMESPACE,
450                  vcFEATURE_SCHED,  vcFEATURE_VINFO,   vcFEATURE_VHI }
451     vcFeatureSet;
452
453   bool          vc_isSupported(vcFeatureSet);
454   bool          vc_isSupportedString(char const *);
455   
456   /* The management part */
457
458 #define VC_LIMIT_VSERVER_NAME_LEN       1024
459   
460   typedef enum { vcCFG_NONE, vcCFG_AUTO,
461                  vcCFG_LEGACY,
462                  vcCFG_RECENT_SHORT,
463                  vcCFG_RECENT_FULL }            vcCfgStyle;
464
465   vcCfgStyle    vc_getVserverCfgStyle(char const *id);
466   
467   /** Resolves the name of the vserver. The result will be allocated and must
468       be freed by the caller. */
469   char *        vc_getVserverName(char const *id, vcCfgStyle style);
470
471   /** Returns the path of the vserver configuration directory. When the given
472    *  vserver does not exist, or when it does not have such a directory, NULL
473    *  will be returned. Else, the result will be allocated and must be freed
474    *  by the caller. */
475   char *        vc_getVserverCfgDir(char const *id, vcCfgStyle style);
476
477   /** Returns the path of the configuration directory for the given
478    *  application. The result will be allocated and must be freed by the
479    *  caller. */
480   char *        vc_getVserverAppDir(char const *id, vcCfgStyle style, char const *app);
481
482   /** Returns the path to the vserver root-directory. The result will be
483    *  allocated and must be freed by the caller. */
484   char *        vc_getVserverVdir(char const *id, vcCfgStyle style, bool physical);
485
486   /** Returns the ctx of the given vserver. When vserver is not running and
487    *  'honor_static' is false, VC_NOCTX will be returned. Else, when
488    *  'honor_static' is true and a static assignment exists, those value will
489    *  be returned. Else, the result will be VC_NOCTX.
490    *
491    *  When 'is_running' is not null, the status of the vserver will be
492    *  assigned to this variable. */
493   xid_t         vc_getVserverCtx(char const *id, vcCfgStyle style,
494                                  bool honor_static, bool /*@null@*/ *is_running);
495
496   /** Resolves the cfg-path of the vserver owning the given ctx. 'revdir' will
497       be used as the directory holding the mapping-links; when NULL, the
498       default value will be assumed.  The result will be allocated and must be
499       freed by the caller. */
500   char *        vc_getVserverByCtx(xid_t ctx, /*@null@*/vcCfgStyle *style,
501                                    /*@null@*/char const *revdir);
502
503 #define vcSKEL_INTERFACES       1u
504 #define vcSKEL_PKGMGMT          2u
505 #define vcSKEL_FILESYSTEM       4u
506   
507   /** Create a basic configuration skeleton for a vserver plus toplevel
508    *  directories for pkgmanagemt and filesystem (when requested). */
509   int           vc_createSkeleton(char const *id, vcCfgStyle style, int flags);
510   
511 #ifdef __cplusplus
512 }
513 #endif
514
515 #undef VC_ATTR_PURE
516 #undef VC_ATTR_ALWAYSINLINE
517 #undef VC_ATTR_NORETURN
518 #undef VC_ATTR_UNUSED
519 #undef VC_ATTR_NONNULL
520
521 #endif