cgroup-label.c revision a32360f1a5a85c12f00e9dfb7353280067cccb5b
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/***
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen This file is part of systemd.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright 2010 Lennart Poettering
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is free software; you can redistribute it and/or modify it
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen under the terms of the GNU Lesser General Public License as published by
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen (at your option) any later version.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is distributed in the hope that it will be useful, but
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Lesser General Public License for more details.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen You should have received a copy of the GNU Lesser General Public License
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen***/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include <errno.h>
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen#include <unistd.h>
be077570f779664ed87b50f60608df9fbe258821Tom Gundersen#include <signal.h>
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen#include <string.h>
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#include <stdlib.h>
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen#include <dirent.h>
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include <sys/stat.h>
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen#include <sys/types.h>
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen#include <ftw.h>
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen#include "cgroup-util.h"
4dc355680460fdc8e0d590d8572dff1b6a257d88Tom Gundersen#include "log.h"
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen#include "set.h"
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen#include "macro.h"
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen#include "util.h"
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen#include "mkdir.h"
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersenint cg_create(const char *controller, const char *path, const char *suffix) {
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen _cleanup_free_ char *fs = NULL;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen int r;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert(controller);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen assert(path);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = cg_get_path_and_check(controller, path, suffix, &fs);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen r = mkdir_parents_label(fs, 0755);
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen if (r < 0)
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen return r;
2dead8129f7b6fe644e17e1dc1739bebacfe1364Tom Gundersen
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen if (mkdir(fs, 0755) < 0) {
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen if (errno == EEXIST)
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen return 0;
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen return -errno;
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen }
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen return 1;
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen}
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenint cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen int r, q;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert(controller);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert(path);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert(pid >= 0);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = cg_create(controller, path, NULL);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (r < 0)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return r;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen q = cg_attach(controller, path, pid);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen if (q < 0)
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return q;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen /* This does not remove the cgroup on failure */
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen return r;
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen}
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen