~ubuntu-branches/ubuntu/maverick/libcgroup/maverick-proposed

« back to all changes in this revision

Viewing changes to include/libcgroup.h

  • Committer: Bazaar Package Importer
  • Author(s): Dustin Kirkland
  • Date: 2009-08-26 11:29:17 UTC
  • Revision ID: james.westby@ubuntu.com-20090826112917-402ews2uj6v350d2
Tags: upstream-0.34
ImportĀ upstreamĀ versionĀ 0.34

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright IBM Corporation. 2007
 
3
 *
 
4
 * Author:      Balbir Singh <balbir@linux.vnet.ibm.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify it
 
7
 * under the terms of version 2.1 of the GNU Lesser General Public License
 
8
 * as published by the Free Software Foundation.
 
9
 *
 
10
 * This program is distributed in the hope that it would be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
13
 *
 
14
 */
 
15
 
 
16
#ifndef _LIBCG_H
 
17
#define _LIBCG_H
 
18
 
 
19
#include <features.h>
 
20
 
 
21
__BEGIN_DECLS
 
22
 
 
23
#include <grp.h>
 
24
#include <linux/types.h>
 
25
#include <stdbool.h>
 
26
#include <stdio.h>
 
27
#include <sys/stat.h>
 
28
#include <sys/types.h>
 
29
#include <unistd.h>
 
30
#include <limits.h>
 
31
#include <linux/cn_proc.h>
 
32
 
 
33
#ifndef _GNU_SOURCE
 
34
#define _GNU_SOURCE
 
35
#endif
 
36
 
 
37
#ifndef __USE_GNU
 
38
#define __USE_GNU
 
39
#endif
 
40
 
 
41
/* Maximum number of mount points/controllers */
 
42
#define MAX_MNT_ELEMENTS        8
 
43
/* Estimated number of groups created */
 
44
#define MAX_GROUP_ELEMENTS      128
 
45
 
 
46
/*
 
47
 * NOTE: Wide characters are not supported at the moment. Wide character support
 
48
 * would require us to use a scanner/parser that can parse beyond ASCII
 
49
 */
 
50
 
 
51
/* Definitions for the uid and gid members of a cgroup_rules */
 
52
#define CGRULE_INVALID (-1)
 
53
#define CGRULE_WILD (-2)
 
54
 
 
55
#define CGRULE_SUCCESS_STORE_PID        "SUCCESS_STORE_PID"
 
56
 
 
57
/* Flags for cgroup_change_cgroup_uid_gid() */
 
58
enum cgflags {
 
59
        CGFLAG_USECACHE = 0x01,
 
60
};
 
61
 
 
62
/**
 
63
 * per thread errno variable, to be used when return code is ECGOTHER
 
64
 */
 
65
extern __thread int last_errno;
 
66
 
 
67
enum cgroup_errors {
 
68
        ECGROUPNOTCOMPILED=50000,
 
69
        ECGROUPNOTMOUNTED,
 
70
        ECGROUPNOTEXIST,
 
71
        ECGROUPNOTCREATED,
 
72
        ECGROUPSUBSYSNOTMOUNTED,
 
73
        ECGROUPNOTOWNER,
 
74
        ECGROUPMULTIMOUNTED,/* Controllers bound to different mount points */
 
75
        ECGROUPNOTALLOWED,  /* This is the stock error. Default error. */
 
76
        ECGMAXVALUESEXCEEDED,
 
77
        ECGCONTROLLEREXISTS,
 
78
        ECGVALUEEXISTS,
 
79
        ECGINVAL,
 
80
        ECGCONTROLLERCREATEFAILED,
 
81
        ECGFAIL,
 
82
        ECGROUPNOTINITIALIZED,
 
83
        ECGROUPVALUENOTEXIST,
 
84
        /* Represents error coming from other libraries like glibc. libcgroup
 
85
         * users need to check errno upon encoutering ECGOTHER.
 
86
         */
 
87
        ECGOTHER,       /* OS error, see errno */
 
88
        ECGROUPNOTEQUAL,
 
89
        ECGCONTROLLERNOTEQUAL,
 
90
        ECGROUPPARSEFAIL, /* Failed to parse rules configuration file. */
 
91
        ECGROUPNORULES, /* Rules list does not exist. */
 
92
        ECGMOUNTFAIL,
 
93
        ECGSENTINEL,    /* Please insert further error codes above this */
 
94
        ECGEOF,         /* End of file, iterator */
 
95
};
 
