Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/jls/jls.c
| Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | |||||
| #include <stdlib.h> | #include <stdlib.h> | ||||
| #include <string.h> | #include <string.h> | ||||
| #include <unistd.h> | #include <unistd.h> | ||||
| #include <libxo/xo.h> | #include <libxo/xo.h> | ||||
| #define JP_USER 0x01000000 | #define JP_USER 0x01000000 | ||||
| #define JP_OPT 0x02000000 | #define JP_OPT 0x02000000 | ||||
| #define JLS_XO_VERSION "1" | #define JLS_XO_VERSION "2" | ||||
| #define PRINT_DEFAULT 0x01 | #define PRINT_DEFAULT 0x01 | ||||
| #define PRINT_HEADER 0x02 | #define PRINT_HEADER 0x02 | ||||
| #define PRINT_NAMEVAL 0x04 | #define PRINT_NAMEVAL 0x04 | ||||
| #define PRINT_QUOTED 0x08 | #define PRINT_QUOTED 0x08 | ||||
| #define PRINT_SKIP 0x10 | #define PRINT_SKIP 0x10 | ||||
| #define PRINT_VERBOSE 0x20 | #define PRINT_VERBOSE 0x20 | ||||
| #define PRINT_JAIL_NAME 0x40 | #define PRINT_JAIL_NAME 0x40 | ||||
| Show All 9 Lines | |||||
| #endif | #endif | ||||
| static int add_param(const char *name, void *value, size_t valuelen, | static int add_param(const char *name, void *value, size_t valuelen, | ||||
| struct jailparam *source, unsigned flags); | struct jailparam *source, unsigned flags); | ||||
| static int sort_param(const void *a, const void *b); | static int sort_param(const void *a, const void *b); | ||||
| static char *noname(const char *name); | static char *noname(const char *name); | ||||
| static char *nononame(const char *name); | static char *nononame(const char *name); | ||||
| static int print_jail(int pflags, int jflags); | static int print_jail(int pflags, int jflags); | ||||
| static int special_print(int pflags, struct jailparam *param); | |||||
| static void quoted_print(int pflags, char *name, char *value); | static void quoted_print(int pflags, char *name, char *value); | ||||
| static void emit_ip_addr_list(int af_family, const char *list_name, struct jailparam *param); | |||||
| int | int | ||||
| main(int argc, char **argv) | main(int argc, char **argv) | ||||
| { | { | ||||
| char *dot, *ep, *jname, *pname; | char *dot, *ep, *jname, *pname; | ||||
| int c, i, jflags, jid, lastjid, pflags, spc; | int c, i, jflags, jid, lastjid, pflags, spc; | ||||
| argc = xo_parse_args(argc, argv); | argc = xo_parse_args(argc, argv); | ||||
| ▲ Show 20 Lines • Show All 285 Lines • ▼ Show 20 Lines | nononame(const char *name) | ||||
| return nname; | return nname; | ||||
| } | } | ||||
| static int | static int | ||||
| print_jail(int pflags, int jflags) | print_jail(int pflags, int jflags) | ||||
| { | { | ||||
| char *nname, *xo_nname; | char *nname, *xo_nname; | ||||
| char **param_values; | char **param_values; | ||||
| int i, ai, jid, count, n, spc; | int i, jid, n, spc; | ||||
| char ipbuf[INET6_ADDRSTRLEN]; | |||||
| jid = jailparam_get(params, nparams, jflags); | jid = jailparam_get(params, nparams, jflags); | ||||
| if (jid < 0) | if (jid < 0) | ||||
| return jid; | return jid; | ||||
| xo_open_instance("jail"); | xo_open_instance("jail"); | ||||
| if (pflags & PRINT_VERBOSE) { | if (pflags & PRINT_VERBOSE) { | ||||
| xo_emit("{:jid/%6d}{P: }{:hostname/%-29.29s/%s}{P: }" | xo_emit("{:jid/%6d}{P: }{:hostname/%-29.29s/%s}{P: }" | ||||
| "{:path/%.74s/%s}\n", | "{:path/%.74s/%s}\n", | ||||
| *(int *)params[0].jp_value, | *(int *)params[0].jp_value, | ||||
| (char *)params[1].jp_value, | (char *)params[1].jp_value, | ||||
| (char *)params[2].jp_value); | (char *)params[2].jp_value); | ||||
| xo_emit("{P: }{:name/%-29.29s/%s}{P: }{:state/%.74s}\n", | xo_emit("{P: }{:name/%-29.29s/%s}{P: }{:state/%.74s}\n", | ||||
| (char *)params[3].jp_value, | (char *)params[3].jp_value, | ||||
| *(int *)params[4].jp_value ? "DYING" : "ACTIVE"); | *(int *)params[4].jp_value ? "DYING" : "ACTIVE"); | ||||
| xo_emit("{P: }{:cpusetid/%d}\n", *(int *)params[5].jp_value); | xo_emit("{P: }{:cpusetid/%d}\n", *(int *)params[5].jp_value); | ||||
| n = 6; | n = 6; | ||||
| #ifdef INET | #ifdef INET | ||||
| if (ip4_ok && !strcmp(params[n].jp_name, "ip4.addr")) { | if (ip4_ok && !strcmp(params[n].jp_name, "ip4.addr")) { | ||||
| count = params[n].jp_valuelen / sizeof(struct in_addr); | emit_ip_addr_list(AF_INET, "ipv4_addrs", params + n); | ||||
| for (ai = 0; ai < count; ai++) | |||||
| if (inet_ntop(AF_INET, | |||||
| &((struct in_addr *)params[n].jp_value)[ai], | |||||
| ipbuf, sizeof(ipbuf)) == NULL) | |||||
| xo_err(1, "inet_ntop"); | |||||
| else { | |||||
| xo_emit("{P: }{l:ipv4_addrs}{P:\n}", ipbuf); | |||||
| } | |||||
| n++; | n++; | ||||
| } | } | ||||
| #endif | #endif | ||||
| #ifdef INET6 | #ifdef INET6 | ||||
| if (ip6_ok && !strcmp(params[n].jp_name, "ip6.addr")) { | if (ip6_ok && !strcmp(params[n].jp_name, "ip6.addr")) { | ||||
| count = params[n].jp_valuelen / sizeof(struct in6_addr); | emit_ip_addr_list(AF_INET6, "ipv6_addrs", params + n); | ||||
| for (ai = 0; ai < count; ai++) | |||||
| if (inet_ntop(AF_INET6, | |||||
| &((struct in6_addr *) | |||||
| params[n].jp_value)[ai], | |||||
| ipbuf, sizeof(ipbuf)) == NULL) | |||||
| xo_err(1, "inet_ntop"); | |||||
| else | |||||
| xo_emit("{P: }{l:ipv6_addrs}{P:\n}", ipbuf); | |||||
| n++; | n++; | ||||
| } | } | ||||
| #endif | #endif | ||||
| } else if (pflags & PRINT_DEFAULT) { | } else if (pflags & PRINT_DEFAULT) { | ||||
| if (pflags & PRINT_JAIL_NAME) | if (pflags & PRINT_JAIL_NAME) | ||||
| xo_emit("{P: }{:name/%-15s/%s}{P: }", | xo_emit("{P: }{:name/%-15s/%s}{P: }", | ||||
| (char *)params[0].jp_value); | (char *)params[0].jp_value); | ||||
| else | else | ||||
| Show All 10 Lines | #else | ||||
| (char *)params[2].jp_value); | (char *)params[2].jp_value); | ||||
| #endif | #endif | ||||
| } else { | } else { | ||||
| param_values = alloca(nparams * sizeof(*param_values)); | param_values = alloca(nparams * sizeof(*param_values)); | ||||
| for (i = 0; i < nparams; i++) { | for (i = 0; i < nparams; i++) { | ||||
| if (!(params[i].jp_flags & JP_USER)) | if (!(params[i].jp_flags & JP_USER)) | ||||
| continue; | continue; | ||||
| param_values[i] = jailparam_export(params + i); | param_values[i] = jailparam_export(params + i); | ||||
| if (param_values[i] == NULL) | if (param_values[i] == NULL) | ||||
jamie: This line got somehow duplicated. | |||||
| xo_errx(1, "%s", jail_errmsg); | xo_errx(1, "%s", jail_errmsg); | ||||
| } | } | ||||
| for (i = spc = 0; i < nparams; i++) { | for (i = spc = 0; i < nparams; i++) { | ||||
| if (!(params[i].jp_flags & JP_USER)) | if (!(params[i].jp_flags & JP_USER)) | ||||
| continue; | continue; | ||||
| if ((pflags & PRINT_SKIP) && | if ((pflags & PRINT_SKIP) && | ||||
| ((!(params[i].jp_ctltype & | ((!(params[i].jp_ctltype & | ||||
| (CTLFLAG_WR | CTLFLAG_TUN))) || | (CTLFLAG_WR | CTLFLAG_TUN))) || | ||||
| Show All 32 Lines | for (i = spc = 0; i < nparams; i++) { | ||||
| xo_emit("{d:/%s}", nname); | xo_emit("{d:/%s}", nname); | ||||
| free(nname); | free(nname); | ||||
| } | } | ||||
| free(xo_nname); | free(xo_nname); | ||||
| continue; | continue; | ||||
| } | } | ||||
| xo_emit("{d:%s}=", params[i].jp_name); | xo_emit("{d:%s}=", params[i].jp_name); | ||||
| } | } | ||||
| if (params[i].jp_valuelen == 0) { | |||||
| if (pflags & PRINT_QUOTED) | if (!special_print(pflags, params + i)) | ||||
| xo_emit("{P:\"\"}"); | |||||
| else if (!(pflags & PRINT_NAMEVAL)) | |||||
| xo_emit("{P:-}"); | |||||
| } else { | |||||
| quoted_print(pflags, params[i].jp_name, param_values[i]); | quoted_print(pflags, params[i].jp_name, param_values[i]); | ||||
| } | } | ||||
| } | |||||
| xo_emit("{P:\n}"); | xo_emit("{P:\n}"); | ||||
| for (i = 0; i < nparams; i++) | for (i = 0; i < nparams; i++) | ||||
| if (params[i].jp_flags & JP_USER) | if (params[i].jp_flags & JP_USER) | ||||
| free(param_values[i]); | free(param_values[i]); | ||||
| } | } | ||||
| xo_close_instance("jail"); | xo_close_instance("jail"); | ||||
| return (jid); | return (jid); | ||||
| Show All 29 Lines | quoted_print(int pflags, char *name, char *value) | ||||
| xo_emit(param_name_value, value); | xo_emit(param_name_value, value); | ||||
| free(param_name_value); | free(param_name_value); | ||||
| if (qc && pflags & PRINT_QUOTED) | if (qc && pflags & PRINT_QUOTED) | ||||
| xo_emit("{P:/%c}", qc); | xo_emit("{P:/%c}", qc); | ||||
| } | } | ||||
| static int | |||||
| special_print(int pflags, struct jailparam *param) { | |||||
| int ip_as_list; | |||||
| switch (xo_get_style(NULL)) { | |||||
| case XO_STYLE_JSON: | |||||
| case XO_STYLE_XML: | |||||
| ip_as_list = 1; | |||||
| break; | |||||
| default: | |||||
| ip_as_list = 0; | |||||
| } | |||||
| if (!ip_as_list && param->jp_valuelen == 0) { | |||||
| if (pflags & PRINT_QUOTED) | |||||
| xo_emit("{P:\"\"}"); | |||||
| else if (!(pflags & PRINT_NAMEVAL)) | |||||
| xo_emit("{P:-}"); | |||||
| } else if (ip_as_list && !strcmp(param->jp_name, "ip4.addr")) { | |||||
| emit_ip_addr_list(AF_INET, param->jp_name, param); | |||||
| } else if (ip_as_list && !strcmp(param->jp_name, "ip6.addr")) { | |||||
| emit_ip_addr_list(AF_INET6, param->jp_name, param); | |||||
| } else { | |||||
| return 0; | |||||
| } | |||||
| return 1; | |||||
| } | |||||
| static void | |||||
| emit_ip_addr_list(int af_family, const char *list_name, struct jailparam *param) { | |||||
| char ipbuf[INET6_ADDRSTRLEN]; | |||||
| size_t addr_len; | |||||
| const char *emit_str; | |||||
| int count, ai; | |||||
| switch (af_family) { | |||||
| case AF_INET: | |||||
| addr_len = sizeof(struct in_addr); | |||||
| emit_str = "{P: }{ql:ipv4_addr}{P:\n}"; | |||||
| break; | |||||
| case AF_INET6: | |||||
| addr_len = sizeof(struct in6_addr); | |||||
| emit_str = "{P: }{ql:ipv6_addr}{P:\n}"; | |||||
| break; | |||||
| default: | |||||
| xo_err(1, "unsupported af_family"); | |||||
| return; | |||||
| } | |||||
| count = param->jp_valuelen / addr_len; | |||||
| xo_open_list(list_name); | |||||
| for (ai = 0; ai < count; ai++) { | |||||
| if (inet_ntop(af_family, | |||||
| ((uint8_t*)param->jp_value) + addr_len*ai, | |||||
| ipbuf, sizeof(ipbuf)) == NULL) { | |||||
| xo_err(1, "inet_ntop"); | |||||
| } else { | |||||
| xo_emit(emit_str, ipbuf); | |||||
| } | |||||
| } | |||||
| xo_close_list(list_name); | |||||
| } | |||||
This line got somehow duplicated.