]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/cgroup.c
Add return error status in the different functions
[mirror_lxc.git] / src / lxc / cgroup.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #undef _GNU_SOURCE
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <mntent.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/param.h>
35 #include <sys/inotify.h>
36 #include <netinet/in.h>
37 #include <net/if.h>
38
39 #include "error.h"
40 #include <lxc/lxc.h>
41
42 #define MTAB "/etc/mtab"
43
44 static int get_cgroup_mount(const char *mtab, char *mnt)
45 {
46 struct mntent *mntent;
47 FILE *file = NULL;
48 int err = -1;
49
50 file = setmntent(mtab, "r");
51 if (!file) {
52 lxc_log_syserror("failed to open %s", mtab);
53 goto out;
54 }
55
56 while ((mntent = getmntent(file))) {
57 if (strcmp(mntent->mnt_type, "cgroup"))
58 continue;
59 strcpy(mnt, mntent->mnt_dir);
60 err = 0;
61 break;
62 };
63
64 fclose(file);
65 out:
66 return err;
67 }
68
69 int lxc_link_nsgroup(const char *name, pid_t pid)
70 {
71 char lxc[MAXPATHLEN];
72 char nsgroup[MAXPATHLEN];
73 char cgroup[MAXPATHLEN];
74 int ret;
75
76 if (get_cgroup_mount(MTAB, cgroup)) {
77 lxc_log_info("cgroup is not mounted");
78 return -1;
79 }
80
81 snprintf(lxc, MAXPATHLEN, LXCPATH "/%s/nsgroup", name);
82 snprintf(nsgroup, MAXPATHLEN, "%s/%d", cgroup, pid);
83
84 unlink(lxc);
85 ret = symlink(nsgroup, lxc);
86 if (ret)
87 lxc_log_syserror("failed to create symlink %s->%s",
88 nsgroup, lxc);
89 return ret;
90 }
91
92 int lxc_unlink_nsgroup(const char *name)
93 {
94 char nsgroup[MAXPATHLEN];
95
96 snprintf(nsgroup, MAXPATHLEN, LXCPATH "/%s/nsgroup", name);
97 return unlink(nsgroup);
98 }
99
100 int lxc_cgroup_set(const char *name, const char *subsystem, const char *value)
101 {
102 int fd, ret = -LXC_ERROR_INTERNAL;;
103 char path[MAXPATHLEN];
104
105 snprintf(path, MAXPATHLEN, LXCPATH "/%s/nsgroup/%s", name, subsystem);
106
107 fd = open(path, O_WRONLY);
108 if (fd < 0)
109 return -1;
110
111 if (write(fd, value, strlen(value)) < 0)
112 goto out;
113
114 ret = 0;
115 out:
116 close(fd);
117 return ret;
118 }
119
120 int lxc_cgroup_get(const char *name, const char *subsystem,
121 char *value, size_t len)
122 {
123 int fd, ret = -LXC_ERROR_INTERNAL;
124 char path[MAXPATHLEN];
125
126 snprintf(path, MAXPATHLEN, LXCPATH "/%s/nsgroup/%s", name, subsystem);
127
128 fd = open(path, O_RDONLY);
129 if (fd < 0)
130 return -1;
131
132 if (read(fd, value, len) < 0)
133 goto out;
134
135 ret = 0;
136 out:
137 close(fd);
138 return ret;
139 }