96
 
 
97
/*
 
98
 * Don't use CGROUP_WALK_TYPE_FILE right now. It is added here for
 
99
 * later refactoring and better implementation. Most users *should*
 
100
 * use CGROUP_WALK_TYPE_PRE_DIR.
 
101
 */
 
102
enum cgroup_walk_type {
 
103
        CGROUP_WALK_TYPE_PRE_DIR = 0x1, /* Pre Order Directory */
 
104
        CGROUP_WALK_TYPE_POST_DIR = 0x2,        /* Post Order Directory */
 
105
};
 
106
 
 
107
enum cgroup_file_type {
 
108
        CGROUP_FILE_TYPE_FILE,          /* File */
 
109
        CGROUP_FILE_TYPE_DIR,           /* Directory */
 
110
        CGROUP_FILE_TYPE_OTHER,         /* Directory */
 
111
};
 
112
 
 
113
enum cgroup_daemon_type {
 
114
        CGROUP_DAEMON_UNCHANGE_CHILDREN = 0x1,
 
115
};
 
116
 
 
117
struct cgroup_file_info {
 
118
        enum cgroup_file_type type;
 
119
        const char *path;
 
120
        const char *parent;
 
121
        const char *full_path;
 
122
        short depth;
 
123
};
 
124
 
 
125
#define CG_NV_MAX 100
 
126
#define CG_CONTROLLER_MAX 100
 
127
#define CG_VALUE_MAX 100
 
128
/* Max number of mounted hierarchies. Event if one controller is mounted per
 
129
 * hier, it can not exceed CG_CONTROLLER_MAX
 
130
 */
 
131
#define CG_HIER_MAX  CG_CONTROLLER_MAX
 
132
 
 
133
struct cgroup_stat {
 
134
        char name[FILENAME_MAX];
 
135
        char value[CG_VALUE_MAX];
 
136
};
 
137
 
 
138
struct cgroup_mount_point {
 
139
        char name[FILENAME_MAX];
 
140
        char path[FILENAME_MAX];
 
141
};
 
142
 
 
143
/* Functions and structures that can be used by the application*/
 
144
struct cgroup;
 
145
struct cgroup_controller;
 
146
 
 
147
int cgroup_init(void);
 
148
int cgroup_attach_task(struct cgroup *cgroup);
 
149
int cgroup_modify_cgroup(struct cgroup *cgroup);
 
150
int cgroup_create_cgroup(struct cgroup *cgroup, int ignore_ownership);
 
151
int cgroup_delete_cgroup(struct cgroup *cgroup, int ignore_migration);
 
152
int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid);
 
153
int cgroup_get_cgroup(struct cgroup *cgroup);
 
154
int cgroup_create_cgroup_from_parent(struct cgroup *cgroup, int ignore_ownership);
 
155
int cgroup_copy_cgroup(struct cgroup *dst, struct cgroup *src);
 
156
 
 
157
/**
 
158
 * Changes the cgroup of a program based on the rules in the config file.
 
159
 * If a rule exists for the given UID, GID or PROCESS NAME, then the given
 
160
 * PID is placed into the correct group.  By default, this function parses
 
161
 * the configuration file each time it is called.
 
162
 *
 
163
 * The flags can alter the behavior of this function:
 
164
 *      CGFLAG_USECACHE: Use cached rules instead of parsing the config file
 
165
 *
 
166
 * This function may NOT be thread safe.
 
167
 *      @param uid The UID to match
 
168
 *      @param gid The GID to match
 
169
 *      @param procname The PROCESS NAME to match
 
170
 *      @param pid The PID of the process to move
 
171
 *      @param flags Bit flags to change the behavior, as defined above
 
172
 *      @return 0 on success, > 0 on error
 
173
 * TODO: Determine thread-safeness and fix of not safe.
 
174
 */
 
