Date: Mon, 19 Jun 2006 06:32:42 GMT From: Alex Lyashkov <als@FreeBSD.org> To: Perforce Change Reviews <perforce@FreeBSD.org> Subject: PERFORCE change 99584 for review Message-ID: <200606190632.k5J6WgMZ021665@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=99584 Change 99584 by als@als_head on 2006/06/19 06:32:33 extend jail stats. reformat soruce to style(9) compatible. Affected files ... .. //depot/projects/jail2/j2_tools/jctl/jctl.c#2 edit Differences ... ==== //depot/projects/jail2/j2_tools/jctl/jctl.c#2 (text+ko) ==== @@ -7,14 +7,17 @@ #include <sys/jail2.h> #include <sys/jail2_flags.h> +#include <ctype.h> +#include <err.h> +#include <errno.h> +#include <getopt.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <sysexits.h> #include <unistd.h> -#include <ctype.h> -#include <string.h> -#include <getopt.h> -#include <err.h> -#include <errno.h> + + static void usage(void); @@ -28,244 +31,247 @@ #define C_FLAGS JAIL2_COMMAND(J_COMMON,J_SETFLAGS) #define C_EXEC 1 -char default_exec[50] = "/bin/csh"; -char csh_login[50]="/bin/csh -l"; -char *login_shell[] = { csh_login, NULL }; +char default_exec[50] = "/bin/csh"; +char csh_login[50] = "/bin/csh -l"; +char *login_shell[] = {csh_login, NULL}; #define J_FLAGS 4 struct flag { - const char *name; - uint32_t mask; + const char *name; + uint32_t mask; } flags [J_FLAGS] = { - {"START", J_START_FL}, - {"PROCLIMIT", J_PROCLIMIT_FL}, - {"IPC", J_SYSVIPC_ALLOW_FL }, - {NULL, 0} + {"START", J_START_FL}, + {"PROCLIMIT", J_PROCLIMIT_FL}, + {"IPC", J_SYSVIPC_ALLOW_FL}, + {NULL, 0} }; -static int +static int jail_exec(struct jail_2 *j, char *exec, char **arg) { - int ret; - + int ret; + j->cmd = C_ENTER; ret = jail(j); - if( ret != 0 ) + if (ret != 0) return ret; - - return execvp(exec, arg); + + return execvp(exec, arg); } static void jail_stats_v1(struct jail2_stats_v1 *jst) { - uint32_t i; - - printf("flags\t\t%x ( ",jst->flags); - for(i=0;i<J_FLAGS;i++) - if ((jst->flags & flags[i].mask)!=0) - printf("%s ", flags[i].name); - printf(")\n"); - printf("Task count\t%d\n",jst->taskcount); - printf("CPU limits\t%u:%u\n",jst->cpulow, jst->cpuhigh); - printf("CPU usage\t%u\n",jst->cpuusage); - printf("Jail SYSV IPC\n"); - printf("\t\tallocate limit\n"); - printf("Message queue %u %u\n", jst->msq_count, jst->msq_limit); - printf("Semaphore %u %u\n", jst->sem_count, jst->sem_limit); - printf("Shared segments %u %u\n", jst->shm_count, jst->shm_limit); - printf("Jail disk usage\n"); - printf(" "); + uint32_t i; + + printf("flags\t\t%x ( ", jst->flags); + for (i = 0; i < J_FLAGS; i++) + if ((jst->flags & flags[i].mask) != 0) + printf("%s ", flags[i].name); + printf(")\n"); + printf("Task count\t%d\n", jst->taskcount); + printf("CPU limits\t%u:%u\n", jst->cpulow, jst->cpuhigh); + printf("CPU usage\t%u\n", jst->cpuusage); + printf("\t\tallocate limit\n"); + printf("Task count %u \n", jst->taskcount); + printf("Message queue %u %u\n", jst->msq_count, jst->msq_limit); + printf("Semaphore %u %u\n", jst->sem_count, jst->sem_limit); + printf("Shared segments %u %u\n", jst->shm_count, jst->shm_limit); + printf("Disk space usage %u %u\n", jst->diskblocks, jst->disklimit); + printf("Inode usage %u %u\n", jst->inodecount, jst->inodelimit); + printf("filehandle usage %u %u\n", jst->fdcount, jst->fdlimit); + printf(" \n"); } static int jail_stats(struct jail_2 *j) { - struct jail2_stats *jst; - int ret = 0; + struct jail2_stats *jst; + int ret = 0; + + jst = malloc(sizeof(struct jail2_stats)); + if (jst == NULL) + return (ENOMEM); + j->data = jst; - - jst = malloc(sizeof(struct jail2_stats)); - if (jst == NULL) - return (ENOMEM); - j->data = jst; - - ret = jail(j); - if (ret != 0) - goto exit; + ret = jail(j); + if (ret != 0) + goto exit; - printf("Jail\t\t%d\n",jst->id); - switch(jst->version) { - case 1: - jail_stats_v1(&jst->v1); - break; - default: - ret = ENOTSUP; - break; - } - + printf("Jail\t\t%d\n", jst->id); + switch (jst->version) { + case 1: + jail_stats_v1(&jst->v1); + break; + default: + ret = ENOTSUP; + break; + } exit: - free(jst); - j->data = NULL; - return ret; + free(jst); + j->data = NULL; + return ret; } -enum mask_op { m_nop, m_add, m_clear, m_xor}; +enum mask_op { + M_NOP, + M_ADD, + M_CLEAR, + M_XOR +}; struct m_ops { - char str; - enum mask_op op; + char str; + enum mask_op op; } mask_ops [4] = { - {'+', m_add}, - {'-', m_clear}, - {'^', m_xor}, - {'\0',m_nop} + {'+', M_ADD}, + {'-', M_CLEAR}, + {'^', M_XOR}, + {'\0', M_NOP} }; static void skip_space(char **ostr) { - char *str = *ostr; - - while(isspace(*str)) { - if(*str == '\0') - break; - str++; - } - *ostr = str; + char *str = *ostr; + + while (isspace(*str)) { + if (*str == '\0') + break; + str++; + } + *ostr = str; } static int -parse_flag(char **oflag, uint32_t *omask) +parse_flag(char **oflag, uint32_t * omask) { - uint32_t i; - int ret = -1; - enum mask_op op = m_nop; - char *flag = *oflag; - char *end_word; - uint32_t mask = 0; - - /* skip space*/ - skip_space(&flag); - i = 0; - while(mask_ops[i].str!='\0'){ - if(*flag == mask_ops[i].str) { - op = mask_ops[i].op; - break; + uint32_t i; + int ret = -1; + enum mask_op op = M_NOP; + char *flag = *oflag; + char *end_word; + uint32_t mask = 0; + + /* skip space */ + skip_space(&flag); + i = 0; + while (mask_ops[i].str != '\0') { + if (*flag == mask_ops[i].str) { + op = mask_ops[i].op; + break; + } + i++; + } + flag++; + if (op == M_NOP) { + ret = EINVAL; + goto exit; + } + skip_space(&flag); + end_word = flag; + while (!isspace(*end_word)) { + if (*end_word == '\0') + break; + end_word++; + } + *end_word = '\0'; + + for (i = 0; i < J_FLAGS; i++) { + if (strncasecmp(flag, flags[i].name, end_word - flag) == 0) { + mask = flags[i].mask; + break; + } + } + flag = end_word; + + if (mask == 0) { + ret = EINVAL; + goto exit; } - i++; - } - flag++; - if (op == m_nop) { - ret = EINVAL; - goto exit; - } - skip_space(&flag); - end_word = flag; - while(!isspace(*end_word)) { - if(*end_word == '\0') - break; - end_word++; - } - *end_word = '\0'; - - for(i=0;i<J_FLAGS;i++) { - if (strncasecmp(flag,flags[i].name,end_word - flag) == 0) { - mask = flags[i].mask; - break; + switch (op) { + case M_ADD: + *omask |= mask; + break; + case M_CLEAR: + *omask &= ~mask; + break; + case M_XOR: + *omask ^= mask; + break; + default: + ret = EINVAL; + break; } - } - flag = end_word; - - if (mask == 0) { - ret = EINVAL; - goto exit; - } - - switch (op) { - case m_add: - *omask |= mask; - break; - case m_clear: - *omask &= ~mask; - break; - case m_xor: - *omask ^= mask; - break; - default: - ret = EINVAL; - break; - } - + exit: - *oflag = flag; - return ret; + *oflag = flag; + return ret; } static int jail_flags(struct jail_2 *j, char *cflags) { - struct jail2_stats *jst; - int ret = 0; - uint32_t oldfl; - - j->cmd = C_STAT; - jst = malloc(sizeof(struct jail2_stats)); - if (jst == NULL) - return (ENOMEM); - j->data = jst; - - ret = jail(j); - if (ret != 0) - goto exit_free; - - oldfl = jst->v1.flags; + struct jail2_stats *jst; + int ret = 0; + uint32_t oldfl; + + j->cmd = C_STAT; + jst = malloc(sizeof(struct jail2_stats)); + if (jst == NULL) + return (ENOMEM); + j->data = jst; + + ret = jail(j); + if (ret != 0) + goto exit_free; + + oldfl = jst->v1.flags; + + while (*cflags != '\0') + parse_flag(&cflags, &oldfl); - while(*cflags != '\0') - parse_flag(&cflags,&oldfl); + j->cmd = C_FLAGS; + j->data = &oldfl; + ret = jail(j); - j->cmd = C_FLAGS; - j->data = &oldfl; - ret = jail(j); - exit_free: - free(jst); - return (ret); + free(jst); + return (ret); } int main(int argc, char **argv) { - int ch; - char *error; - struct jail_2 j; - int ret; - - /* options descriptor */ - static struct option longopts[] = { - { "ctx", required_argument, NULL, C_CTX }, - { "create", no_argument, NULL, C_CREATE }, - { "destroy", no_argument, NULL, C_DESTROY }, - { "start", no_argument, NULL, C_START }, - { "stop", no_argument, NULL, C_STOP }, - { "enter", no_argument, NULL, C_ENTER }, - { "exec", required_argument, NULL, C_EXEC }, - { "stat", no_argument, NULL, C_STAT }, - { "flags", required_argument, NULL, C_FLAGS }, - { NULL, 0, NULL, 0 } - }; + int ch; + char *error; + struct jail_2 j; + int ret; + + /* options descriptor */ + static struct option longopts[] = { + {"ctx", required_argument, NULL, C_CTX}, + {"create", no_argument, NULL, C_CREATE}, + {"destroy", no_argument, NULL, C_DESTROY}, + {"start", no_argument, NULL, C_START}, + {"stop", no_argument, NULL, C_STOP}, + {"enter", no_argument, NULL, C_ENTER}, + {"exec", required_argument, NULL, C_EXEC}, + {"stat", no_argument, NULL, C_STAT}, + {"flags", required_argument, NULL, C_FLAGS}, + {NULL, 0, NULL, 0} + }; j.version = 2; j.ctx_id = 0; j.data = NULL; j.cmd = 0; - while ((ch = getopt_long(argc, argv, "", longopts, NULL)) != -1) - { - switch( ch ) { + while ((ch = getopt_long(argc, argv, "", longopts, NULL)) != -1) { + switch (ch) { case C_CTX: - j.ctx_id = strtol(optarg,&error,10); + j.ctx_id = strtol(optarg, &error, 10); if (*error != '\0') - errx(1,"Error in context identifyer"); + errx(1, "Error in context identifyer"); break; case C_CREATE: j.cmd = C_CREATE; @@ -289,38 +295,38 @@ j.cmd = C_STOP; ret = jail(&j); if (ret != 0) - err(5,"Stop"); + err(5, "Stop"); break; case C_ENTER: ret = jail_exec(&j, default_exec, login_shell); if (ret != 0) - err(6,"Enter"); + err(6, "Enter"); break; case C_EXEC: - ret = jail_exec(&j,optarg, argv+(optind-1)); + ret = jail_exec(&j, optarg, argv + (optind - 1)); if (ret != 0) - err(7,"Exec"); + err(7, "Exec"); break; case C_STAT: j.cmd = C_STAT; - if (jail_stats(&j)!=0) - err(8,"Error get jail stats"); + if (jail_stats(&j) != 0) + err(8, "Error get jail stats"); break; case C_FLAGS: - if (jail_flags(&j, optarg)!=0) - err(8,"Error set jail flags"); + if (jail_flags(&j, optarg) != 0) + err(8, "Error set jail flags"); break; default: usage(); - errx(100,"Unknow command"); + errx(100, "Unknow command"); } } - return( 0 ); + return (EX_OK); } static void usage(void) { - exit(1); + exit(EX_USAGE); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200606190632.k5J6WgMZ021665>