chiark / gitweb /
split selinux label operations out of cgroup-util, socket-util
[elogind.git] / src / shared / cgroup-label.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <unistd.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <ftw.h>
31
32 #include "cgroup-util.h"
33 #include "log.h"
34 #include "set.h"
35 #include "macro.h"
36 #include "util.h"
37 #include "mkdir.h"
38
39 int cg_create(const char *controller, const char *path) {
40         char *fs;
41         int r;
42
43         assert(controller);
44         assert(path);
45
46         if ((r = cg_get_path(controller, path, NULL, &fs)) < 0)
47                 return r;
48
49         r = mkdir_parents(fs, 0755);
50
51         if (r >= 0) {
52                 if (mkdir(fs, 0755) >= 0)
53                         r = 1;
54                 else if (errno == EEXIST)
55                         r = 0;
56                 else
57                         r = -errno;
58         }
59
60         free(fs);
61
62         return r;
63 }
64
65 int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
66         int r, q;
67
68         assert(controller);
69         assert(path);
70         assert(pid >= 0);
71
72         if ((r = cg_create(controller, path)) < 0)
73                 return r;
74
75         if ((q = cg_attach(controller, path, pid)) < 0)
76                 return q;
77
78         /* This does not remove the cgroup on failure */
79
80         return r;
81 }