175
int cgroup_change_cgroup_flags(const uid_t uid, const gid_t gid,
 
176
                char *procname, const pid_t pid, const int flags);
 
177
 
 
178
/**
 
179
 * Changes the cgroup of a program based on the rules in the config file.  If a
 
180
 * rule exists for the given UID or GID, then the given PID is placed into the
 
181
 * correct group.  By default, this function parses the configuration file each
 
182
 * time it is called.
 
183
 * 
 
184
 * The flags can alter the behavior of this function:
 
185
 *      CGFLAG_USECACHE: Use cached rules instead of parsing the config file
 
186
 * 
 
187
 * This function may NOT be thread safe.
 
188
 *      @param uid The UID to match
 
189
 *      @param gid The GID to match
 
190
 *      @param pid The PID of the process to move
 
191
 *      @param flags Bit flags to change the behavior, as defined above
 
192
 *      @return 0 on success, > 0 on error
 
193
 * TODO: Determine thread-safeness and fix if not safe.
 
194
 */
 
195
int cgroup_change_cgroup_uid_gid_flags(const uid_t uid, const gid_t gid,
 
196
                                const pid_t pid, const int flags);
 
197
 
 
198
/**
 
199
 * Provides backwards-compatibility with older versions of the API.  This
 
200
 * function is deprecated, and cgroup_change_cgroup_uid_gid_flags() should be
 
201
 * used instead.  In fact, this function simply calls the newer one with flags
 
202
 * set to 0 (none).
 
203
 *      @param uid The UID to match
 
204
 *      @param gid The GID to match
 
205
 *      @param pid The PID of the process to move
 
206
 *      @return 0 on success, > 0 on error
 
207
 * 
 
208
 */
 
209
int cgroup_change_cgroup_uid_gid(uid_t uid, gid_t gid, pid_t pid);
 
210
 
 
211
/**
 
212
 * Changes the cgroup of a program based on the path provided.  In this case,
 
213
 * the user must already know into which cgroup the task should be placed and
 
214
 * no rules will be parsed.
 
215
 *
 
216
 *  returns 0 on success.
 
217
 */
 
218
int cgroup_change_cgroup_path(char *path, pid_t pid, char *controllers[]);
 
219
 
 
220
/**
 
221
 * Print the cached rules table.  This function should be called only after
 
222
 * first calling cgroup_parse_config(), but it will work with an empty rule
 
223
 * list.
 
224
 *      @param fp The file stream to print to
 
225
 */
 
226
void cgroup_print_rules_config(FILE *fp);
 
227
 
 
228
/**
 
229
 * Reloads the rules list, using the given configuration file.  This function
 
230
 * is probably NOT thread safe (calls cgroup_parse_rules_config()).
 
231
 *      @return 0 on success, > 0 on failure
 
232
 */
 
233
int cgroup_reload_cached_rules(void);
 
234
 
 
235
/**
 
236
 * Initializes the rules cache.
 
237
 *      @return 0 on success, > 0 on failure
 
238
 */
 
239
int cgroup_init_rules_cache(void);
 
240
 
 
241
/**
 
242
 * Get the current cgroup path where the task specified by pid_t pid
 
243
 * has been classified
 
244
 */
 
245
int cgroup_get_current_controller_path(pid_t pid, const char *controller,
 
246
                                        char **current_path);
 
247
/**
 
248
 * Return error corresponding to @code in human readable format.
 
249
 * @code: error code for which the corresponding error string is to be
 
250
 * returned
 
251
 */
 
252
char *cgroup_strerror(int code);
 
253
 
 
254
/**
 
255
 * Return last errno, which caused ECGOTHER error.
 
256
 */
 
