print.c revision 130856
1/*-
2 * Copyright (c) 1990, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
33#endif /* not lint */
34#endif
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/bin/ps/print.c 130856 2004-06-21 16:53:11Z gad $");
38
39#include <sys/param.h>
40#include <sys/time.h>
41#include <sys/resource.h>
42#include <sys/proc.h>
43#include <sys/stat.h>
44
45#include <sys/mac.h>
46#include <sys/user.h>
47#include <sys/sysctl.h>
48
49#include <err.h>
50#include <grp.h>
51#include <langinfo.h>
52#include <locale.h>
53#include <math.h>
54#include <nlist.h>
55#include <pwd.h>
56#include <stddef.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61#include <vis.h>
62
63#include "ps.h"
64
65#define	ps_pgtok(a)	(((a) * getpagesize()) / 1024)
66
67void
68printheader(void)
69{
70	VAR *v;
71	struct varent *vent;
72	int allempty;
73
74	allempty = 1;
75	for (vent = vhead; vent; vent = vent->next)
76		if (*vent->header != '\0') {
77			allempty = 0;
78			break;
79		}
80	if (allempty)
81		return;
82	for (vent = vhead; vent; vent = vent->next) {
83		v = vent->var;
84		if (v->flag & LJUST) {
85			if (vent->next == NULL)	/* last one */
86				(void)printf("%s", vent->header);
87			else
88				(void)printf("%-*s", v->width, vent->header);
89		} else
90			(void)printf("%*s", v->width, vent->header);
91		if (vent->next != NULL)
92			(void)putchar(' ');
93	}
94	(void)putchar('\n');
95}
96
97void
98arguments(KINFO *k, VARENT *ve)
99{
100	VAR *v;
101	int left;
102	char *cp, *vis_args;
103
104	v = ve->var;
105	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
106		errx(1, "malloc failed");
107	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
108	if (ve->next == NULL) {
109		/* last field */
110		if (termwidth == UNLIMITED) {
111			(void)printf("%s", vis_args);
112		} else {
113			left = termwidth - (totwidth - v->width);
114			if (left < 1) /* already wrapped, just use std width */
115				left = v->width;
116			for (cp = vis_args; --left >= 0 && *cp != '\0';)
117				(void)putchar(*cp++);
118		}
119	} else {
120		(void)printf("%-*.*s", v->width, v->width, vis_args);
121	}
122	free(vis_args);
123}
124
125void
126command(KINFO *k, VARENT *ve)
127{
128	VAR *v;
129	int left;
130	char *cp, *vis_env, *vis_args;
131
132	v = ve->var;
133	if (cflag) {
134		if (ve->next == NULL)	/* last field, don't pad */
135			(void)printf("%s", k->ki_p->ki_comm);
136		else
137			(void)printf("%-*s", v->width, k->ki_p->ki_comm);
138		return;
139	}
140	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
141		errx(1, "malloc failed");
142	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
143	if (k->ki_env) {
144		if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
145			errx(1, "malloc failed");
146		strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
147	} else
148		vis_env = NULL;
149
150	if (ve->next == NULL) {
151		/* last field */
152		if (termwidth == UNLIMITED) {
153			if (vis_env)
154				(void)printf("%s ", vis_env);
155			(void)printf("%s", vis_args);
156		} else {
157			left = termwidth - (totwidth - v->width);
158			if (left < 1) /* already wrapped, just use std width */
159				left = v->width;
160			if ((cp = vis_env) != NULL) {
161				while (--left >= 0 && *cp)
162					(void)putchar(*cp++);
163				if (--left >= 0)
164					putchar(' ');
165			}
166			for (cp = vis_args; --left >= 0 && *cp != '\0';)
167				(void)putchar(*cp++);
168		}
169	} else
170		/* XXX env? */
171		(void)printf("%-*.*s", v->width, v->width, vis_args);
172	free(vis_args);
173	if (vis_env != NULL)
174		free(vis_env);
175}
176
177void
178ucomm(KINFO *k, VARENT *ve)
179{
180	VAR *v;
181
182	v = ve->var;
183	(void)printf("%-*s", v->width, k->ki_p->ki_comm);
184}
185
186void
187logname(KINFO *k, VARENT *ve)
188{
189	VAR *v;
190	char *s;
191
192	v = ve->var;
193	(void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
194}
195
196void
197state(KINFO *k, VARENT *ve)
198{
199	int flag, sflag, tdflags;
200	char *cp;
201	VAR *v;
202	char buf[16];
203
204	v = ve->var;
205	flag = k->ki_p->ki_flag;
206	sflag = k->ki_p->ki_sflag;
207	tdflags = k->ki_p->ki_tdflags;	/* XXXKSE */
208	cp = buf;
209
210	switch (k->ki_p->ki_stat) {
211
212	case SSTOP:
213		*cp = 'T';
214		break;
215
216	case SSLEEP:
217		if (tdflags & TDF_SINTR)	/* interruptable (long) */
218			*cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
219		else
220			*cp = 'D';
221		break;
222
223	case SRUN:
224	case SIDL:
225		*cp = 'R';
226		break;
227
228	case SWAIT:
229		*cp = 'W';
230		break;
231
232	case SLOCK:
233		*cp = 'L';
234		break;
235
236	case SZOMB:
237		*cp = 'Z';
238		break;
239
240	default:
241		*cp = '?';
242	}
243	cp++;
244	if (!(sflag & PS_INMEM))
245		*cp++ = 'W';
246	if (k->ki_p->ki_nice < NZERO)
247		*cp++ = '<';
248	else if (k->ki_p->ki_nice > NZERO)
249		*cp++ = 'N';
250	if (flag & P_TRACED)
251		*cp++ = 'X';
252	if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
253		*cp++ = 'E';
254	if (flag & P_PPWAIT)
255		*cp++ = 'V';
256	if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
257		*cp++ = 'L';
258	if (k->ki_p->ki_kiflag & KI_SLEADER)
259		*cp++ = 's';
260	if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
261		*cp++ = '+';
262	if (flag & P_JAILED)
263		*cp++ = 'J';
264	*cp = '\0';
265	(void)printf("%-*s", v->width, buf);
266}
267
268void
269pri(KINFO *k, VARENT *ve)
270{
271	VAR *v;
272
273	v = ve->var;
274	(void)printf("%*d", v->width, k->ki_p->ki_pri.pri_level - PZERO);
275}
276
277void
278uname(KINFO *k, VARENT *ve)
279{
280	VAR *v;
281
282	v = ve->var;
283	(void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
284}
285
286int
287s_uname(KINFO *k)
288{
289	return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
290}
291
292void
293rgroupname(KINFO *k, VARENT *ve)
294{
295	VAR *v;
296
297	v = ve->var;
298	(void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
299}
300
301int
302s_rgroupname(KINFO *k)
303{
304	return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
305}
306
307void
308runame(KINFO *k, VARENT *ve)
309{
310	VAR *v;
311
312	v = ve->var;
313	(void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
314}
315
316int
317s_runame(KINFO *k)
318{
319	return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
320}
321
322
323void
324tdev(KINFO *k, VARENT *ve)
325{
326	VAR *v;
327	dev_t dev;
328	char buff[16];
329
330	v = ve->var;
331	dev = k->ki_p->ki_tdev;
332	if (dev == NODEV)
333		(void)printf("%*s", v->width, "??");
334	else {
335		(void)snprintf(buff, sizeof(buff),
336		    "%d/%d", major(dev), minor(dev));
337		(void)printf("%*s", v->width, buff);
338	}
339}
340
341void
342tname(KINFO *k, VARENT *ve)
343{
344	VAR *v;
345	dev_t dev;
346	char *ttname;
347
348	v = ve->var;
349	dev = k->ki_p->ki_tdev;
350	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
351		(void)printf("%*s ", v->width - 1, "??");
352	else {
353		if (strncmp(ttname, "tty", 3) == 0 ||
354		    strncmp(ttname, "cua", 3) == 0)
355			ttname += 3;
356		(void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname,
357		    k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
358	}
359}
360
361void
362longtname(KINFO *k, VARENT *ve)
363{
364	VAR *v;
365	dev_t dev;
366	char *ttname;
367
368	v = ve->var;
369	dev = k->ki_p->ki_tdev;
370	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
371		(void)printf("%-*s", v->width, "??");
372	else
373		(void)printf("%-*s", v->width, ttname);
374}
375
376void
377started(KINFO *k, VARENT *ve)
378{
379	VAR *v;
380	time_t then;
381	struct tm *tp;
382	static int use_ampm = -1;
383	char buf[100];
384
385	v = ve->var;
386	if (!k->ki_valid) {
387		(void)printf("%-*s", v->width, "-");
388		return;
389	}
390	if (use_ampm < 0)
391		use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
392	then = k->ki_p->ki_start.tv_sec;
393	tp = localtime(&then);
394	if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
395		(void)strftime(buf, sizeof(buf),
396		    use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
397	} else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
398		(void)strftime(buf, sizeof(buf),
399		    use_ampm ? "%a%I%p" : "%a%H  ", tp);
400	} else
401		(void)strftime(buf, sizeof(buf), "%e%b%y", tp);
402	(void)printf("%-*s", v->width, buf);
403}
404
405void
406lstarted(KINFO *k, VARENT *ve)
407{
408	VAR *v;
409	time_t then;
410	char buf[100];
411
412	v = ve->var;
413	if (!k->ki_valid) {
414		(void)printf("%-*s", v->width, "-");
415		return;
416	}
417	then = k->ki_p->ki_start.tv_sec;
418	(void)strftime(buf, sizeof(buf), "%c", localtime(&then));
419	(void)printf("%-*s", v->width, buf);
420}
421
422void
423lockname(KINFO *k, VARENT *ve)
424{
425	VAR *v;
426
427	v = ve->var;
428	if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
429		if (k->ki_p->ki_lockname[0] != 0)
430			(void)printf("%-*.*s", v->width, v->width,
431			    k->ki_p->ki_lockname);
432		else
433			(void)printf("%-*s", v->width, "???");
434	} else
435		(void)printf("%-*s", v->width, "-");
436}
437
438void
439wchan(KINFO *k, VARENT *ve)
440{
441	VAR *v;
442
443	v = ve->var;
444	if (k->ki_p->ki_wchan) {
445		if (k->ki_p->ki_wmesg[0] != 0)
446			(void)printf("%-*.*s", v->width, v->width,
447			    k->ki_p->ki_wmesg);
448		else
449			(void)printf("%-*lx", v->width,
450			    (long)k->ki_p->ki_wchan);
451	} else
452		(void)printf("%-*s", v->width, "-");
453}
454
455void
456nwchan(KINFO *k, VARENT *ve)
457{
458	VAR *v;
459
460	v = ve->var;
461	if (k->ki_p->ki_wchan) {
462		(void)printf("%0*lx", v->width,
463		    (long)k->ki_p->ki_wchan);
464	} else
465		(void)printf("%-*s", v->width, "-");
466}
467
468void
469mwchan(KINFO *k, VARENT *ve)
470{
471	VAR *v;
472
473	v = ve->var;
474	if (k->ki_p->ki_wchan) {
475		if (k->ki_p->ki_wmesg[0] != 0)
476			(void)printf("%-*.*s", v->width, v->width,
477			    k->ki_p->ki_wmesg);
478		else
479			(void)printf("%-*lx", v->width,
480			    (long)k->ki_p->ki_wchan);
481	} else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
482		if (k->ki_p->ki_lockname[0]) {
483			(void)printf("%-*.*s", v->width, v->width,
484			    k->ki_p->ki_lockname);
485		} else
486			(void)printf("%-*s", v->width, "???");
487	} else
488		(void)printf("%-*s", v->width, "-");
489}
490
491void
492vsize(KINFO *k, VARENT *ve)
493{
494	VAR *v;
495
496	v = ve->var;
497	(void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
498}
499
500void
501cputime(KINFO *k, VARENT *ve)
502{
503	VAR *v;
504	long secs;
505	long psecs;	/* "parts" of a second. first micro, then centi */
506	char obuff[128];
507	static char decimal_point;
508
509	if (decimal_point == '\0')
510		decimal_point = localeconv()->decimal_point[0];
511	v = ve->var;
512	if (!k->ki_valid) {
513		secs = 0;
514		psecs = 0;
515	} else {
516		/*
517		 * This counts time spent handling interrupts.  We could
518		 * fix this, but it is not 100% trivial (and interrupt
519		 * time fractions only work on the sparc anyway).	XXX
520		 */
521		secs = k->ki_p->ki_runtime / 1000000;
522		psecs = k->ki_p->ki_runtime % 1000000;
523		if (sumrusage) {
524			secs += k->ki_p->ki_childtime.tv_sec;
525			psecs += k->ki_p->ki_childtime.tv_usec;
526		}
527		/*
528		 * round and scale to 100's
529		 */
530		psecs = (psecs + 5000) / 10000;
531		secs += psecs / 100;
532		psecs = psecs % 100;
533	}
534	(void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
535	    secs / 60, secs % 60, decimal_point, psecs);
536	(void)printf("%*s", v->width, obuff);
537}
538
539void
540elapsed(KINFO *k, VARENT *ve)
541{
542	VAR *v;
543	time_t val;
544	int days, hours, mins, secs;
545	char obuff[128];
546
547	v = ve->var;
548	val = now - k->ki_p->ki_start.tv_sec;
549	days = val / (24 * 60 * 60);
550	val %= 24 * 60 * 60;
551	hours = val / (60 * 60);
552	val %= 60 * 60;
553	mins = val / 60;
554	secs = val % 60;
555	if (days != 0)
556		(void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
557		    days, hours, mins, secs);
558	else if (hours != 0)
559		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
560		    hours, mins, secs);
561	else
562		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
563	(void)printf("%*s", v->width, obuff);
564}
565
566double
567getpcpu(const KINFO *k)
568{
569	static int failure;
570
571	if (!nlistread)
572		failure = donlist();
573	if (failure)
574		return (0.0);
575
576#define	fxtofl(fixpt)	((double)(fixpt) / fscale)
577
578	/* XXX - I don't like this */
579	if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0)
580		return (0.0);
581	if (rawcpu)
582		return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
583	return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
584		(1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
585}
586
587void
588pcpu(KINFO *k, VARENT *ve)
589{
590	VAR *v;
591
592	v = ve->var;
593	(void)printf("%*.1f", v->width, getpcpu(k));
594}
595
596static double
597getpmem(KINFO *k)
598{
599	static int failure;
600	double fracmem;
601
602	if (!nlistread)
603		failure = donlist();
604	if (failure)
605		return (0.0);
606
607	if ((k->ki_p->ki_sflag & PS_INMEM) == 0)
608		return (0.0);
609	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
610	/* XXX don't have info about shared */
611	fracmem = ((float)k->ki_p->ki_rssize) / mempages;
612	return (100.0 * fracmem);
613}
614
615void
616pmem(KINFO *k, VARENT *ve)
617{
618	VAR *v;
619
620	v = ve->var;
621	(void)printf("%*.1f", v->width, getpmem(k));
622}
623
624void
625pagein(KINFO *k, VARENT *ve)
626{
627	VAR *v;
628
629	v = ve->var;
630	(void)printf("%*ld", v->width,
631	    k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
632}
633
634/* ARGSUSED */
635void
636maxrss(KINFO *k __unused, VARENT *ve)
637{
638	VAR *v;
639
640	v = ve->var;
641	/* XXX not yet */
642	(void)printf("%*s", v->width, "-");
643}
644
645void
646priorityr(KINFO *k, VARENT *ve)
647{
648	VAR *v;
649	struct priority *lpri;
650	char str[8];
651	unsigned class, level;
652
653	v = ve->var;
654	lpri = &k->ki_p->ki_pri;
655	class = lpri->pri_class;
656	level = lpri->pri_level;
657	switch (class) {
658	case PRI_REALTIME:
659		snprintf(str, sizeof(str), "real:%u", level);
660		break;
661	case PRI_TIMESHARE:
662		strncpy(str, "normal", sizeof(str));
663		break;
664	case PRI_IDLE:
665		snprintf(str, sizeof(str), "idle:%u", level);
666		break;
667	default:
668		snprintf(str, sizeof(str), "%u:%u", class, level);
669		break;
670	}
671	str[sizeof(str) - 1] = '\0';
672	(void)printf("%*s", v->width, str);
673}
674
675/*
676 * Generic output routines.  Print fields from various prototype
677 * structures.
678 */
679static void
680printval(void *bp, VAR *v)
681{
682	static char ofmt[32] = "%";
683	const char *fcp;
684	char *cp;
685
686	cp = ofmt + 1;
687	fcp = v->fmt;
688	if (v->flag & LJUST)
689		*cp++ = '-';
690	*cp++ = '*';
691	while ((*cp++ = *fcp++));
692
693#define	CHKINF127(n)	(((n) > 127) && (v->flag & INF127) ? 127 : (n))
694
695	switch (v->type) {
696	case CHAR:
697		(void)printf(ofmt, v->width, *(char *)bp);
698		break;
699	case UCHAR:
700		(void)printf(ofmt, v->width, *(u_char *)bp);
701		break;
702	case SHORT:
703		(void)printf(ofmt, v->width, *(short *)bp);
704		break;
705	case USHORT:
706		(void)printf(ofmt, v->width, *(u_short *)bp);
707		break;
708	case INT:
709		(void)printf(ofmt, v->width, *(int *)bp);
710		break;
711	case UINT:
712		(void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
713		break;
714	case LONG:
715		(void)printf(ofmt, v->width, *(long *)bp);
716		break;
717	case ULONG:
718		(void)printf(ofmt, v->width, *(u_long *)bp);
719		break;
720	case KPTR:
721		(void)printf(ofmt, v->width, *(u_long *)bp);
722		break;
723	case PGTOK:
724		(void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
725		break;
726	default:
727		errx(1, "unknown type %d", v->type);
728	}
729}
730
731void
732kvar(KINFO *k, VARENT *ve)
733{
734	VAR *v;
735
736	v = ve->var;
737	printval((char *)((char *)k->ki_p + v->off), v);
738}
739
740void
741rvar(KINFO *k, VARENT *ve)
742{
743	VAR *v;
744
745	v = ve->var;
746	if (k->ki_valid)
747		printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
748	else
749		(void)printf("%*s", v->width, "-");
750}
751
752void
753emulname(KINFO *k, VARENT *ve)
754{
755	VAR *v;
756
757	v = ve->var;
758	printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
759}
760
761void
762label(KINFO *k, VARENT *ve)
763{
764	char *string;
765	VAR *v;
766	mac_t proclabel;
767	int error;
768
769	v = ve->var;
770	string = NULL;
771	if (mac_prepare_process_label(&proclabel) == -1) {
772		perror("mac_prepare_process_label");
773		goto out;
774	}
775	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
776	if (error == 0) {
777		if (mac_to_text(proclabel, &string) == -1)
778			string = NULL;
779	}
780	mac_free(proclabel);
781out:
782	if (string != NULL) {
783		(void)printf("%-*s", v->width, string);
784		free(string);
785	} else
786		(void)printf("%-*s", v->width, "  -");
787	return;
788}
789
790int
791s_label(KINFO *k)
792{
793	char *string = NULL;
794	mac_t proclabel;
795	int error, size = 0;
796
797	if (mac_prepare_process_label(&proclabel) == -1) {
798		perror("mac_prepare_process_label");
799		return (0);
800	}
801	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
802	if (error == 0 && mac_to_text(proclabel, &string) == 0) {
803		size = strlen(string);
804		free(string);
805	}
806	mac_free(proclabel);
807	return (size);
808}
809