]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/cgroup.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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.
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.
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
32 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/inotify.h>
36 #include <netinet/in.h>
42 #define MTAB "/etc/mtab"
44 static int get_cgroup_mount(const char *mtab
, char *mnt
)
46 struct mntent
*mntent
;
50 file
= setmntent(mtab
, "r");
52 lxc_log_syserror("failed to open %s", mtab
);
56 while ((mntent
= getmntent(file
))) {
57 if (strcmp(mntent
->mnt_type
, "cgroup"))
59 strcpy(mnt
, mntent
->mnt_dir
);
69 int lxc_link_nsgroup(const char *name
, pid_t pid
)
72 char nsgroup
[MAXPATHLEN
];
73 char cgroup
[MAXPATHLEN
];
76 if (get_cgroup_mount(MTAB
, cgroup
)) {
77 lxc_log_info("cgroup is not mounted");
81 snprintf(lxc
, MAXPATHLEN
, LXCPATH
"/%s/nsgroup", name
);
82 snprintf(nsgroup
, MAXPATHLEN
, "%s/%d", cgroup
, pid
);
85 ret
= symlink(nsgroup
, lxc
);
87 lxc_log_syserror("failed to create symlink %s->%s",
92 int lxc_unlink_nsgroup(const char *name
)
94 char nsgroup
[MAXPATHLEN
];
96 snprintf(nsgroup
, MAXPATHLEN
, LXCPATH
"/%s/nsgroup", name
);
97 return unlink(nsgroup
);
100 int lxc_cgroup_set(const char *name
, const char *subsystem
, const char *value
)
102 int fd
, ret
= -LXC_ERROR_INTERNAL
;;
103 char path
[MAXPATHLEN
];
105 snprintf(path
, MAXPATHLEN
, LXCPATH
"/%s/nsgroup/%s", name
, subsystem
);
107 fd
= open(path
, O_WRONLY
);
111 if (write(fd
, value
, strlen(value
)) < 0)
120 int lxc_cgroup_get(const char *name
, const char *subsystem
,
121 char *value
, size_t len
)
123 int fd
, ret
= -LXC_ERROR_INTERNAL
;
124 char path
[MAXPATHLEN
];
126 snprintf(path
, MAXPATHLEN
, LXCPATH
"/%s/nsgroup/%s", name
, subsystem
);
128 fd
= open(path
, O_RDONLY
);
132 if (read(fd
, value
, len
) < 0)