lxc_cgroup.c revision 51cab6315f1500af5105cfbf56cc03b2ceefa0d9
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn/*
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * lxc: linux Container library
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn *
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * (C) Copyright IBM Corp. 2007, 2008
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn *
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * Authors:
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * Daniel Lezcano <dlezcano at fr.ibm.com>
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn *
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * This library is free software; you can redistribute it and/or
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * modify it under the terms of the GNU Lesser General Public
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * License as published by the Free Software Foundation; either
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * version 2.1 of the License, or (at your option) any later version.
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn *
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * This library is distributed in the hope that it will be useful,
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * but WITHOUT ANY WARRANTY; without even the implied warranty of
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * Lesser General Public License for more details.
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn *
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * You should have received a copy of the GNU Lesser General Public
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * License along with this library; if not, write to the Free Software
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn */
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn#include <stdio.h>
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn#include <unistd.h>
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn#include <libgen.h>
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn#include <sys/types.h>
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn#include <lxc/lxc.h>
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallynlxc_log_define(lxc_cgroup, lxc);
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallynvoid usage(char *cmd)
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn{
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn fprintf(stderr, "%s <subsystem> [value]\n", basename(cmd));
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn fprintf(stderr, "\t -n <name> : name of the container\n");
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn fprintf(stderr, "\t[-o <logfile>] : path of the log file\n");
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn fprintf(stderr, "\t[-l <logpriority>]: log level priority\n");
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn _exit(1);
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn}
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallynint main(int argc, char *argv[])
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn{
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn int opt;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn char *name = NULL, *subsystem = NULL, *value = NULL;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn const char *log_file = NULL, *log_priority = NULL;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn int nbargs = 0;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn while ((opt = getopt(argc, argv, "n:o:l:")) != -1) {
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn switch (opt) {
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn case 'n':
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn name = optarg;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn break;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn case 'o':
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn log_file = optarg;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn break;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn case 'l':
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn log_priority = optarg;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn break;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn }
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn nbargs++;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn }
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn if (!name || (argc-optind) < 1)
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn usage(argv[0]);
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn if (lxc_log_init(log_file, log_priority, basename(argv[0])))
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn return 1;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn if ((argc -optind) >= 1)
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn subsystem = argv[optind];
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn if ((argc -optind) >= 2)
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn value = argv[optind+1];
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn if (value) {
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn if (lxc_cgroup_set(name, subsystem, value)) {
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn ERROR("failed to assign '%s' value to '%s' for '%s'\n",
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn value, subsystem, name);
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn return 1;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn }
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn } else {
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn const unsigned long len = 4096;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn char buffer[len];
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn if (lxc_cgroup_get(name, subsystem, buffer, len)) {
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn ERROR("failed to retrieve value of '%s' for '%s'\n",
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn subsystem, name);
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn return 1;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn }
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn printf("%s", buffer);
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn }
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn return 0;
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn}
198b363fff1de9afcee2f26b9aa847316f589afeSerge Hallyn