<commit_before>/*
 * lxc: linux Container library
 *
 * (C) Copyright IBM Corp. 2007, 2008
 *
 * Authors:
 * <NAME> <dlezcano at fr.ibm.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <stdlib.h>
#include <errno.h>
#include <mntent.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/inotify.h>
#include <netinet/in.h>
#include <net/if.h>

#include "error.h"
#include "config.h"

#include <lxc/log.h>
#include <lxc/cgroup.h>
#include <lxc/start.h>

lxc_log_define(lxc_cgroup, lxc);

#define MTAB "/etc/mtab"

static char nsgroup_path[MAXPATHLEN];

static int get_cgroup_mount(const char *mtab, char *mnt)
{
        struct mntent *mntent;
        FILE *file = NULL;
        int err = -1;

        file = setmntent(mtab, "r");
        if (!file) {
                SYSERROR("failed to open %s", mtab);
                goto out;
        }

        while ((mntent = getmntent(file))) {

		/* there is a cgroup mounted named "lxc" */
		if (!strcmp(mntent->mnt_fsname, "lxc") &&
		    !strcmp(mntent->mnt_type, "cgroup")) {
			strcpy(mnt, mntent->mnt_dir);
			err = 0;
			break;
		}

		/* fallback to the first non-lxc cgroup found */
                if (!strcmp(mntent->mnt_type, "cgroup") && err) {
			strcpy(mnt, mntent->mnt_dir);
			err = 0;
		}
        };

	DEBUG("using cgroup mounted at '%s'", mnt);

        fclose(file);
out:
        return err;
}

int lxc_rename_nsgroup(const char *name, pid_t pid)
{
	char oldname[MAXPATHLEN];
	char newname[MAXPATHLEN];
	char cgroup[MAXPATHLEN];
	int ret;

	if (get_cgroup_mount(MTAB, cgroup)) {
		ERROR("cgroup is not mounted");
		return -1;
	}

	snprintf(oldname, MAXPATHLEN, "%s/%d", cgroup, pid);
	snprintf(newname, MAXPATHLEN, "%s/%s", cgroup, name);

	/* there is a previous cgroup, assume it is empty, otherwise
	 * that fails */
	if (!access(newname, F_OK)) {
		ret = rmdir(newname);
		if (ret) {
			SYSERROR("failed to remove previous cgroup '%s'",
				 newname);
			return ret;
		}
	}

	ret = rename(oldname, newname);
	if (ret)
		SYSERROR("failed to rename cgroup %s->%s", oldname, newname);
	else
		DEBUG("'%s' renamed to '%s'", oldname, newname);


	return ret;
}

int lxc_unlink_nsgroup(const char *name)
{
	char nsgroup[MAXPATHLEN];
	char cgroup[MAXPATHLEN];
	int ret;

	if (get_cgroup_mount(MTAB, cgroup)) {
		ERROR("cgroup is not mounted");
		return -1;
	}

	snprintf(nsgroup, MAXPATHLEN, "%s/%s", cgroup, name);
	ret = rmdir(nsgroup);
	if (ret)
		SYSERROR("failed to remove cgroup '%s'", nsgroup);
	else
		DEBUG("'%s' unlinked", nsgroup);

	return ret;
}

int lxc_cgroup_create(const char *name, pid_t pid)
{
	return lxc_rename_nsgroup(name, pid);
}

int lxc_cgroup_destroy(const char *name)
{
	return lxc_unlink_nsgroup(name);
}

