xref: /freebsd/lib/libprocstat/libprocstat_compat.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*-
2  * Copyright (c) 2014 Gleb Kurtsou <gleb@FreeBSD.org>
3  * 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/user.h>
34 #include <sys/socket.h>
35 #include <string.h>
36 
37 #include "libprocstat.h"
38 
39 struct freebsd11_ptsstat {
40 	uint32_t	dev;
41 	char		devname[SPECNAMELEN + 1];
42 };
43 
44 struct freebsd11_vnstat {
45 	uint64_t	vn_fileid;
46 	uint64_t	vn_size;
47 	char		*vn_mntdir;
48 	uint32_t	vn_dev;
49 	uint32_t	vn_fsid;
50 	int		vn_type;
51 	uint16_t	vn_mode;
52 	char		vn_devname[SPECNAMELEN + 1];
53 };
54 struct freebsd11_semstat {
55 	uint32_t	value;
56 	uint16_t	mode;
57 };
58 struct freebsd11_shmstat {
59 	uint64_t	size;
60 	uint16_t	mode;
61 };
62 
63 struct freebsd11_sockstat {
64 	uint64_t	inp_ppcb;
65 	uint64_t	so_addr;
66 	uint64_t	so_pcb;
67 	uint64_t	unp_conn;
68 	int		dom_family;
69 	int		proto;
70 	int		so_rcv_sb_state;
71 	int		so_snd_sb_state;
72 	struct sockaddr_storage	sa_local;	/* Socket address. */
73 	struct sockaddr_storage	sa_peer;	/* Peer address. */
74 	int		type;
75 	char		dname[32];
76 };
77 
78 int	freebsd11_procstat_get_pts_info(struct procstat *procstat,
79     struct filestat *fst, struct freebsd11_ptsstat *pts, char *errbuf);
80 int	freebsd11_procstat_get_sem_info(struct procstat *procstat,
81     struct filestat *fst, struct freebsd11_semstat *sem, char *errbuf);
82 int	freebsd11_procstat_get_shm_info(struct procstat *procstat,
83     struct filestat *fst, struct freebsd11_shmstat *shm, char *errbuf);
84 int	freebsd11_procstat_get_socket_info(struct procstat *procstat,
85     struct filestat *fst, struct freebsd11_sockstat *sock, char *errbuf);
86 int	freebsd11_procstat_get_vnode_info(struct procstat *procstat,
87     struct filestat *fst, struct freebsd11_vnstat *vn, char *errbuf);
88 
89 int
90 freebsd11_procstat_get_pts_info(struct procstat *procstat,
91     struct filestat *fst, struct freebsd11_ptsstat *pts_compat, char *errbuf)
92 {
93 	struct ptsstat pts;
94 	int r;
95 
96 	r = procstat_get_pts_info(procstat, fst, &pts, errbuf);
97 	if (r != 0)
98 		return (r);
99 	pts_compat->dev = pts.dev;
100 	memcpy(pts_compat->devname, pts.devname,
101 	    sizeof(pts_compat->devname));
102 	return (0);
103 }
104 
105 int
106 freebsd11_procstat_get_sem_info(struct procstat *procstat,
107     struct filestat *fst, struct freebsd11_semstat *sem_compat, char *errbuf)
108 {
109 	struct semstat sem;
110 	int r;
111 
112 	r = procstat_get_sem_info(procstat, fst, &sem, errbuf);
113 	if (r != 0)
114 		return (r);
115 	sem_compat->value = sem.value;
116 	sem_compat->mode = sem.mode;
117 	return (0);
118 }
119 
120 int
121 freebsd11_procstat_get_shm_info(struct procstat *procstat,
122     struct filestat *fst, struct freebsd11_shmstat *shm_compat, char *errbuf)
123 {
124 	struct shmstat shm;
125 	int r;
126 
127 	r = procstat_get_shm_info(procstat, fst, &shm, errbuf);
128 	if (r != 0)
129 		return (r);
130 	shm_compat->size = shm.size;
131 	shm_compat->mode = shm.mode;
132 	return (0);
133 }
134 
135 int
136 freebsd11_procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
137     struct freebsd11_sockstat *sock_compat, char *errbuf)
138 {
139 	struct sockstat sock;
140 	int r;
141 
142 	r = procstat_get_socket_info(procstat, fst, &sock, errbuf);
143 	if (r != 0)
144 		return (r);
145 	sock_compat->inp_ppcb = sock.inp_ppcb;
146 	sock_compat->so_addr = sock.so_addr;
147 	sock_compat->so_pcb = sock.so_pcb;
148 	sock_compat->unp_conn = sock.unp_conn;
149 	sock_compat->dom_family = sock.dom_family;
150 	sock_compat->proto = sock.proto;
151 	sock_compat->so_rcv_sb_state = sock.so_rcv_sb_state;
152 	sock_compat->so_snd_sb_state = sock.so_snd_sb_state;
153 	sock_compat->sa_local = sock.sa_local;
154 	sock_compat->sa_peer = sock.sa_peer;
155 	sock_compat->type = sock.type;
156 	memcpy(sock_compat->dname, sock.dname, sizeof(sock.dname));
157 	return (0);
158 }
159 
160 int
161 freebsd11_procstat_get_vnode_info(struct procstat *procstat,
162     struct filestat *fst, struct freebsd11_vnstat *vn_compat, char *errbuf)
163 {
164 	struct vnstat vn;
165 	int r;
166 
167 	r = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
168 	if (r != 0)
169 		return (r);
170 	vn_compat->vn_fileid = vn.vn_fileid;
171 	vn_compat->vn_size = vn.vn_size;
172 	vn_compat->vn_mntdir = vn.vn_mntdir;
173 	vn_compat->vn_dev = vn.vn_dev;
174 	vn_compat->vn_fsid = vn.vn_fsid;
175 	vn_compat->vn_type = vn.vn_type;
176 	vn_compat->vn_mode = vn.vn_mode;
177 	memcpy(vn_compat->vn_devname, vn.vn_devname,
178 	    sizeof(vn_compat->vn_devname));
179 	return (0);
180 }
181 
182 __sym_compat(procstat_get_pts_info, freebsd11_procstat_get_pts_info, FBSD_1.2);
183 __sym_compat(procstat_get_socket_info, freebsd11_procstat_get_socket_info,
184     FBSD_1.2);
185 __sym_compat(procstat_get_vnode_info, freebsd11_procstat_get_vnode_info,
186     FBSD_1.2);
187 __sym_compat(procstat_get_sem_info, freebsd11_procstat_get_sem_info, FBSD_1.3);
188 __sym_compat(procstat_get_shm_info, freebsd11_procstat_get_shm_info, FBSD_1.3);
189