libprocstat.c revision 222053
1/*-
2 * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
3 * Copyright (c) 1988, 1993
4 *      The Regents of the University of California.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *      This product includes software developed by the University of
17 *      California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/lib/libprocstat/libprocstat.c 222053 2011-05-18 10:04:54Z pluknet $");
37
38#include <sys/param.h>
39#include <sys/time.h>
40#include <sys/proc.h>
41#include <sys/user.h>
42#include <sys/stat.h>
43#include <sys/vnode.h>
44#include <sys/socket.h>
45#include <sys/socketvar.h>
46#include <sys/domain.h>
47#include <sys/protosw.h>
48#include <sys/un.h>
49#include <sys/unpcb.h>
50#include <sys/sysctl.h>
51#include <sys/tty.h>
52#include <sys/filedesc.h>
53#include <sys/queue.h>
54#define	_WANT_FILE
55#include <sys/file.h>
56#include <sys/conf.h>
57#define	_KERNEL
58#include <sys/mount.h>
59#include <sys/pipe.h>
60#include <ufs/ufs/quota.h>
61#include <ufs/ufs/inode.h>
62#include <fs/devfs/devfs.h>
63#include <fs/devfs/devfs_int.h>
64#undef _KERNEL
65#include <nfs/nfsproto.h>
66#include <nfsclient/nfs.h>
67#include <nfsclient/nfsnode.h>
68
69#include <vm/vm.h>
70#include <vm/vm_map.h>
71#include <vm/vm_object.h>
72
73#include <net/route.h>
74#include <netinet/in.h>
75#include <netinet/in_systm.h>
76#include <netinet/ip.h>
77#include <netinet/in_pcb.h>
78
79#include <assert.h>
80#include <ctype.h>
81#include <err.h>
82#include <fcntl.h>
83#include <kvm.h>
84#include <libutil.h>
85#include <limits.h>
86#include <paths.h>
87#include <pwd.h>
88#include <stdio.h>
89#include <stdlib.h>
90#include <stddef.h>
91#include <string.h>
92#include <unistd.h>
93#include <netdb.h>
94
95#include <libprocstat.h>
96#include "libprocstat_internal.h"
97#include "common_kvm.h"
98
99int     statfs(const char *, struct statfs *);	/* XXX */
100
101#define	PROCSTAT_KVM	1
102#define	PROCSTAT_SYSCTL	2
103
104static char	*getmnton(kvm_t *kd, struct mount *m);
105static struct filestat_list	*procstat_getfiles_kvm(
106    struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
107static struct filestat_list	*procstat_getfiles_sysctl(
108    struct procstat *procstat, struct kinfo_proc *kp, int mmapped);
109static int	procstat_get_pipe_info_sysctl(struct filestat *fst,
110    struct pipestat *pipe, char *errbuf);
111static int	procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
112    struct pipestat *pipe, char *errbuf);
113static int	procstat_get_pts_info_sysctl(struct filestat *fst,
114    struct ptsstat *pts, char *errbuf);
115static int	procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
116    struct ptsstat *pts, char *errbuf);
117static int	procstat_get_socket_info_sysctl(struct filestat *fst,
118    struct sockstat *sock, char *errbuf);
119static int	procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
120    struct sockstat *sock, char *errbuf);
121static int	to_filestat_flags(int flags);
122static int	procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
123    struct vnstat *vn, char *errbuf);
124static int	procstat_get_vnode_info_sysctl(struct filestat *fst,
125    struct vnstat *vn, char *errbuf);
126static int	vntype2psfsttype(int type);
127
128void
129procstat_close(struct procstat *procstat)
130{
131
132	assert(procstat);
133	if (procstat->type == PROCSTAT_KVM)
134		kvm_close(procstat->kd);
135	free(procstat);
136}
137
138struct procstat *
139procstat_open_sysctl(void)
140{
141	struct procstat *procstat;
142
143	procstat = calloc(1, sizeof(*procstat));
144	if (procstat == NULL) {
145		warn("malloc()");
146		return (NULL);
147	}
148	procstat->type = PROCSTAT_SYSCTL;
149	return (procstat);
150}
151
152struct procstat *
153procstat_open_kvm(const char *nlistf, const char *memf)
154{
155	struct procstat *procstat;
156	kvm_t *kd;
157	char buf[_POSIX2_LINE_MAX];
158
159	procstat = calloc(1, sizeof(*procstat));
160	if (procstat == NULL) {
161		warn("malloc()");
162		return (NULL);
163	}
164	kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
165	if (kd == NULL) {
166		warnx("kvm_openfiles(): %s", buf);
167		free(procstat);
168		return (NULL);
169	}
170	procstat->type = PROCSTAT_KVM;
171	procstat->kd = kd;
172	return (procstat);
173}
174
175struct kinfo_proc *
176procstat_getprocs(struct procstat *procstat, int what, int arg,
177    unsigned int *count)
178{
179	struct kinfo_proc *p0, *p;
180	size_t len;
181	int name[4];
182	int error;
183
184	assert(procstat);
185	assert(count);
186	p = NULL;
187	if (procstat->type == PROCSTAT_KVM) {
188		p0 = kvm_getprocs(procstat->kd, what, arg, count);
189		if (p0 == NULL || count == 0)
190			return (NULL);
191		len = *count * sizeof(*p);
192		p = malloc(len);
193		if (p == NULL) {
194			warnx("malloc(%zd)", len);
195			goto fail;
196		}
197		bcopy(p0, p, len);
198		return (p);
199	} else if (procstat->type == PROCSTAT_SYSCTL) {
200		len = 0;
201		name[0] = CTL_KERN;
202		name[1] = KERN_PROC;
203		name[2] = what;
204		name[3] = arg;
205		error = sysctl(name, 4, NULL, &len, NULL, 0);
206		if (error < 0 && errno != EPERM) {
207			warn("sysctl(kern.proc)");
208			goto fail;
209		}
210		if (len == 0) {
211			warnx("no processes?");
212			goto fail;
213		}
214		p = malloc(len);
215		if (p == NULL) {
216			warnx("malloc(%zd)", len);
217			goto fail;
218		}
219		error = sysctl(name, 4, p, &len, NULL, 0);
220		if (error < 0 && errno != EPERM) {
221			warn("sysctl(kern.proc)");
222			goto fail;
223		}
224		/* Perform simple consistency checks. */
225		if ((len % sizeof(*p)) != 0 || p->ki_structsize != sizeof(*p)) {
226			warnx("kinfo_proc structure size mismatch");
227			goto fail;
228		}
229		*count = len / sizeof(*p);
230		return (p);
231	} else {
232		warnx("unknown access method");
233		return (NULL);
234	}
235fail:
236	if (p)
237		free(p);
238	return (NULL);
239}
240
241void
242procstat_freeprocs(struct procstat *procstat __unused, struct kinfo_proc *p)
243{
244
245	if (p != NULL)
246		free(p);
247	p = NULL;
248}
249
250struct filestat_list *
251procstat_getfiles(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
252{
253
254	if (procstat->type == PROCSTAT_SYSCTL)
255		return (procstat_getfiles_sysctl(procstat, kp, mmapped));
256	else if (procstat->type == PROCSTAT_KVM)
257		return (procstat_getfiles_kvm(procstat, kp, mmapped));
258	else
259		return (NULL);
260}
261
262void
263procstat_freefiles(struct procstat *procstat, struct filestat_list *head)
264{
265	struct filestat *fst, *tmp;
266
267	STAILQ_FOREACH_SAFE(fst, head, next, tmp) {
268		if (fst->fs_path != NULL)
269			free(fst->fs_path);
270		free(fst);
271	}
272	free(head);
273	if (procstat->vmentries != NULL) {
274		free (procstat->vmentries);
275		procstat->vmentries = NULL;
276	}
277	if (procstat->files != NULL) {
278		free (procstat->files);
279		procstat->files = NULL;
280	}
281}
282
283static struct filestat *
284filestat_new_entry(void *typedep, int type, int fd, int fflags, int uflags,
285    int refcount, off_t offset, char *path)
286{
287	struct filestat *entry;
288
289	entry = calloc(1, sizeof(*entry));
290	if (entry == NULL) {
291		warn("malloc()");
292		return (NULL);
293	}
294	entry->fs_typedep = typedep;
295	entry->fs_fflags = fflags;
296	entry->fs_uflags = uflags;
297	entry->fs_fd = fd;
298	entry->fs_type = type;
299	entry->fs_ref_count = refcount;
300	entry->fs_offset = offset;
301	entry->fs_path = path;
302	return (entry);
303}
304
305static struct vnode *
306getctty(kvm_t *kd, struct kinfo_proc *kp)
307{
308	struct pgrp pgrp;
309	struct proc proc;
310	struct session sess;
311	int error;
312
313	assert(kp);
314	error = kvm_read_all(kd, (unsigned long)kp->ki_paddr, &proc,
315	    sizeof(proc));
316	if (error == 0) {
317		warnx("can't read proc struct at %p for pid %d",
318		    kp->ki_paddr, kp->ki_pid);
319		return (NULL);
320	}
321	if (proc.p_pgrp == NULL)
322		return (NULL);
323	error = kvm_read_all(kd, (unsigned long)proc.p_pgrp, &pgrp,
324	    sizeof(pgrp));
325	if (error == 0) {
326		warnx("can't read pgrp struct at %p for pid %d",
327		    proc.p_pgrp, kp->ki_pid);
328		return (NULL);
329	}
330	error = kvm_read_all(kd, (unsigned long)pgrp.pg_session, &sess,
331	    sizeof(sess));
332	if (error == 0) {
333		warnx("can't read session struct at %p for pid %d",
334		    pgrp.pg_session, kp->ki_pid);
335		return (NULL);
336	}
337	return (sess.s_ttyvp);
338}
339
340static struct filestat_list *
341procstat_getfiles_kvm(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
342{
343	struct file file;
344	struct filedesc filed;
345	struct vm_map_entry vmentry;
346	struct vm_object object;
347	struct vmspace vmspace;
348	vm_map_entry_t entryp;
349	vm_map_t map;
350	vm_object_t objp;
351	struct vnode *vp;
352	struct file **ofiles;
353	struct filestat *entry;
354	struct filestat_list *head;
355	kvm_t *kd;
356	void *data;
357	int i, fflags;
358	int prot, type;
359	unsigned int nfiles;
360
361	assert(procstat);
362	kd = procstat->kd;
363	if (kd == NULL)
364		return (NULL);
365	if (kp->ki_fd == NULL)
366		return (NULL);
367	if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &filed,
368	    sizeof(filed))) {
369		warnx("can't read filedesc at %p", (void *)kp->ki_fd);
370		return (NULL);
371	}
372
373	/*
374	 * Allocate list head.
375	 */
376	head = malloc(sizeof(*head));
377	if (head == NULL)
378		return (NULL);
379	STAILQ_INIT(head);
380
381	/* root directory vnode, if one. */
382	if (filed.fd_rdir) {
383		entry = filestat_new_entry(filed.fd_rdir, PS_FST_TYPE_VNODE, -1,
384		    PS_FST_FFLAG_READ, PS_FST_UFLAG_RDIR, 0, 0, NULL);
385		if (entry != NULL)
386			STAILQ_INSERT_TAIL(head, entry, next);
387	}
388	/* current working directory vnode. */
389	if (filed.fd_cdir) {
390		entry = filestat_new_entry(filed.fd_cdir, PS_FST_TYPE_VNODE, -1,
391		    PS_FST_FFLAG_READ, PS_FST_UFLAG_CDIR, 0, 0, NULL);
392		if (entry != NULL)
393			STAILQ_INSERT_TAIL(head, entry, next);
394	}
395	/* jail root, if any. */
396	if (filed.fd_jdir) {
397		entry = filestat_new_entry(filed.fd_jdir, PS_FST_TYPE_VNODE, -1,
398		    PS_FST_FFLAG_READ, PS_FST_UFLAG_JAIL, 0, 0, NULL);
399		if (entry != NULL)
400			STAILQ_INSERT_TAIL(head, entry, next);
401	}
402	/* ktrace vnode, if one */
403	if (kp->ki_tracep) {
404		entry = filestat_new_entry(kp->ki_tracep, PS_FST_TYPE_VNODE, -1,
405		    PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
406		    PS_FST_UFLAG_TRACE, 0, 0, NULL);
407		if (entry != NULL)
408			STAILQ_INSERT_TAIL(head, entry, next);
409	}
410	/* text vnode, if one */
411	if (kp->ki_textvp) {
412		entry = filestat_new_entry(kp->ki_textvp, PS_FST_TYPE_VNODE, -1,
413		    PS_FST_FFLAG_READ, PS_FST_UFLAG_TEXT, 0, 0, NULL);
414		if (entry != NULL)
415			STAILQ_INSERT_TAIL(head, entry, next);
416	}
417	/* Controlling terminal. */
418	if ((vp = getctty(kd, kp)) != NULL) {
419		entry = filestat_new_entry(vp, PS_FST_TYPE_VNODE, -1,
420		    PS_FST_FFLAG_READ | PS_FST_FFLAG_WRITE,
421		    PS_FST_UFLAG_CTTY, 0, 0, NULL);
422		if (entry != NULL)
423			STAILQ_INSERT_TAIL(head, entry, next);
424	}
425
426	nfiles = filed.fd_lastfile + 1;
427	ofiles = malloc(nfiles * sizeof(struct file *));
428	if (ofiles == NULL) {
429		warn("malloc(%zd)", nfiles * sizeof(struct file *));
430		goto do_mmapped;
431	}
432	if (!kvm_read_all(kd, (unsigned long)filed.fd_ofiles, ofiles,
433	    nfiles * sizeof(struct file *))) {
434		warnx("cannot read file structures at %p",
435		    (void *)filed.fd_ofiles);
436		free(ofiles);
437		goto do_mmapped;
438	}
439	for (i = 0; i <= filed.fd_lastfile; i++) {
440		if (ofiles[i] == NULL)
441			continue;
442		if (!kvm_read_all(kd, (unsigned long)ofiles[i], &file,
443		    sizeof(struct file))) {
444			warnx("can't read file %d at %p", i,
445			    (void *)ofiles[i]);
446			continue;
447		}
448		switch (file.f_type) {
449		case DTYPE_VNODE:
450			type = PS_FST_TYPE_VNODE;
451			data = file.f_vnode;
452			break;
453		case DTYPE_SOCKET:
454			type = PS_FST_TYPE_SOCKET;
455			data = file.f_data;
456			break;
457		case DTYPE_PIPE:
458			type = PS_FST_TYPE_PIPE;
459			data = file.f_data;
460			break;
461		case DTYPE_FIFO:
462			type = PS_FST_TYPE_FIFO;
463			data = file.f_vnode;
464			break;
465#ifdef DTYPE_PTS
466		case DTYPE_PTS:
467			type = PS_FST_TYPE_PTS;
468			data = file.f_data;
469			break;
470#endif
471		default:
472			continue;
473		}
474		entry = filestat_new_entry(data, type, i,
475		    to_filestat_flags(file.f_flag), 0, 0, 0, NULL);
476		if (entry != NULL)
477			STAILQ_INSERT_TAIL(head, entry, next);
478	}
479	free(ofiles);
480
481do_mmapped:
482
483	/*
484	 * Process mmapped files if requested.
485	 */
486	if (mmapped) {
487		if (!kvm_read_all(kd, (unsigned long)kp->ki_vmspace, &vmspace,
488		    sizeof(vmspace))) {
489			warnx("can't read vmspace at %p",
490			    (void *)kp->ki_vmspace);
491			goto exit;
492		}
493		map = &vmspace.vm_map;
494
495		for (entryp = map->header.next;
496		    entryp != &kp->ki_vmspace->vm_map.header;
497		    entryp = vmentry.next) {
498			if (!kvm_read_all(kd, (unsigned long)entryp, &vmentry,
499			    sizeof(vmentry))) {
500				warnx("can't read vm_map_entry at %p",
501				    (void *)entryp);
502				continue;
503			}
504			if (vmentry.eflags & MAP_ENTRY_IS_SUB_MAP)
505				continue;
506			if ((objp = vmentry.object.vm_object) == NULL)
507				continue;
508			for (; objp; objp = object.backing_object) {
509				if (!kvm_read_all(kd, (unsigned long)objp,
510				    &object, sizeof(object))) {
511					warnx("can't read vm_object at %p",
512					    (void *)objp);
513					break;
514				}
515			}
516
517			/* We want only vnode objects. */
518			if (object.type != OBJT_VNODE)
519				continue;
520
521			prot = vmentry.protection;
522			fflags = 0;
523			if (prot & VM_PROT_READ)
524				fflags = PS_FST_FFLAG_READ;
525			if (prot & VM_PROT_WRITE)
526				fflags |= PS_FST_FFLAG_WRITE;
527
528			/*
529			 * Create filestat entry.
530			 */
531			entry = filestat_new_entry(object.handle,
532			    PS_FST_TYPE_VNODE, -1, fflags,
533			    PS_FST_UFLAG_MMAP, 0, 0, NULL);
534			if (entry != NULL)
535				STAILQ_INSERT_TAIL(head, entry, next);
536		}
537	}
538exit:
539	return (head);
540}
541
542/*
543 * kinfo types to filestat translation.
544 */
545static int
546kinfo_type2fst(int kftype)
547{
548	static struct {
549		int	kf_type;
550		int	fst_type;
551	} kftypes2fst[] = {
552		{ KF_TYPE_CRYPTO, PS_FST_TYPE_CRYPTO },
553		{ KF_TYPE_FIFO, PS_FST_TYPE_FIFO },
554		{ KF_TYPE_KQUEUE, PS_FST_TYPE_KQUEUE },
555		{ KF_TYPE_MQUEUE, PS_FST_TYPE_MQUEUE },
556		{ KF_TYPE_NONE, PS_FST_TYPE_NONE },
557		{ KF_TYPE_PIPE, PS_FST_TYPE_PIPE },
558		{ KF_TYPE_PTS, PS_FST_TYPE_PTS },
559		{ KF_TYPE_SEM, PS_FST_TYPE_SEM },
560		{ KF_TYPE_SHM, PS_FST_TYPE_SHM },
561		{ KF_TYPE_SOCKET, PS_FST_TYPE_SOCKET },
562		{ KF_TYPE_VNODE, PS_FST_TYPE_VNODE },
563		{ KF_TYPE_UNKNOWN, PS_FST_TYPE_UNKNOWN }
564	};
565#define NKFTYPES	(sizeof(kftypes2fst) / sizeof(*kftypes2fst))
566	unsigned int i;
567
568	for (i = 0; i < NKFTYPES; i++)
569		if (kftypes2fst[i].kf_type == kftype)
570			break;
571	if (i == NKFTYPES)
572		return (PS_FST_TYPE_UNKNOWN);
573	return (kftypes2fst[i].fst_type);
574}
575
576/*
577 * kinfo flags to filestat translation.
578 */
579static int
580kinfo_fflags2fst(int kfflags)
581{
582	static struct {
583		int	kf_flag;
584		int	fst_flag;
585	} kfflags2fst[] = {
586		{ KF_FLAG_APPEND, PS_FST_FFLAG_APPEND },
587		{ KF_FLAG_ASYNC, PS_FST_FFLAG_ASYNC },
588		{ KF_FLAG_CREAT, PS_FST_FFLAG_CREAT },
589		{ KF_FLAG_DIRECT, PS_FST_FFLAG_DIRECT },
590		{ KF_FLAG_EXCL, PS_FST_FFLAG_EXCL },
591		{ KF_FLAG_EXEC, PS_FST_FFLAG_EXEC },
592		{ KF_FLAG_EXLOCK, PS_FST_FFLAG_EXLOCK },
593		{ KF_FLAG_FSYNC, PS_FST_FFLAG_SYNC },
594		{ KF_FLAG_HASLOCK, PS_FST_FFLAG_HASLOCK },
595		{ KF_FLAG_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
596		{ KF_FLAG_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
597		{ KF_FLAG_READ, PS_FST_FFLAG_READ },
598		{ KF_FLAG_SHLOCK, PS_FST_FFLAG_SHLOCK },
599		{ KF_FLAG_TRUNC, PS_FST_FFLAG_TRUNC },
600		{ KF_FLAG_WRITE, PS_FST_FFLAG_WRITE }
601	};
602#define NKFFLAGS	(sizeof(kfflags2fst) / sizeof(*kfflags2fst))
603	unsigned int i;
604	int flags;
605
606	flags = 0;
607	for (i = 0; i < NKFFLAGS; i++)
608		if ((kfflags & kfflags2fst[i].kf_flag) != 0)
609			flags |= kfflags2fst[i].fst_flag;
610	return (flags);
611}
612
613static int
614kinfo_uflags2fst(int fd)
615{
616
617	switch (fd) {
618	case KF_FD_TYPE_CTTY:
619		return (PS_FST_UFLAG_CTTY);
620	case KF_FD_TYPE_CWD:
621		return (PS_FST_UFLAG_CDIR);
622	case KF_FD_TYPE_JAIL:
623		return (PS_FST_UFLAG_JAIL);
624	case KF_FD_TYPE_TEXT:
625		return (PS_FST_UFLAG_TEXT);
626	case KF_FD_TYPE_TRACE:
627		return (PS_FST_UFLAG_TRACE);
628	case KF_FD_TYPE_ROOT:
629		return (PS_FST_UFLAG_RDIR);
630	}
631	return (0);
632}
633
634static struct filestat_list *
635procstat_getfiles_sysctl(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
636{
637	struct kinfo_file *kif, *files;
638	struct kinfo_vmentry *kve, *vmentries;
639	struct filestat_list *head;
640	struct filestat *entry;
641	char *path;
642	off_t offset;
643	int cnt, fd, fflags;
644	int i, type, uflags;
645	int refcount;
646
647	assert(kp);
648	if (kp->ki_fd == NULL)
649		return (NULL);
650
651	files = kinfo_getfile(kp->ki_pid, &cnt);
652	if (files == NULL && errno != EPERM) {
653		warn("kinfo_getfile()");
654		return (NULL);
655	}
656	procstat->files = files;
657
658	/*
659	 * Allocate list head.
660	 */
661	head = malloc(sizeof(*head));
662	if (head == NULL)
663		return (NULL);
664	STAILQ_INIT(head);
665	for (i = 0; i < cnt; i++) {
666		kif = &files[i];
667
668		type = kinfo_type2fst(kif->kf_type);
669		fd = kif->kf_fd >= 0 ? kif->kf_fd : -1;
670		fflags = kinfo_fflags2fst(kif->kf_flags);
671		uflags = kinfo_uflags2fst(kif->kf_fd);
672		refcount = kif->kf_ref_count;
673		offset = kif->kf_offset;
674		if (*kif->kf_path != '\0')
675			path = strdup(kif->kf_path);
676		else
677			path = NULL;
678
679		/*
680		 * Create filestat entry.
681		 */
682		entry = filestat_new_entry(kif, type, fd, fflags, uflags,
683		    refcount, offset, path);
684		if (entry != NULL)
685			STAILQ_INSERT_TAIL(head, entry, next);
686	}
687	if (mmapped != 0) {
688		vmentries = kinfo_getvmmap(kp->ki_pid, &cnt);
689		procstat->vmentries = vmentries;
690		if (vmentries == NULL || cnt == 0)
691			goto fail;
692		for (i = 0; i < cnt; i++) {
693			kve = &vmentries[i];
694			if (kve->kve_type != KVME_TYPE_VNODE)
695				continue;
696			fflags = 0;
697			if (kve->kve_protection & KVME_PROT_READ)
698				fflags = PS_FST_FFLAG_READ;
699			if (kve->kve_protection & KVME_PROT_WRITE)
700				fflags |= PS_FST_FFLAG_WRITE;
701			offset = kve->kve_offset;
702			refcount = kve->kve_ref_count;
703			if (*kve->kve_path != '\0')
704				path = strdup(kve->kve_path);
705			else
706				path = NULL;
707			entry = filestat_new_entry(kve, PS_FST_TYPE_VNODE, -1,
708			    fflags, PS_FST_UFLAG_MMAP, refcount, offset, path);
709			if (entry != NULL)
710				STAILQ_INSERT_TAIL(head, entry, next);
711		}
712	}
713fail:
714	return (head);
715}
716
717int
718procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
719    struct pipestat *ps, char *errbuf)
720{
721
722	assert(ps);
723	if (procstat->type == PROCSTAT_KVM) {
724		return (procstat_get_pipe_info_kvm(procstat->kd, fst, ps,
725		    errbuf));
726	} else if (procstat->type == PROCSTAT_SYSCTL) {
727		return (procstat_get_pipe_info_sysctl(fst, ps, errbuf));
728	} else {
729		warnx("unknow access method: %d", procstat->type);
730		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
731		return (1);
732	}
733}
734
735static int
736procstat_get_pipe_info_kvm(kvm_t *kd, struct filestat *fst,
737    struct pipestat *ps, char *errbuf)
738{
739	struct pipe pi;
740	void *pipep;
741
742	assert(kd);
743	assert(ps);
744	assert(fst);
745	bzero(ps, sizeof(*ps));
746	pipep = fst->fs_typedep;
747	if (pipep == NULL)
748		goto fail;
749	if (!kvm_read_all(kd, (unsigned long)pipep, &pi, sizeof(struct pipe))) {
750		warnx("can't read pipe at %p", (void *)pipep);
751		goto fail;
752	}
753	ps->addr = (uintptr_t)pipep;
754	ps->peer = (uintptr_t)pi.pipe_peer;
755	ps->buffer_cnt = pi.pipe_buffer.cnt;
756	return (0);
757
758fail:
759	snprintf(errbuf, _POSIX2_LINE_MAX, "error");
760	return (1);
761}
762
763static int
764procstat_get_pipe_info_sysctl(struct filestat *fst, struct pipestat *ps,
765    char *errbuf __unused)
766{
767	struct kinfo_file *kif;
768
769	assert(ps);
770	assert(fst);
771	bzero(ps, sizeof(*ps));
772	kif = fst->fs_typedep;
773	if (kif == NULL)
774		return (1);
775	ps->addr = kif->kf_un.kf_pipe.kf_pipe_addr;
776	ps->peer = kif->kf_un.kf_pipe.kf_pipe_peer;
777	ps->buffer_cnt = kif->kf_un.kf_pipe.kf_pipe_buffer_cnt;
778	return (0);
779}
780
781int
782procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
783    struct ptsstat *pts, char *errbuf)
784{
785
786	assert(pts);
787	if (procstat->type == PROCSTAT_KVM) {
788		return (procstat_get_pts_info_kvm(procstat->kd, fst, pts,
789		    errbuf));
790	} else if (procstat->type == PROCSTAT_SYSCTL) {
791		return (procstat_get_pts_info_sysctl(fst, pts, errbuf));
792	} else {
793		warnx("unknow access method: %d", procstat->type);
794		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
795		return (1);
796	}
797}
798
799static int
800procstat_get_pts_info_kvm(kvm_t *kd, struct filestat *fst,
801    struct ptsstat *pts, char *errbuf)
802{
803	struct tty tty;
804	void *ttyp;
805
806	assert(kd);
807	assert(pts);
808	assert(fst);
809	bzero(pts, sizeof(*pts));
810	ttyp = fst->fs_typedep;
811	if (ttyp == NULL)
812		goto fail;
813	if (!kvm_read_all(kd, (unsigned long)ttyp, &tty, sizeof(struct tty))) {
814		warnx("can't read tty at %p", (void *)ttyp);
815		goto fail;
816	}
817	pts->dev = dev2udev(kd, tty.t_dev);
818	(void)kdevtoname(kd, tty.t_dev, pts->devname);
819	return (0);
820
821fail:
822	snprintf(errbuf, _POSIX2_LINE_MAX, "error");
823	return (1);
824}
825
826static int
827procstat_get_pts_info_sysctl(struct filestat *fst, struct ptsstat *pts,
828    char *errbuf __unused)
829{
830	struct kinfo_file *kif;
831
832	assert(pts);
833	assert(fst);
834	bzero(pts, sizeof(*pts));
835	kif = fst->fs_typedep;
836	if (kif == NULL)
837		return (0);
838	pts->dev = kif->kf_un.kf_pts.kf_pts_dev;
839	strlcpy(pts->devname, kif->kf_path, sizeof(pts->devname));
840	return (0);
841}
842
843int
844procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
845    struct vnstat *vn, char *errbuf)
846{
847
848	assert(vn);
849	if (procstat->type == PROCSTAT_KVM) {
850		return (procstat_get_vnode_info_kvm(procstat->kd, fst, vn,
851		    errbuf));
852	} else if (procstat->type == PROCSTAT_SYSCTL) {
853		return (procstat_get_vnode_info_sysctl(fst, vn, errbuf));
854	} else {
855		warnx("unknow access method: %d", procstat->type);
856		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
857		return (1);
858	}
859}
860
861static int
862procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
863    struct vnstat *vn, char *errbuf)
864{
865	/* Filesystem specific handlers. */
866	#define FSTYPE(fst)     {#fst, fst##_filestat}
867	struct {
868		const char	*tag;
869		int		(*handler)(kvm_t *kd, struct vnode *vp,
870		    struct vnstat *vn);
871	} fstypes[] = {
872		FSTYPE(devfs),
873		FSTYPE(isofs),
874		FSTYPE(msdosfs),
875		FSTYPE(nfs),
876		FSTYPE(ntfs),
877#ifdef LIBPROCSTAT_NWFS
878		FSTYPE(nwfs),
879#endif
880		FSTYPE(smbfs),
881		FSTYPE(udf),
882		FSTYPE(ufs),
883#ifdef LIBPROCSTAT_ZFS
884		FSTYPE(zfs),
885#endif
886	};
887#define	NTYPES	(sizeof(fstypes) / sizeof(*fstypes))
888	struct vnode vnode;
889	char tagstr[12];
890	void *vp;
891	int error, found;
892	unsigned int i;
893
894	assert(kd);
895	assert(vn);
896	assert(fst);
897	vp = fst->fs_typedep;
898	if (vp == NULL)
899		goto fail;
900	error = kvm_read_all(kd, (unsigned long)vp, &vnode, sizeof(vnode));
901	if (error == 0) {
902		warnx("can't read vnode at %p", (void *)vp);
903		goto fail;
904	}
905	bzero(vn, sizeof(*vn));
906	vn->vn_type = vntype2psfsttype(vnode.v_type);
907	if (vnode.v_type == VNON || vnode.v_type == VBAD)
908		return (0);
909	error = kvm_read_all(kd, (unsigned long)vnode.v_tag, tagstr,
910	    sizeof(tagstr));
911	if (error == 0) {
912		warnx("can't read v_tag at %p", (void *)vp);
913		goto fail;
914	}
915	tagstr[sizeof(tagstr) - 1] = '\0';
916
917	/*
918	 * Find appropriate handler.
919	 */
920	for (i = 0, found = 0; i < NTYPES; i++)
921		if (!strcmp(fstypes[i].tag, tagstr)) {
922			if (fstypes[i].handler(kd, &vnode, vn) != 0) {
923				goto fail;
924			}
925			break;
926		}
927	if (i == NTYPES) {
928		snprintf(errbuf, _POSIX2_LINE_MAX, "?(%s)", tagstr);
929		return (1);
930	}
931	vn->vn_mntdir = getmnton(kd, vnode.v_mount);
932	if ((vnode.v_type == VBLK || vnode.v_type == VCHR) &&
933	    vnode.v_rdev != NULL){
934		vn->vn_dev = dev2udev(kd, vnode.v_rdev);
935		(void)kdevtoname(kd, vnode.v_rdev, vn->vn_devname);
936	} else {
937		vn->vn_dev = -1;
938	}
939	return (0);
940
941fail:
942	snprintf(errbuf, _POSIX2_LINE_MAX, "error");
943	return (1);
944}
945
946/*
947 * kinfo vnode type to filestat translation.
948 */
949static int
950kinfo_vtype2fst(int kfvtype)
951{
952	static struct {
953		int	kf_vtype;
954		int	fst_vtype;
955	} kfvtypes2fst[] = {
956		{ KF_VTYPE_VBAD, PS_FST_VTYPE_VBAD },
957		{ KF_VTYPE_VBLK, PS_FST_VTYPE_VBLK },
958		{ KF_VTYPE_VCHR, PS_FST_VTYPE_VCHR },
959		{ KF_VTYPE_VDIR, PS_FST_VTYPE_VDIR },
960		{ KF_VTYPE_VFIFO, PS_FST_VTYPE_VFIFO },
961		{ KF_VTYPE_VLNK, PS_FST_VTYPE_VLNK },
962		{ KF_VTYPE_VNON, PS_FST_VTYPE_VNON },
963		{ KF_VTYPE_VREG, PS_FST_VTYPE_VREG },
964		{ KF_VTYPE_VSOCK, PS_FST_VTYPE_VSOCK }
965	};
966#define	NKFVTYPES	(sizeof(kfvtypes2fst) / sizeof(*kfvtypes2fst))
967	unsigned int i;
968
969	for (i = 0; i < NKFVTYPES; i++)
970		if (kfvtypes2fst[i].kf_vtype == kfvtype)
971			break;
972	if (i == NKFVTYPES)
973		return (PS_FST_VTYPE_UNKNOWN);
974	return (kfvtypes2fst[i].fst_vtype);
975}
976
977static int
978procstat_get_vnode_info_sysctl(struct filestat *fst, struct vnstat *vn,
979    char *errbuf)
980{
981	struct statfs stbuf;
982	struct kinfo_file *kif;
983	struct kinfo_vmentry *kve;
984	uint64_t fileid;
985	uint64_t size;
986	char *name, *path;
987	uint32_t fsid;
988	uint16_t mode;
989	uint32_t rdev;
990	int vntype;
991	int status;
992
993	assert(fst);
994	assert(vn);
995	bzero(vn, sizeof(*vn));
996	if (fst->fs_typedep == NULL)
997		return (1);
998	if (fst->fs_uflags & PS_FST_UFLAG_MMAP) {
999		kve = fst->fs_typedep;
1000		fileid = kve->kve_vn_fileid;
1001		fsid = kve->kve_vn_fsid;
1002		mode = kve->kve_vn_mode;
1003		path = kve->kve_path;
1004		rdev = kve->kve_vn_rdev;
1005		size = kve->kve_vn_size;
1006		vntype = kinfo_vtype2fst(kve->kve_vn_type);
1007		status = kve->kve_status;
1008	} else {
1009		kif = fst->fs_typedep;
1010		fileid = kif->kf_un.kf_file.kf_file_fileid;
1011		fsid = kif->kf_un.kf_file.kf_file_fsid;
1012		mode = kif->kf_un.kf_file.kf_file_mode;
1013		path = kif->kf_path;
1014		rdev = kif->kf_un.kf_file.kf_file_rdev;
1015		size = kif->kf_un.kf_file.kf_file_size;
1016		vntype = kinfo_vtype2fst(kif->kf_vnode_type);
1017		status = kif->kf_status;
1018	}
1019	vn->vn_type = vntype;
1020	if (vntype == PS_FST_VTYPE_VNON || vntype == PS_FST_VTYPE_VBAD)
1021		return (0);
1022	if ((status & KF_ATTR_VALID) == 0) {
1023		snprintf(errbuf, _POSIX2_LINE_MAX, "? (no info available)");
1024		return (1);
1025	}
1026	if (path && *path) {
1027		statfs(path, &stbuf);
1028		vn->vn_mntdir = strdup(stbuf.f_mntonname);
1029	} else
1030		vn->vn_mntdir = strdup("-");
1031	vn->vn_dev = rdev;
1032	if (vntype == PS_FST_VTYPE_VBLK) {
1033		name = devname(rdev, S_IFBLK);
1034		if (name != NULL)
1035			strlcpy(vn->vn_devname, name,
1036			    sizeof(vn->vn_devname));
1037	} else if (vntype == PS_FST_VTYPE_VCHR) {
1038		name = devname(vn->vn_dev, S_IFCHR);
1039		if (name != NULL)
1040			strlcpy(vn->vn_devname, name,
1041			    sizeof(vn->vn_devname));
1042	}
1043	vn->vn_fsid = fsid;
1044	vn->vn_fileid = fileid;
1045	vn->vn_size = size;
1046	vn->vn_mode = mode;
1047	return (0);
1048}
1049
1050int
1051procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
1052    struct sockstat *sock, char *errbuf)
1053{
1054
1055	assert(sock);
1056	if (procstat->type == PROCSTAT_KVM) {
1057		return (procstat_get_socket_info_kvm(procstat->kd, fst, sock,
1058		    errbuf));
1059	} else if (procstat->type == PROCSTAT_SYSCTL) {
1060		return (procstat_get_socket_info_sysctl(fst, sock, errbuf));
1061	} else {
1062		warnx("unknow access method: %d", procstat->type);
1063		snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1064		return (1);
1065	}
1066}
1067
1068static int
1069procstat_get_socket_info_kvm(kvm_t *kd, struct filestat *fst,
1070    struct sockstat *sock, char *errbuf)
1071{
1072	struct domain dom;
1073	struct inpcb inpcb;
1074	struct protosw proto;
1075	struct socket s;
1076	struct unpcb unpcb;
1077	ssize_t len;
1078	void *so;
1079
1080	assert(kd);
1081	assert(sock);
1082	assert(fst);
1083	bzero(sock, sizeof(*sock));
1084	so = fst->fs_typedep;
1085	if (so == NULL)
1086		goto fail;
1087	sock->so_addr = (uintptr_t)so;
1088	/* fill in socket */
1089	if (!kvm_read_all(kd, (unsigned long)so, &s,
1090	    sizeof(struct socket))) {
1091		warnx("can't read sock at %p", (void *)so);
1092		goto fail;
1093	}
1094	/* fill in protosw entry */
1095	if (!kvm_read_all(kd, (unsigned long)s.so_proto, &proto,
1096	    sizeof(struct protosw))) {
1097		warnx("can't read protosw at %p", (void *)s.so_proto);
1098		goto fail;
1099	}
1100	/* fill in domain */
1101	if (!kvm_read_all(kd, (unsigned long)proto.pr_domain, &dom,
1102	    sizeof(struct domain))) {
1103		warnx("can't read domain at %p",
1104		    (void *)proto.pr_domain);
1105		goto fail;
1106	}
1107	if ((len = kvm_read(kd, (unsigned long)dom.dom_name, sock->dname,
1108	    sizeof(sock->dname) - 1)) < 0) {
1109		warnx("can't read domain name at %p", (void *)dom.dom_name);
1110		sock->dname[0] = '\0';
1111	}
1112	else
1113		sock->dname[len] = '\0';
1114
1115	/*
1116	 * Fill in known data.
1117	 */
1118	sock->type = s.so_type;
1119	sock->proto = proto.pr_protocol;
1120	sock->dom_family = dom.dom_family;
1121	sock->so_pcb = (uintptr_t)s.so_pcb;
1122
1123	/*
1124	 * Protocol specific data.
1125	 */
1126	switch(dom.dom_family) {
1127	case AF_INET:
1128	case AF_INET6:
1129		if (proto.pr_protocol == IPPROTO_TCP) {
1130			if (s.so_pcb) {
1131				if (kvm_read(kd, (u_long)s.so_pcb,
1132				    (char *)&inpcb, sizeof(struct inpcb))
1133				    != sizeof(struct inpcb)) {
1134					warnx("can't read inpcb at %p",
1135					    (void *)s.so_pcb);
1136				} else
1137					sock->inp_ppcb =
1138					    (uintptr_t)inpcb.inp_ppcb;
1139			}
1140		}
1141		break;
1142	case AF_UNIX:
1143		if (s.so_pcb) {
1144			if (kvm_read(kd, (u_long)s.so_pcb, (char *)&unpcb,
1145			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
1146				warnx("can't read unpcb at %p",
1147				    (void *)s.so_pcb);
1148			} else if (unpcb.unp_conn) {
1149				sock->so_rcv_sb_state = s.so_rcv.sb_state;
1150				sock->so_snd_sb_state = s.so_snd.sb_state;
1151				sock->unp_conn = (uintptr_t)unpcb.unp_conn;
1152			}
1153		}
1154		break;
1155	default:
1156		break;
1157	}
1158	return (0);
1159
1160fail:
1161	snprintf(errbuf, _POSIX2_LINE_MAX, "error");
1162	return (1);
1163}
1164
1165static int
1166procstat_get_socket_info_sysctl(struct filestat *fst, struct sockstat *sock,
1167    char *errbuf __unused)
1168{
1169	struct kinfo_file *kif;
1170
1171	assert(sock);
1172	assert(fst);
1173	bzero(sock, sizeof(*sock));
1174	kif = fst->fs_typedep;
1175	if (kif == NULL)
1176		return (0);
1177
1178	/*
1179	 * Fill in known data.
1180	 */
1181	sock->type = kif->kf_sock_type;
1182	sock->proto = kif->kf_sock_protocol;
1183	sock->dom_family = kif->kf_sock_domain;
1184	sock->so_pcb = kif->kf_un.kf_sock.kf_sock_pcb;
1185	strlcpy(sock->dname, kif->kf_path, sizeof(sock->dname));
1186	bcopy(&kif->kf_sa_local, &sock->sa_local, kif->kf_sa_local.ss_len);
1187	bcopy(&kif->kf_sa_peer, &sock->sa_peer, kif->kf_sa_peer.ss_len);
1188
1189	/*
1190	 * Protocol specific data.
1191	 */
1192	switch(sock->dom_family) {
1193	case AF_INET:
1194	case AF_INET6:
1195		if (sock->proto == IPPROTO_TCP)
1196			sock->inp_ppcb = kif->kf_un.kf_sock.kf_sock_inpcb;
1197		break;
1198	case AF_UNIX:
1199		if (kif->kf_un.kf_sock.kf_sock_unpconn != 0) {
1200				sock->so_rcv_sb_state =
1201				    kif->kf_un.kf_sock.kf_sock_rcv_sb_state;
1202				sock->so_snd_sb_state =
1203				    kif->kf_un.kf_sock.kf_sock_snd_sb_state;
1204				sock->unp_conn =
1205				    kif->kf_un.kf_sock.kf_sock_unpconn;
1206		}
1207		break;
1208	default:
1209		break;
1210	}
1211	return (0);
1212}
1213
1214/*
1215 * Descriptor flags to filestat translation.
1216 */
1217static int
1218to_filestat_flags(int flags)
1219{
1220	static struct {
1221		int flag;
1222		int fst_flag;
1223	} fstflags[] = {
1224		{ FREAD, PS_FST_FFLAG_READ },
1225		{ FWRITE, PS_FST_FFLAG_WRITE },
1226		{ O_APPEND, PS_FST_FFLAG_APPEND },
1227		{ O_ASYNC, PS_FST_FFLAG_ASYNC },
1228		{ O_CREAT, PS_FST_FFLAG_CREAT },
1229		{ O_DIRECT, PS_FST_FFLAG_DIRECT },
1230		{ O_EXCL, PS_FST_FFLAG_EXCL },
1231		{ O_EXEC, PS_FST_FFLAG_EXEC },
1232		{ O_EXLOCK, PS_FST_FFLAG_EXLOCK },
1233		{ O_NOFOLLOW, PS_FST_FFLAG_NOFOLLOW },
1234		{ O_NONBLOCK, PS_FST_FFLAG_NONBLOCK },
1235		{ O_SHLOCK, PS_FST_FFLAG_SHLOCK },
1236		{ O_SYNC, PS_FST_FFLAG_SYNC },
1237		{ O_TRUNC, PS_FST_FFLAG_TRUNC }
1238	};
1239#define NFSTFLAGS	(sizeof(fstflags) / sizeof(*fstflags))
1240	int fst_flags;
1241	unsigned int i;
1242
1243	fst_flags = 0;
1244	for (i = 0; i < NFSTFLAGS; i++)
1245		if (flags & fstflags[i].flag)
1246			fst_flags |= fstflags[i].fst_flag;
1247	return (fst_flags);
1248}
1249
1250/*
1251 * Vnode type to filestate translation.
1252 */
1253static int
1254vntype2psfsttype(int type)
1255{
1256	static struct {
1257		int	vtype;
1258		int	fst_vtype;
1259	} vt2fst[] = {
1260		{ VBAD, PS_FST_VTYPE_VBAD },
1261		{ VBLK, PS_FST_VTYPE_VBLK },
1262		{ VCHR, PS_FST_VTYPE_VCHR },
1263		{ VDIR, PS_FST_VTYPE_VDIR },
1264		{ VFIFO, PS_FST_VTYPE_VFIFO },
1265		{ VLNK, PS_FST_VTYPE_VLNK },
1266		{ VNON, PS_FST_VTYPE_VNON },
1267		{ VREG, PS_FST_VTYPE_VREG },
1268		{ VSOCK, PS_FST_VTYPE_VSOCK }
1269	};
1270#define	NVFTYPES	(sizeof(vt2fst) / sizeof(*vt2fst))
1271	unsigned int i, fst_type;
1272
1273	fst_type = PS_FST_VTYPE_UNKNOWN;
1274	for (i = 0; i < NVFTYPES; i++) {
1275		if (type == vt2fst[i].vtype) {
1276			fst_type = vt2fst[i].fst_vtype;
1277			break;
1278		}
1279	}
1280	return (fst_type);
1281}
1282
1283static char *
1284getmnton(kvm_t *kd, struct mount *m)
1285{
1286	static struct mount mnt;
1287	static struct mtab {
1288		struct mtab *next;
1289		struct mount *m;
1290		char mntonname[MNAMELEN + 1];
1291	} *mhead = NULL;
1292	struct mtab *mt;
1293
1294	for (mt = mhead; mt != NULL; mt = mt->next)
1295		if (m == mt->m)
1296			return (mt->mntonname);
1297	if (!kvm_read_all(kd, (unsigned long)m, &mnt, sizeof(struct mount))) {
1298		warnx("can't read mount table at %p", (void *)m);
1299		return (NULL);
1300	}
1301	if ((mt = malloc(sizeof (struct mtab))) == NULL)
1302		err(1, NULL);
1303	mt->m = m;
1304	bcopy(&mnt.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
1305	mnt.mnt_stat.f_mntonname[MNAMELEN] = '\0';
1306	mt->next = mhead;
1307	mhead = mt;
1308	return (mt->mntonname);
1309}
1310