int lxc_cgroup_path_get(char **path, const char *name)
{
	char cgroup[MAXPATHLEN];

	*path = &nsgroup_path[0];

	/*
	 * report nsgroup_path string if already set
	 */
	if (**path != 0)
		return 0;

	if (get_cgroup_mount(MTAB, cgroup)) {
		ERROR("cgroup is not mounted");
		return -1;
	}

	snprintf(nsgroup_path, MAXPATHLEN, "%s/%s", cgroup, name);
	return 0;
}
<commit_msg>Move common code to lxc_cgroup_create<commit_after>/*
 * lxc: linux Container library
 *
 * (C) Copyright IBM Corp. 2007, 2008
 *
 * Authors:
 * <NAME> <dlezcano at fr.ibm.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <stdlib.h>
#include <errno.h>
#include <mntent.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/inotify.h>
#include <netinet/in.h>
#include <net/if.h>

#include "error.h"
#include "config.h"

#include <lxc/log.h>
#include <lxc/cgroup.h>
#include <lxc/start.h>

lxc_log_define(lxc_cgroup, lxc);

#define MTAB "/etc/mtab"

static char nsgroup_path[MAXPATHLEN];

static int get_cgroup_mount(const char *mtab, char *mnt)
{
        struct mntent *mntent;
        FILE *file = NULL;
        int err = -1;

        file = setmntent(mtab, "r");
        if (!file) {
                SYSERROR("failed to open %s", mtab);
                goto out;
        }

        while ((mntent = getmntent(file))) {

		/* there is a cgroup mounted named "lxc" */
		if (!strcmp(mntent->mnt_fsname, "lxc") &&
		    !strcmp(mntent->mnt_type, "cgroup")) {
			strcpy(mnt, mntent->mnt_dir);
			err = 0;
			break;
		}

		/* fallback to the first non-lxc cgroup found */
                if (!strcmp(mntent->mnt_type, "cgroup") && err) {
			strcpy(mnt, mntent->mnt_dir);
			err = 0;
		}
        };

	DEBUG("using cgroup mounted at '%s'", mnt);

        fclose(file);
out:
        return err;
}

int lxc_rename_nsgroup(const char *mnt, const char *name, pid_t pid)
{
	char oldname[MAXPATHLEN];

	snprintf(oldname, MAXPATHLEN, "%s/%d", mnt, pid);

	if (rename(oldname, name)) {
		SYSERROR("failed to rename cgroup %s->%s", oldname, name);
		return -1;
	}

	DEBUG("'%s' renamed to '%s'", oldname, name);

	return 0;
}

int lxc_cgroup_create(const char *name, pid_t pid)
{
	char cgmnt[MAXPATHLEN];
	char cgname[MAXPATHLEN];

	if (get_cgroup_mount(MTAB, cgmnt)) {
		ERROR("cgroup is not mounted");
		return -1;
	}

	snprintf(cgname, MAXPATHLEN, "%s/%s", cgmnt, name);

	/*
	 * There is a previous cgroup, assume it is empty,
	 * otherwise that fails
	 */
	if (!access(cgname, F_OK) && rmdir(cgname)) {
		SYSERROR("failed to remove previous cgroup '%s'", cgname);
		return -1;
	}

	return lxc_rename_nsgroup(cgmnt, cgname, pid);
}

int lxc_cgroup_destroy(const char *name)
{
	char cgmnt[MAXPATHLEN];
	char cgname[MAXPATHLEN];

	if (get_cgroup_mount(MTAB, cgmnt)) {
		ERROR("cgroup is not mounted");
		return -1;
	}

	snprintf(cgname, MAXPATHLEN, "%s/%s", cgmnt, name);
	if (rmdir(cgmnt)) {
		SYSERROR("failed to remove cgroup '%s'", cgname);
		return -1;
	}

	DEBUG("'%s' unlinked", cgname);

	return 0;
}

int lxc_cgroup_path_get(char **path, const char *name)
{
	char cgroup[MAXPATHLEN];

	*path = &nsgroup_path[0];

	/*
	 * report nsgroup_path string if already set
	 */
	if (**path != 0)
		return 0;

	if (get_cgroup_mount(MTAB, cgroup)) {
		ERROR("cgroup is not mounted");
		return -1;
	}

	snprintf(nsgroup_path, MAXPATHLEN, "%s/%s", cgroup, name);
	return 0;
}

{'fraction_non_alphanumeric': 0.13756956127863337, 'fraction_numerical': 0.009447392260903326, 'mean_word_length': 3.5458823529411765, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3465044', 'n_tokens_mistral': 3029, 'n_tokens_neox': 2849, 'n_words': 847}