257
int cgroup_get_last_errno();
 
258
 
 
259
/**
 
260
 * Walk through the directory tree for the specified controller.
 
261
 * @controller: Name of the controller, for which we want to walk
 
262
 * the directory tree
 
263
 * @base_path: Begin walking from this path
 
264
 * @depth: The maximum depth to which the function should walk, 0
 
265
 * implies all the way down
 
266
 * @handle: Handle to be used during iteration
 
267
 * @info: info filled and returned about directory information
 
268
 */
 
269
int cgroup_walk_tree_begin(char *controller, char *base_path, const int depth,
 
270
                                void **handle, struct cgroup_file_info *info,
 
271
                                int *base_level);
 
272
/**
 
273
 * Get the next element during the walk
 
274
 * @depth: The maximum depth to which the function should walk, 0
 
275
 * implies all the way down
 
276
 * @handle: Handle to be used during iteration
 
277
 * @info: info filled and returned about directory information
 
278
 *
 
279
 * Returns ECGEOF when we are done walking through the nodes.
 
280
 */
 
281
int cgroup_walk_tree_next(const int depth, void **handle,
 
282
                                struct cgroup_file_info *info, int base_level);
 
283
int cgroup_walk_tree_end(void **handle);
 
284
 
 
285
/**
 
286
 * This API is used to set the flags for walk_tree API. Currently availble
 
287
 *  flags are
 
288
 *
 
289
 *  CGROUP_WALK_TYPE_PRE_DIR
 
290
 *  CGROUP_WALK_TYPE_POST_DIR
 
291
 *
 
292
 */
 
293
int cgroup_walk_tree_set_flags(void **handle, int flags);
 
294
 
 
295
/**
 
296
 * Read the statistics values for the specified controller
 
297
 * @controller: Name of the controller for which stats are requested.
 
298
 * @path: cgroup path.
 
299
 * @handle: Handle to be used during iteration.
 
300
 * @stat: Stats values will be filled and returned here.
 
301
 */
 
302
int cgroup_read_stats_begin(char *controller, char *path, void **handle,
 
303
                                struct cgroup_stat *stat);
 
304
 
 
305
/**
 
306
 * Read the next stat value.
 
307
 * @handle: Handle to be used during iteration.
 
308
 * @stat: Stats values will be filled and returned here.
 
309
 */
 
310
int cgroup_read_stats_next(void **handle, struct cgroup_stat *stat);
 
311
 
 
312
int cgroup_read_stats_end(void **handle);
 
313
 
 
314
/**
 
315
 * Read the tasks file to get the list of tasks in a cgroup
 
316
 * @cgroup: Name of the cgroup
 
317
 * @controller: Name of the cgroup subsystem
 
318
 * @handle: Handle to be used in the iteration
 
319
 * @pid: The pid read from the tasks file. Will be filled in by the API
 
320
 */
 
321
int cgroup_get_task_begin(char *cgroup, char *controller, void **handle,
 
322
                                                                pid_t *pid);
 
323
 
 
324
/**
 
325
 * Register the unchanged process to a cgrulesengd daemon.
 
326
 * If the daemon does not work, this function returns 0 as success.
 
327
 * @param pid: The process id
 
328
 * @param flags Bit flags to change the behavior, as defined above
 
329
 * @return 0 on success, > 0 on error.
 
330
 */
 
331
int cgroup_register_unchanged_process(pid_t pid, int flags);
 
332
 
 
333
/**
 
334
 * Read the next task value
 
335
 * @handle: The handle used for iterating
 
336
 * @pid: The variable where the value will be stored
 
337
 *
 
338
 * return ECGEOF when the iterator finishes getting the list of tasks.
 
339
 */
 
340
int cgroup_get_task_next(void **handle, pid_t *pid);
 
341
int cgroup_get_task_end(void **handle);
 
