libprocstat.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
  5. * Copyright (c) 2017 Dell EMC
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  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. #ifndef _LIBPROCSTAT_H_
  30. #define _LIBPROCSTAT_H_
  31. /*
  32. * XXX: sys/elf.h conflicts with zfs_context.h. Workaround this by not
  33. * including conflicting parts when building zfs code.
  34. */
  35. #ifndef ZFS
  36. #include <sys/elf.h>
  37. #endif
  38. #include <sys/caprights.h>
  39. /*
  40. * Vnode types.
  41. */
  42. #define PS_FST_VTYPE_VNON 1
  43. #define PS_FST_VTYPE_VREG 2
  44. #define PS_FST_VTYPE_VDIR 3
  45. #define PS_FST_VTYPE_VBLK 4
  46. #define PS_FST_VTYPE_VCHR 5
  47. #define PS_FST_VTYPE_VLNK 6
  48. #define PS_FST_VTYPE_VSOCK 7
  49. #define PS_FST_VTYPE_VFIFO 8
  50. #define PS_FST_VTYPE_VBAD 9
  51. #define PS_FST_VTYPE_UNKNOWN 255
  52. /*
  53. * Descriptor types.
  54. */
  55. #define PS_FST_TYPE_VNODE 1
  56. #define PS_FST_TYPE_FIFO 2
  57. #define PS_FST_TYPE_SOCKET 3
  58. #define PS_FST_TYPE_PIPE 4
  59. #define PS_FST_TYPE_PTS 5
  60. #define PS_FST_TYPE_KQUEUE 6
  61. /* was PS_FST_TYPE_CRYPTO 7 */
  62. #define PS_FST_TYPE_MQUEUE 8
  63. #define PS_FST_TYPE_SHM 9
  64. #define PS_FST_TYPE_SEM 10
  65. #define PS_FST_TYPE_UNKNOWN 11
  66. #define PS_FST_TYPE_NONE 12
  67. #define PS_FST_TYPE_PROCDESC 13
  68. #define PS_FST_TYPE_DEV 14
  69. #define PS_FST_TYPE_EVENTFD 15
  70. /*
  71. * Special descriptor numbers.
  72. */
  73. #define PS_FST_UFLAG_RDIR 0x0001
  74. #define PS_FST_UFLAG_CDIR 0x0002
  75. #define PS_FST_UFLAG_JAIL 0x0004
  76. #define PS_FST_UFLAG_TRACE 0x0008
  77. #define PS_FST_UFLAG_TEXT 0x0010
  78. #define PS_FST_UFLAG_MMAP 0x0020
  79. #define PS_FST_UFLAG_CTTY 0x0040
  80. /*
  81. * Descriptor flags.
  82. */
  83. #define PS_FST_FFLAG_READ 0x0001
  84. #define PS_FST_FFLAG_WRITE 0x0002
  85. #define PS_FST_FFLAG_NONBLOCK 0x0004
  86. #define PS_FST_FFLAG_APPEND 0x0008
  87. #define PS_FST_FFLAG_SHLOCK 0x0010
  88. #define PS_FST_FFLAG_EXLOCK 0x0020
  89. #define PS_FST_FFLAG_ASYNC 0x0040
  90. #define PS_FST_FFLAG_SYNC 0x0080
  91. #define PS_FST_FFLAG_NOFOLLOW 0x0100
  92. #define PS_FST_FFLAG_CREAT 0x0200
  93. #define PS_FST_FFLAG_TRUNC 0x0400
  94. #define PS_FST_FFLAG_EXCL 0x0800
  95. #define PS_FST_FFLAG_DIRECT 0x1000
  96. #define PS_FST_FFLAG_EXEC 0x2000
  97. #define PS_FST_FFLAG_HASLOCK 0x4000
  98. #if !defined(__ILP32__) && !defined(__riscv)
  99. /* Target architecture supports 32-bit compat */
  100. #define PS_ARCH_HAS_FREEBSD32 1
  101. #endif
  102. struct kinfo_kstack;
  103. struct kinfo_proc;
  104. struct kinfo_vmentry;
  105. struct procstat;
  106. struct ptrace_lwpinfo;
  107. struct rlimit;
  108. struct filestat {
  109. int fs_type; /* Descriptor type. */
  110. int fs_flags; /* filestat specific flags. */
  111. int fs_fflags; /* Descriptor access flags. */
  112. int fs_uflags; /* How this file is used. */
  113. int fs_fd; /* File descriptor number. */
  114. int fs_ref_count; /* Reference count. */
  115. off_t fs_offset; /* Seek location. */
  116. void *fs_typedep; /* Type dependent data. */
  117. char *fs_path;
  118. STAILQ_ENTRY(filestat) next;
  119. cap_rights_t fs_cap_rights; /* Capability rights, if flag set. */
  120. };
  121. struct vnstat {
  122. uint64_t vn_fileid;
  123. uint64_t vn_size;
  124. uint64_t vn_dev;
  125. uint64_t vn_fsid;
  126. char *vn_mntdir;
  127. int vn_type;
  128. uint16_t vn_mode;
  129. char vn_devname[SPECNAMELEN + 1];
  130. };
  131. struct ptsstat {
  132. uint64_t dev;
  133. char devname[SPECNAMELEN + 1];
  134. };
  135. struct pipestat {
  136. size_t buffer_cnt;
  137. uint64_t addr;
  138. uint64_t peer;
  139. };
  140. struct semstat {
  141. uint32_t value;
  142. uint16_t mode;
  143. };
  144. struct shmstat {
  145. uint64_t size;
  146. uint16_t mode;
  147. };
  148. struct sockstat {
  149. uint64_t so_addr;
  150. uint64_t so_pcb;
  151. uint64_t unp_conn;
  152. int dom_family;
  153. int proto;
  154. int so_rcv_sb_state;
  155. int so_snd_sb_state;
  156. struct sockaddr_storage sa_local; /* Socket address. */
  157. struct sockaddr_storage sa_peer; /* Peer address. */
  158. int type;
  159. char dname[32];
  160. unsigned int sendq;
  161. unsigned int recvq;
  162. };
  163. STAILQ_HEAD(filestat_list, filestat);
  164. struct advlock {
  165. int rw; /* PS_ADVLOCK_RO/RW */
  166. int type; /* PS_ADVLOCK_TYPE_ */
  167. int pid;
  168. int sysid;
  169. uint64_t file_fsid;
  170. uint64_t file_rdev;
  171. uint64_t file_fileid;
  172. off_t start;
  173. off_t len; /* len == 0 till the EOF */
  174. const char *path;
  175. STAILQ_ENTRY(advlock) next;
  176. };
  177. #define PS_ADVLOCK_RO 0x01
  178. #define PS_ADVLOCK_RW 0x02
  179. #define PS_ADVLOCK_TYPE_FLOCK 0x01
  180. #define PS_ADVLOCK_TYPE_PID 0x02
  181. #define PS_ADVLOCK_TYPE_REMOTE 0x03
  182. STAILQ_HEAD(advlock_list, advlock);
  183. __BEGIN_DECLS
  184. void procstat_close(struct procstat *procstat);
  185. void procstat_freeadvlock(struct procstat *procstat,
  186. struct advlock_list *advlocks);
  187. void procstat_freeargv(struct procstat *procstat);
  188. #ifndef ZFS
  189. void procstat_freeauxv(struct procstat *procstat, Elf_Auxinfo *auxv);
  190. #endif
  191. void procstat_freeenvv(struct procstat *procstat);
  192. void procstat_freegroups(struct procstat *procstat, gid_t *groups);
  193. void procstat_freekstack(struct procstat *procstat,
  194. struct kinfo_kstack *kkstp);
  195. void procstat_freeprocs(struct procstat *procstat, struct kinfo_proc *p);
  196. void procstat_freefiles(struct procstat *procstat,
  197. struct filestat_list *head);
  198. void procstat_freeptlwpinfo(struct procstat *procstat,
  199. struct ptrace_lwpinfo *pl);
  200. void procstat_freevmmap(struct procstat *procstat,
  201. struct kinfo_vmentry *vmmap);
  202. struct advlock_list *procstat_getadvlock(struct procstat *procstat);
  203. struct filestat_list *procstat_getfiles(struct procstat *procstat,
  204. struct kinfo_proc *kp, int mmapped);
  205. struct kinfo_proc *procstat_getprocs(struct procstat *procstat,
  206. int what, int arg, unsigned int *count);
  207. int procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
  208. struct pipestat *pipe, char *errbuf);
  209. int procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
  210. struct ptsstat *pts, char *errbuf);
  211. int procstat_get_sem_info(struct procstat *procstat, struct filestat *fst,
  212. struct semstat *sem, char *errbuf);
  213. int procstat_get_shm_info(struct procstat *procstat, struct filestat *fst,
  214. struct shmstat *shm, char *errbuf);
  215. int procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
  216. struct sockstat *sock, char *errbuf);
  217. int procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
  218. struct vnstat *vn, char *errbuf);
  219. char **procstat_getargv(struct procstat *procstat, struct kinfo_proc *p,
  220. size_t nchr);
  221. #ifndef ZFS
  222. Elf_Auxinfo *procstat_getauxv(struct procstat *procstat,
  223. struct kinfo_proc *kp, unsigned int *cntp);
  224. #endif
  225. struct ptrace_lwpinfo *procstat_getptlwpinfo(struct procstat *procstat,
  226. unsigned int *cntp);
  227. char **procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p,
  228. size_t nchr);
  229. gid_t *procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
  230. unsigned int *count);
  231. struct kinfo_kstack *procstat_getkstack(struct procstat *procstat,
  232. struct kinfo_proc *kp, unsigned int *count);
  233. int procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp,
  234. int *osrelp);
  235. int procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
  236. char *pathname, size_t maxlen);
  237. int procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp,
  238. int which, struct rlimit* rlimit);
  239. int procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
  240. unsigned short* umask);
  241. struct kinfo_vmentry *procstat_getvmmap(struct procstat *procstat,
  242. struct kinfo_proc *kp, unsigned int *count);
  243. struct procstat *procstat_open_core(const char *filename);
  244. struct procstat *procstat_open_sysctl(void);
  245. struct procstat *procstat_open_kvm(const char *nlistf, const char *memf);
  246. __END_DECLS
  247. #endif /* !_LIBPROCSTAT_H_ */