View | Details | Raw Unified | Return to bug 215008 | Differences between
and this patch

Collapse All | Expand All

(-)b/usr.sbin/jls/jls.c (-30 / +78 lines)
Lines 77-83 static int sort_param(const void *a, const void *b); Link Here
77
static char *noname(const char *name);
77
static char *noname(const char *name);
78
static char *nononame(const char *name);
78
static char *nononame(const char *name);
79
static int print_jail(int pflags, int jflags);
79
static int print_jail(int pflags, int jflags);
80
static int special_print(int pflags, struct jailparam *param);
80
static void quoted_print(int pflags, char *name, char *value);
81
static void quoted_print(int pflags, char *name, char *value);
82
static void emit_ip_addr(int af_family, struct jailparam *param);
81
83
82
int
84
int
83
main(int argc, char **argv)
85
main(int argc, char **argv)
Lines 379-386 print_jail(int pflags, int jflags) Link Here
379
{
381
{
380
	char *nname, *xo_nname;
382
	char *nname, *xo_nname;
381
	char **param_values;
383
	char **param_values;
382
	int i, ai, jid, count, n, spc;
384
	int i, jid, n, spc;
383
	char ipbuf[INET6_ADDRSTRLEN];
384
385
385
	jid = jailparam_get(params, nparams, jflags);
386
	jid = jailparam_get(params, nparams, jflags);
386
	if (jid < 0)
387
	if (jid < 0)
Lines 402-435 print_jail(int pflags, int jflags) Link Here
402
#ifdef INET
403
#ifdef INET
403
		if (ip4_ok && !strcmp(params[n].jp_name, "ip4.addr")) {
404
		if (ip4_ok && !strcmp(params[n].jp_name, "ip4.addr")) {
404
			xo_open_list("ipv4_addrs");
405
			xo_open_list("ipv4_addrs");
405
			count = params[n].jp_valuelen / sizeof(struct in_addr);
406
			emit_ip_addr(AF_INET, params + n);
406
			for (ai = 0; ai < count; ai++) {
407
				if (inet_ntop(AF_INET,
408
				    &((struct in_addr *)params[n].jp_value)[ai],
409
				    ipbuf, sizeof(ipbuf)) == NULL) {
410
					xo_err(1, "inet_ntop");
411
				} else {
412
					xo_emit("{P:        }{ql:ipv4_addr}{P:\n}", ipbuf);
413
				}
414
			}
415
			xo_close_list("ipv4_addrs");
407
			xo_close_list("ipv4_addrs");
416
			n++;
408
			n++;
417
		}
409
		}
418
#endif
410
#endif
419
#ifdef INET6
411
#ifdef INET6
420
		if (ip6_ok && !strcmp(params[n].jp_name, "ip6.addr")) {
412
		if (ip6_ok && !strcmp(params[n].jp_name, "ip6.addr")) {
421
			count = params[n].jp_valuelen / sizeof(struct in6_addr);
422
			xo_open_list("ipv6_addrs");
413
			xo_open_list("ipv6_addrs");
423
			for (ai = 0; ai < count; ai++) {
414
			emit_ip_addr(AF_INET6, params + n);
424
				if (inet_ntop(AF_INET6,
425
				    &((struct in6_addr *)
426
					params[n].jp_value)[ai],
427
				    ipbuf, sizeof(ipbuf)) == NULL) {
428
					xo_err(1, "inet_ntop");
429
				} else {
430
					xo_emit("{P:        }{ql:ipv6_addr}{P:\n}", ipbuf);
431
				}
432
			}
433
			xo_close_list("ipv6_addrs");
415
			xo_close_list("ipv6_addrs");
434
			n++;
416
			n++;
435
		}
417
		}
Lines 457-462 print_jail(int pflags, int jflags) Link Here
457
			if (!(params[i].jp_flags & JP_USER))
439
			if (!(params[i].jp_flags & JP_USER))
458
				continue;
440
				continue;
459
			param_values[i] = jailparam_export(params + i);
441
			param_values[i] = jailparam_export(params + i);
442
			param_values[i] = jailparam_export(params + i);
460
			if (param_values[i] == NULL)
443
			if (param_values[i] == NULL)
461
				xo_errx(1, "%s", jail_errmsg);
444
				xo_errx(1, "%s", jail_errmsg);
462
		}
445
		}
