added 'physical' parameter to vc_getVserverVdir() which resolves the
[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 #ifdef __cplusplus
103 extern "C" {
104 #endif
105
106   struct vc_ip_mask_pair {
107     uint32_t    ip;
108     uint32_t    mask;
109   };
110
111     /** Returns version of the given API-category */
112   int   vc_get_version(int category);
113   
114     /** Puts current process into context <ctx>, removes the given caps and
115      *  sets flags.
116      *  Special values for ctx are
117      *  - VC_SAMECTX  which means the current context (just for changing caps and flags)
118      *  - VC_RANDCTX  which means the next free context; this value can be used by
119      *                ordinary users also
120      *  See http://vserver.13thfloor.at/Stuff/Logic.txt for details */
121   int   vc_new_s_context(xid_t ctx, unsigned int remove_cap, unsigned int flags);
122
123     /** Sets the ipv4root information.
124      *  \precondition: nb<16 */
125   int   vc_set_ipv4root(uint32_t  bcast, size_t nb, struct vc_ip_mask_pair const *ips);
126   
127   /* rlimit related functions */
128   typedef uint64_t      vc_limit_t;
129   
130   
131   struct vc_rlimit {
132       vc_limit_t min;
133       vc_limit_t soft;
134       vc_limit_t hard;      
135   };
136
137   struct  vc_rlimit_mask {
138       uint32_t min;
139       uint32_t soft;
140       uint32_t hard;
141   };
142
143   int   vc_get_rlimit(xid_t ctx, int resource, struct vc_rlimit *lim);
144   int   vc_set_rlimit(xid_t ctx, int resource, struct vc_rlimit const *lim);
145   int   vc_get_rlimit_mask(xid_t ctx, struct vc_rlimit_mask *lim);
146
147
148   /** sends a signal to a context/pid
149       Special values for pid are:
150       * -1   which means every process in ctx except the init-process
151       *  0   which means every process in ctx inclusive the init-process */
152   int   vc_ctx_kill(xid_t ctx, pid_t pid, int sig);
153
154
155     /** Returns the context of the given process. pid==0 means the current process. */
156   xid_t vc_X_getctx(pid_t pid);
157
158     /** Returns the pid of the 'init' process */
159   pid_t vc_X_getinitpid(pid_t pid);
160
161
162   xid_t         vc_X_get_filecontext(int fd);
163   int           vc_X_set_filecontext(int fd, xid_t ctx);
164
165   int           vc_X_get_ext2flags(int fd, long *flags);
166   int           vc_X_set_ext2flags(int fd, long set_flags, long del_flags);
167
168
169   int           vc_text2cap(char const *);
170   char const *  vc_cap2text(int);
171
172   inline static int     vc_get_securecaps() {
173     return ( (1<<VC_CAP_LINUX_IMMUTABLE) | (1<<VC_CAP_NET_BROADCAST) |
174              (1<<VC_CAP_NET_ADMIN) | (1<<VC_CAP_NET_RAW) |
175              (1<<VC_CAP_IPC_LOCK) | (1<<VC_CAP_IPC_OWNER) |
176              (1<<VC_CAP_SYS_MODULE) | (1<<VC_CAP_SYS_RAWIO) |
177              (1<<VC_CAP_SYS_PACCT) | (1<<VC_CAP_SYS_ADMIN) |
178              (1<<VC_CAP_SYS_BOOT) | (1<<VC_CAP_SYS_NICE) |
179              (1<<VC_CAP_SYS_RESOURCE) | (1<<VC_CAP_SYS_TIME) |
180              (1<<VC_CAP_MKNOD) | (1<<VC_CAP_QUOTACTL) );
181   }
182
183   
184   /* The management part */
185
186 #define VC_LIMIT_VSERVER_NAME_LEN       1024
187   
188   typedef enum { vcCFG_NONE, vcCFG_AUTO,
189                  vcCFG_LEGACY,
190                  vcCFG_RECENT_SHORT,
191                  vcCFG_RECENT_FULL }            vcCfgStyle;
192
193   vcCfgStyle    vc_getVserverCfgStyle(char const *id);
194   
195   /** Resolves the name of the vserver. The result will be allocated and must
196       be freed by the caller. */
197   char *        vc_getVserverName(char const *id, vcCfgStyle style);
198
199   /** Returns the path of the vserver configuration directory. When the given
200    *  vserver does not exist, or when it does not have such a directory, NULL
201    *  will be returned. Else, the result will be allocated and must be freed
202    *  by the caller. */
203   char *        vc_getVserverCfgDir(char const *id, vcCfgStyle style);
204
205   /** Returns the path of the configuration directory for the given
206    *  application. The result will be allocated and must be freed by the
207    *  caller. */
208   char *        vc_getVserverAppDir(char const *id, vcCfgStyle style, char const *app);
209
210   /** Returns the path to the vserver root-directory. The result will be
211    *  allocated and must be freed by the caller. */
212   char *        vc_getVserverVdir(char const *id, vcCfgStyle style, bool physical);
213
214   /** Returns the ctx of the given vserver. When vserver is not running and
215    *  'honor_static' is false, VC_NOCTX will be returned. Else, when
216    *  'honor_static' is true and a static assignment exists, those value will
217    *  be returned. Else, the result will be VC_NOCTX.
218    *
219    *  When 'is_running' is not null, the status of the vserver will be
220    *  assigned to this variable. */
221   xid_t         vc_getVserverCtx(char const *id, vcCfgStyle style,
222                                  bool honor_static, bool /*@null@*/ *is_running);
223
224   /** Resolves the cfg-path of the vserver owning the given ctx. 'revdir' will
225       be used as the directory holding the mapping-links; when NULL, the
226       default value will be assumed.  The result will be allocated and must be
227       freed by the caller. */
228   char *        vc_getVserverByCtx(xid_t ctx, /*@null@*/vcCfgStyle *style,
229                                    /*@null@*/char const *revdir);
230   
231 #ifdef __cplusplus
232 }
233 #endif
234
235 #endif