342
 
 
343
/**
 
344
 * Read the mount table to give a list where each controller is
 
345
 * mounted
 
346
 * @handle: Handle to be used for iteration.
 
347
 * @name: The variable where the name is stored. Should be freed by caller.
 
348
 * @path: Te variable where the path to the controller is stored. Should be
 
349
 * freed by the caller.
 
350
 */
 
351
int cgroup_get_controller_begin(void **handle, struct cgroup_mount_point *info);
 
352
/*
 
353
 * While walking through the mount table, the controllers will be
 
354
 * returned in order of their mount points.
 
355
 */
 
356
int cgroup_get_controller_next(void **handle, struct cgroup_mount_point *info);
 
357
int cgroup_get_controller_end(void **handle);
 
358
 
 
359
/*
 
360
 * Reads the mount to table to give the mount point of a controller
 
361
 * @controller: Name of the controller
 
362
 * @mount_point: The string where the mount point is stored. Please note,
 
363
 * the caller must free mount_point.
 
364
 */
 
365
int cgroup_get_subsys_mount_point(char *controller, char **mount_point);
 
366
 
 
367
/* The wrappers for filling libcg structures */
 
368
 
 
369
struct cgroup *cgroup_new_cgroup(const char *name);
 
370
struct cgroup_controller *cgroup_add_controller(struct cgroup *cgroup,
 
371
                                                const char *name);
 
372
void cgroup_free(struct cgroup **cgroup);
 
373
void cgroup_free_controllers(struct cgroup *cgroup);
 
374
int cgroup_add_value_string(struct cgroup_controller *controller,
 
375
                                const char *name, const char *value);
 
376
int cgroup_add_value_int64(struct cgroup_controller *controller,
 
377
                                const char *name, int64_t value);
 
378
int cgroup_add_value_uint64(struct cgroup_controller *controller,
 
379
                                const char *name, u_int64_t value);
 
380
int cgroup_add_value_bool(struct cgroup_controller *controller,
 
381
                                const char *name, bool value);
 
382
int cgroup_compare_cgroup(struct cgroup *cgroup_a, struct cgroup *cgroup_b);
 
383
int cgroup_compare_controllers(struct cgroup_controller *cgca,
 
384
                                        struct cgroup_controller *cgcb);
 
385
int cgroup_set_uid_gid(struct cgroup *cgroup, uid_t tasks_uid, gid_t tasks_gid,
 
386
                                        uid_t control_uid, gid_t control_gid);
 
387
int cgroup_get_uid_gid(struct cgroup *cgroup, uid_t *tasks_uid,
 
388
                gid_t *tasks_gid, uid_t *control_uid, gid_t *control_gid);
 
389
int cgroup_get_value_string(struct cgroup_controller *controller,
 
390
                                        const char *name, char **value);
 
391
int cgroup_set_value_string(struct cgroup_controller *controller,
 
392
                                        const char *name, const char *value);
 
393
int cgroup_get_value_int64(struct cgroup_controller *controller,
 
394
                                        const char *name, int64_t *value);
 
395
int cgroup_set_value_int64(struct cgroup_controller *controller,
 
396
                                        const char *name, int64_t value);
 
397
int cgroup_get_value_uint64(struct cgroup_controller *controller,
 
398
                                        const char *name, u_int64_t *value);
 
399
int cgroup_set_value_uint64(struct cgroup_controller *controller,
 
400
                                        const char *name, u_int64_t value);
 
401
int cgroup_get_value_bool(struct cgroup_controller *controller,
 
402
                                                const char *name, bool *value);
 
403
int cgroup_set_value_bool(struct cgroup_controller *controller,
 
404
                                                const char *name, bool value);
 
405
struct cgroup_controller *cgroup_get_controller(struct cgroup *cgroup,
 
406
                                                        const char *name);
 
407
/*
 
408
 * Config related stuff
 
409
 */
 
410
int cgroup_config_load_config(const char *pathname);
 
411
int cgroup_unload_cgroups(void);
 
412
 
 
413
__END_DECLS
 
414
 
 
415
#endif /* _LIBCG_H  */