Lines 506-519 print_jail(int pflags, int jflags) Link Here
506
				}
489
				}
507
				xo_emit("{d:%s}=", params[i].jp_name);
490
				xo_emit("{d:%s}=", params[i].jp_name);
508
			}
491
			}
509
			if (params[i].jp_valuelen == 0) {
492
510
				if (pflags & PRINT_QUOTED)
493
			if (!special_print(pflags, params + i))
511
					xo_emit("{P:\"\"}");
512
				else if (!(pflags & PRINT_NAMEVAL))
513
					xo_emit("{P:-}");
514
			} else {
515
				quoted_print(pflags, params[i].jp_name, param_values[i]);
494
				quoted_print(pflags, params[i].jp_name, param_values[i]);
516
			}
495
517
		}
496
		}
518
		xo_emit("{P:\n}");
497
		xo_emit("{P:\n}");
519
		for (i = 0; i < nparams; i++)
498
		for (i = 0; i < nparams; i++)
Lines 560-562 quoted_print(int pflags, char *name, char *value) Link Here
560
	if (qc && pflags & PRINT_QUOTED)
539
	if (qc && pflags & PRINT_QUOTED)
561
		xo_emit("{P:/%c}", qc);
540
		xo_emit("{P:/%c}", qc);
562
}
541
}
542
543
static int
544
special_print(int pflags, struct jailparam *param) {
545
546
	int ip_as_list;
547
548
	switch (xo_get_style(NULL)) {
549
		case XO_STYLE_JSON:
550
		case XO_STYLE_XML:
551
			ip_as_list = 1;
552
			break;
553
		default:
554
			ip_as_list = 0;
555
	}
556
557
	if (!ip_as_list && param->jp_valuelen == 0) {
558
		if (pflags & PRINT_QUOTED)
559
			xo_emit("{P:\"\"}");
560
		else if (!(pflags & PRINT_NAMEVAL))
561
			xo_emit("{P:-}");
562
	} else if (ip_as_list && !strcmp(param->jp_name, "ip4.addr")) {
563
		xo_open_list("ip4.addr");
564
		emit_ip_addr(AF_INET, param);
565
		xo_close_list("ip4.addr");
566
	} else if (ip_as_list && !strcmp(param->jp_name, "ip6.addr")) {
567
		xo_open_list("ip6.addr");
568
		emit_ip_addr(AF_INET6, param);
569
		xo_close_list("ip6.addr");
570
	}  else {
571
		return 0;
572
	}
573
574
	return 1;
575
}
576
577
static void
578
emit_ip_addr(int af_family, struct jailparam *param) {
579
	char ipbuf[INET6_ADDRSTRLEN];
580
	size_t addr_len;
581
	const char *emit_str;
582
	int count, ai;
583
584
	switch (af_family) {
585
		case AF_INET:
586
			addr_len = sizeof(struct in_addr);
587
			emit_str = "{P:        }{ql:ipv4_addr}{P:\n}";
588
			break;
589
		case AF_INET6:
590
			addr_len = sizeof(struct in6_addr);
591
			emit_str = "{P:        }{ql:ipv6_addr}{P:\n}";
592
			break;
593
		default:
594
			xo_err(1, "unsupported af_family");
595
			return;
596
	}
597
598
	count = param->jp_valuelen / addr_len;
599
600
	for (ai = 0; ai < count; ai++) {
601
		if (inet_ntop(af_family,
602
			      ((uint8_t*)param->jp_value) + addr_len*ai,
603
			      ipbuf, sizeof(ipbuf)) == NULL) {
604
			xo_err(1, "inet_ntop");
605
		} else {
606
			xo_emit(emit_str, ipbuf);
607
		}
608
	}
609
610
}

Return to bug 215008