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