From owner-p4-projects@FreeBSD.ORG Mon Jun 19 06:32:43 2006 Return-Path: X-Original-To: p4-projects@freebsd.org Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id CE57D16A47C; Mon, 19 Jun 2006 06:32:43 +0000 (UTC) X-Original-To: perforce@FreeBSD.org Delivered-To: perforce@FreeBSD.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 8EDC116A474 for ; Mon, 19 Jun 2006 06:32:43 +0000 (UTC) (envelope-from als@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id 423EC43D46 for ; Mon, 19 Jun 2006 06:32:43 +0000 (GMT) (envelope-from als@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.6/8.13.6) with ESMTP id k5J6WhA6021668 for ; Mon, 19 Jun 2006 06:32:43 GMT (envelope-from als@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.6/8.13.4/Submit) id k5J6WgMZ021665 for perforce@freebsd.org; Mon, 19 Jun 2006 06:32:42 GMT (envelope-from als@FreeBSD.org) Date: Mon, 19 Jun 2006 06:32:42 GMT Message-Id: <200606190632.k5J6WgMZ021665@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to als@FreeBSD.org using -f From: Alex Lyashkov To: Perforce Change Reviews Cc: Subject: PERFORCE change 99584 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Jun 2006 06:32:44 -0000 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 #include +#include +#include +#include +#include #include #include +#include +#include #include -#include -#include -#include -#include -#include + + 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;iflags & 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;icmd = 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); }