]>
Commit | Line | Data |
---|---|---|
576f946d | 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 <lxc/lxc.h> | |
40 | ||
41 | #define MTAB "/etc/mtab" | |
42 | ||
43 | static int get_cgroup_mount(const char *mtab, char *mnt) | |
44 | { | |
45 | struct mntent *mntent; | |
46 | FILE *file = NULL; | |
47 | int err = -1; | |
48 | ||
49 | file = setmntent(mtab, "r"); | |
50 | if (!file) { | |
51 | lxc_log_syserror("failed to open %s", mtab); | |
52 | goto out; | |
53 | } | |
54 | ||
55 | while ((mntent = getmntent(file))) { | |
56 | if (strcmp(mntent->mnt_type, "cgroup")) | |
57 | continue; | |
58 | strcpy(mnt, mntent->mnt_dir); | |
59 | err = 0; | |
60 | break; | |
61 | }; | |
62 | ||
63 | fclose(file); | |
64 | out: | |
65 | return err; | |
66 | } | |
67 | ||
68 | int lxc_link_nsgroup(const char *name, pid_t pid) | |
69 | { | |
70 | char lxc[MAXPATHLEN]; | |
71 | char nsgroup[MAXPATHLEN]; | |
72 | char cgroup[MAXPATHLEN]; | |
73 | int ret; | |
74 | ||
75 | if (get_cgroup_mount(MTAB, cgroup)) { | |
76 | lxc_log_info("cgroup is not mounted"); | |
77 | return -1; | |
78 | } | |
79 | ||
80 | snprintf(lxc, MAXPATHLEN, LXCPATH "/%s/nsgroup", name); | |
81 | snprintf(nsgroup, MAXPATHLEN, "%s/%d", cgroup, pid); | |
82 | ||
83 | unlink(lxc); | |
84 | ret = symlink(nsgroup, lxc); | |
85 | if (ret) | |
86 | lxc_log_syserror("failed to create symlink %s->%s", | |
87 | nsgroup, lxc); | |
88 | return ret; | |
89 | } | |
90 | ||
91 | int lxc_unlink_nsgroup(const char *name) | |
92 | { | |
93 | char nsgroup[MAXPATHLEN]; | |
94 | ||
95 | snprintf(nsgroup, MAXPATHLEN, LXCPATH "/%s/nsgroup", name); | |
96 | return unlink(nsgroup); | |
97 | } | |
98 | ||
576f946d | 99 | int lxc_cgroup_set(const char *name, const char *subsystem, const char *value) |
100 | { | |
101 | int fd, ret = -1;; | |
102 | char path[MAXPATHLEN]; | |
103 | ||
104 | snprintf(path, MAXPATHLEN, LXCPATH "/%s/nsgroup/%s", name, subsystem); | |
105 | ||
106 | fd = open(path, O_WRONLY); | |
107 | if (fd < 0) | |
108 | return -1; | |
109 | ||
110 | if (write(fd, value, strlen(value)) < 0) | |
111 | goto out; | |
112 | ||
113 | ret = 0; | |
114 | out: | |
115 | close(fd); | |
116 | return ret; | |
117 | } | |
118 | ||
119 | int lxc_cgroup_get(const char *name, const char *subsystem, | |
120 | char *value, size_t len) | |
121 | { | |
122 | int fd, ret = -1;; | |
123 | char path[MAXPATHLEN]; | |
124 | ||
125 | snprintf(path, MAXPATHLEN, LXCPATH "/%s/nsgroup/%s", name, subsystem); | |
126 | ||
127 | fd = open(path, O_RDONLY); | |
128 | if (fd < 0) | |
129 | return -1; | |
130 | ||
131 | if (read(fd, value, len) < 0) | |
132 | goto out; | |
133 | ||
134 | ret = 0; | |
135 | out: | |
136 | close(fd); | |
137 | return ret; | |
138 | } |