]> git.proxmox.com Git - mirror_lxc.git/blame - src/liblxc/lxc_cgroup.c
Added C++ compatibility, change to libtool, improve monitoring
[mirror_lxc.git] / src / liblxc / lxc_cgroup.c
CommitLineData
620eff3c 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.h>
40
41#define MAXPRIOLEN 24
42#define MTAB "/etc/mtab"
43
44static 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_fsname, "cgroup"))
58 continue;
59 strcpy(mnt, mntent->mnt_dir);
60 err = 0;
61 break;
62 };
63
64 fclose(file);
65out:
66 return err;
67}
68
69int lxc_link_nsgroup(const char *name, pid_t pid)
70{
71 char *lxc, *nsgroup, cgroup[MAXPATHLEN];
72 int ret;
73
74 if (get_cgroup_mount(MTAB, cgroup)) {
75 lxc_log_info("cgroup is not mounted");
76 return -1;
77 }
78
79 asprintf(&lxc, LXCPATH "/%s/nsgroup", name);
80 asprintf(&nsgroup, "%s/%d", cgroup, pid);
81
82 unlink(lxc);
83 ret = symlink(nsgroup, lxc);
84 if (ret)
85 lxc_log_syserror("failed to create symlink %s->%s",
86 nsgroup, lxc);
87 free(lxc);
88 free(nsgroup);
89 return ret;
90}
91
92int lxc_unlink_nsgroup(const char *name)
93{
94 char *nsgroup;
95 int ret;
96
97 asprintf(&nsgroup, LXCPATH "/%s/nsgroup", name);
98 ret = unlink(nsgroup);
99 free(nsgroup);
100
101 return ret;
102}
103
c2cc9f0a 104int lxc_cgroup_set_priority(const char *name, int priority)
620eff3c 105{
106 int fd;
107 char *path = NULL, *prio = NULL;
108
109 asprintf(&path, LXCPATH "/%s/nsgroup/cpu.shares", name);
110
111 fd = open(path, O_WRONLY);
112 if (fd < 0) {
113 lxc_log_syserror("failed to open '%s'", path);
114 goto out;
115 }
116
117 asprintf(&prio, "%d", priority);
118
119 if (write(fd, prio, strlen(prio) + 1) < 0) {
120 lxc_log_syserror("failed to write to '%s'", path);
121 close(fd);
122 goto out;
123 }
124
125 close(fd);
126out:
127 free(path);
128 free(prio);
129 return 0;
130}
131
c2cc9f0a 132int lxc_cgroup_get_priority(const char *name, int *priority)
620eff3c 133{
134 int fd, ret = -1;
135 char *path, prio[MAXPRIOLEN];
136
137 asprintf(&path, LXCPATH "/%s/nsgroup/cpu.shares", name);
138
139 fd = open(path, O_RDONLY);
140 if (fd < 0) {
141 lxc_log_syserror("failed to open '%s'", path);
142 goto out;
143 }
144
145 if (read(fd, prio, MAXPRIOLEN) < 0) {
146 lxc_log_syserror("failed to read from '%s'", path);
147 close(fd);
148 goto out;
149 }
150
151 close(fd);
152 *priority = atoi(prio);
153
154 ret = 0;
155out:
156 free(path);
187d3a35 157 return ret;
620eff3c 158}
159
160int lxc_set_memory(const char *name, size_t memmax)
161{
162 return 0;
163}
164
165int lxc_get_memory(const char *name, size_t *memmax)
166{
167 return 0;
168}
169
170int lxc_get_memstat(const char *name, struct lxc_mem_stat *memstat)
171{
172 return 0;
173}
174
175int lxc_set_cpuset(const char *name, long *cpumask, int len, int shared)
176{
177 return 0;
178}
179
180int lxc_get_cpuset(const char *name, long *cpumask, int len, int *shared)
181{
182 return 0;
183}
184
185int lxc_get_cpu_usage(const char *name, long long *usage)
186{
187 return 0;
188}