return xid_t on vc_new_s_context()
[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_RANDCTX              ((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 #define VC_CAP_CHOWN                     0
68 #define VC_CAP_DAC_OVERRIDE              1
69 #define VC_CAP_DAC_READ_SEARCH           2
70 #define VC_CAP_FOWNER                    3
71 #define VC_CAP_FSETID                    4
72 #define VC_CAP_KILL                      5
73 #define VC_CAP_SETGID                    6
74 #define VC_CAP_SETUID                    7
75 #define VC_CAP_SETPCAP                   8
76 #define VC_CAP_LINUX_IMMUTABLE           9
77 #define VC_CAP_NET_BIND_SERVICE         10
78 #define VC_CAP_NET_BROADCAST            11
79 #define VC_CAP_NET_ADMIN                12
80 #define VC_CAP_NET_RAW                  13
81 #define VC_CAP_IPC_LOCK                 14
82 #define VC_CAP_IPC_OWNER                15
83 #define VC_CAP_SYS_MODULE               16
84 #define VC_CAP_SYS_RAWIO                17
85 #define VC_CAP_SYS_CHROOT               18
86 #define VC_CAP_SYS_PTRACE               19
87 #define VC_CAP_SYS_PACCT                20
88 #define VC_CAP_SYS_ADMIN                21
89 #define VC_CAP_SYS_BOOT                 22
90 #define VC_CAP_SYS_NICE                 23
91 #define VC_CAP_SYS_RESOURCE             24
92 #define VC_CAP_SYS_TIME                 25
93 #define VC_CAP_SYS_TTY_CONFIG           26
94 #define VC_CAP_MKNOD                    27
95 #define VC_CAP_LEASE                    28
96 #define VC_CAP_QUOTACTL                 29
97
98 #define VC_IMMUTABLE_FILE_FL            0x00000010l
99 #define VC_IMMUTABLE_LINK_FL            0x00008000l
100 #define VC_IMMUTABLE_ALL                (VC_IMMUTABLE_LINK_FL|VC_IMMUTABLE_FILE_FL)
101
102 #define VC_IATTR_XID                    0x01000000
103
104 #define VC_IATTR_ADMIN                  0x00000001
105 #define VC_IATTR_WATCH                  0x00000002
106 #define VC_IATTR_HIDE                   0x00000004
107
108 #define VC_IATTR_BARRIER                0x00010000
109 #define VC_IATTR_IUNLINK                0x00020000
110
111
112 #ifdef __cplusplus
113 extern "C" {
114 #endif
115
116   struct vc_ip_mask_pair {
117     uint32_t    ip;
118     uint32_t    mask;
119   };
120
121     /** Returns the version of the current kernel API. */
122   int   vc_get_version();
123   
124     /** Puts current process into context <ctx>, removes the given caps and
125      *  sets flags.
126      *  Special values for ctx are
127      *  - VC_SAMECTX  which means the current context (just for changing caps and flags)
128      *  - VC_RANDCTX  which means the next free context; this value can be used by
129      *                ordinary users also
130      *  See http://vserver.13thfloor.at/Stuff/Logic.txt for details */
131   xid_t vc_new_s_context(xid_t ctx, unsigned int remove_cap, unsigned int flags);
132
133     /** Sets the ipv4root information.
134      *  \precondition: nb<16 */
135   int   vc_set_ipv4root(uint32_t  bcast, size_t nb, struct vc_ip_mask_pair const *ips);
136   
137   /* rlimit related functions */
138   typedef uint64_t      vc_limit_t;
139   
140   
141   struct vc_rlimit {
142       vc_limit_t min;
143       vc_limit_t soft;
144       vc_limit_t hard;      
145   };
146
147   struct  vc_rlimit_mask {
148       uint32_t min;
149       uint32_t soft;
150       uint32_t hard;
151   };
152
153   int   vc_get_rlimit(xid_t ctx, int resource, struct vc_rlimit *lim);
154   int   vc_set_rlimit(xid_t ctx, int resource, struct vc_rlimit const *lim);
155   int   vc_get_rlimit_mask(xid_t ctx, struct vc_rlimit_mask *lim);
156
157
158   /** sends a signal to a context/pid
159       Special values for pid are:
160       * -1   which means every process in ctx except the init-process
161       *  0   which means every process in ctx inclusive the init-process */
162   int   vc_ctx_kill(xid_t ctx, pid_t pid, int sig);
163
164
165
166   int           vc_set_iattr(dev_t dev, ino_t ino, xid_t xid,  uint32_t flags, uint32_t mask); 
167   int           vc_get_iattr(dev_t dev, ino_t ino, xid_t * /*@null@*/ xid,
168                              uint32_t * /*@null@*/ flags, uint32_t * /*@null@*/ mask);
169
170   int           vc_set_iattr_compat(char const *filename,
171                                     dev_t dev, ino_t ino, xid_t xid,
172                                     uint32_t flags, uint32_t mask,
173                                     mode_t const * /*@null@*/ mode);
174
175   int           vc_get_iattr_compat(char const *filename,
176                                     dev_t dev, ino_t ino,
177                                     xid_t    * /*@null@*/ xid,
178                                     uint32_t * /*@null@*/ flags,
179                                     uint32_t * /*@inout@*/ mask,
180                                     mode_t const * /*@null@*/ mode);
181
182   struct vc_vx_info {
183       xid_t     xid;
184       pid_t     initpid;
185   };
186   
187     /** Returns the context of the given process. pid==0 means the current process. */
188   xid_t         vc_get_task_xid(pid_t pid);
189   int           vc_get_vx_info(xid_t xid, struct vc_vx_info *info);
190
191
192   typedef enum { vcVHI_CONTEXT, vcVHI_SYSNAME, vcVHI_NODENAME,
193                  vcVHI_RELEASE, vcVHI_VERSION, vcVHI_MACHINE,
194                  vcVHI_DOMAINNAME }             vc_uts_type;
195   
196   int           vc_set_vhi_name(xid_t xid, vc_uts_type type, char const *val, size_t len);
197   int           vc_get_vhi_name(xid_t xid, vc_uts_type type, char *val, size_t len);
198
199   
200   int           vc_text2cap(char const *);
201   char const *  vc_cap2text(int);
202
203   inline static int     vc_get_securecaps() {
204     return ( (1<<VC_CAP_LINUX_IMMUTABLE) | (1<<VC_CAP_NET_BROADCAST) |
205              (1<<VC_CAP_NET_ADMIN) | (1<<VC_CAP_NET_RAW) |
206              (1<<VC_CAP_IPC_LOCK) | (1<<VC_CAP_IPC_OWNER) |
207              (1<<VC_CAP_SYS_MODULE) | (1<<VC_CAP_SYS_RAWIO) |
208              (1<<VC_CAP_SYS_PACCT) | (1<<VC_CAP_SYS_ADMIN) |
209              (1<<VC_CAP_SYS_BOOT) | (1<<VC_CAP_SYS_NICE) |
210              (1<<VC_CAP_SYS_RESOURCE) | (1<<VC_CAP_SYS_TIME) |
211              (1<<VC_CAP_MKNOD) | (1<<VC_CAP_QUOTACTL) );
212   }
213
214   inline static int             vc_setfilecontext(dev_t dev, ino_t ino, xid_t xid) {
215     return vc_set_iattr(dev, ino, xid, 0, VC_IATTR_XID);
216   }
217   
218   inline static xid_t           vc_getfilecontext(dev_t dev, ino_t ino) {
219     xid_t       res;
220     if (vc_get_iattr(dev, ino, &res, 0,0)==-1) return VC_NOCTX;
221     return res;
222   }
223   
224   
225   /* The management part */
226
227 #define VC_LIMIT_VSERVER_NAME_LEN       1024
228   
229   typedef enum { vcCFG_NONE, vcCFG_AUTO,
230                  vcCFG_LEGACY,
231                  vcCFG_RECENT_SHORT,
232                  vcCFG_RECENT_FULL }            vcCfgStyle;
233
234   vcCfgStyle    vc_getVserverCfgStyle(char const *id);
235   
236   /** Resolves the name of the vserver. The result will be allocated and must
237       be freed by the caller. */
238   char *        vc_getVserverName(char const *id, vcCfgStyle style);
239
240   /** Returns the path of the vserver configuration directory. When the given
241    *  vserver does not exist, or when it does not have such a directory, NULL
242    *  will be returned. Else, the result will be allocated and must be freed
243    *  by the caller. */
244   char *        vc_getVserverCfgDir(char const *id, vcCfgStyle style);
245
246   /** Returns the path of the configuration directory for the given
247    *  application. The result will be allocated and must be freed by the
248    *  caller. */
249   char *        vc_getVserverAppDir(char const *id, vcCfgStyle style, char const *app);
250
251   /** Returns the path to the vserver root-directory. The result will be
252    *  allocated and must be freed by the caller. */
253   char *        vc_getVserverVdir(char const *id, vcCfgStyle style, bool physical);
254
255   /** Returns the ctx of the given vserver. When vserver is not running and
256    *  'honor_static' is false, VC_NOCTX will be returned. Else, when
257    *  'honor_static' is true and a static assignment exists, those value will
258    *  be returned. Else, the result will be VC_NOCTX.
259    *
260    *  When 'is_running' is not null, the status of the vserver will be
261    *  assigned to this variable. */
262   xid_t         vc_getVserverCtx(char const *id, vcCfgStyle style,
263                                  bool honor_static, bool /*@null@*/ *is_running);
264
265   /** Resolves the cfg-path of the vserver owning the given ctx. 'revdir' will
266       be used as the directory holding the mapping-links; when NULL, the
267       default value will be assumed.  The result will be allocated and must be
268       freed by the caller. */
269   char *        vc_getVserverByCtx(xid_t ctx, /*@null@*/vcCfgStyle *style,
270                                    /*@null@*/char const *revdir);
271   
272 #ifdef __cplusplus
273 }
274 #endif
275
276 #endif