libprocstat.c (2ff020d3) libprocstat.c (89358231)
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:

--- 128 unchanged lines hidden (view full) ---

137static int to_filestat_flags(int flags);
138static int procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
139 struct vnstat *vn, char *errbuf);
140static int procstat_get_vnode_info_sysctl(struct filestat *fst,
141 struct vnstat *vn, char *errbuf);
142static gid_t *procstat_getgroups_core(struct procstat_core *core,
143 unsigned int *count);
144static gid_t *procstat_getgroups_sysctl(pid_t pid, unsigned int *count);
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:

--- 128 unchanged lines hidden (view full) ---

137static int to_filestat_flags(int flags);
138static int procstat_get_vnode_info_kvm(kvm_t *kd, struct filestat *fst,
139 struct vnstat *vn, char *errbuf);
140static int procstat_get_vnode_info_sysctl(struct filestat *fst,
141 struct vnstat *vn, char *errbuf);
142static gid_t *procstat_getgroups_core(struct procstat_core *core,
143 unsigned int *count);
144static gid_t *procstat_getgroups_sysctl(pid_t pid, unsigned int *count);
145static struct kinfo_kstack *procstat_getkstack_sysctl(pid_t pid,
146 int *cntp);
145static int procstat_getpathname_core(struct procstat_core *core,
146 char *pathname, size_t maxlen);
147static int procstat_getpathname_sysctl(pid_t pid, char *pathname,
148 size_t maxlen);
149static int procstat_getrlimit_core(struct procstat_core *core, int which,
150 struct rlimit* rlimit);
151static int procstat_getrlimit_sysctl(pid_t pid, int which,
152 struct rlimit* rlimit);

--- 1606 unchanged lines hidden (view full) ---

1759 *cntp = cnt;
1760 return (kiv); /* Caller must free() return value */
1761}
1762
1763struct kinfo_vmentry *
1764procstat_getvmmap(struct procstat *procstat, struct kinfo_proc *kp,
1765 unsigned int *cntp)
1766{
147static int procstat_getpathname_core(struct procstat_core *core,
148 char *pathname, size_t maxlen);
149static int procstat_getpathname_sysctl(pid_t pid, char *pathname,
150 size_t maxlen);
151static int procstat_getrlimit_core(struct procstat_core *core, int which,
152 struct rlimit* rlimit);
153static int procstat_getrlimit_sysctl(pid_t pid, int which,
154 struct rlimit* rlimit);

--- 1606 unchanged lines hidden (view full) ---

1761 *cntp = cnt;
1762 return (kiv); /* Caller must free() return value */
1763}
1764
1765struct kinfo_vmentry *
1766procstat_getvmmap(struct procstat *procstat, struct kinfo_proc *kp,
1767 unsigned int *cntp)
1768{
1769
1767 switch(procstat->type) {
1768 case PROCSTAT_KVM:
1769 warnx("kvm method is not supported");
1770 return (NULL);
1771 case PROCSTAT_SYSCTL:
1772 return (kinfo_getvmmap(kp->ki_pid, cntp));
1773 case PROCSTAT_CORE:
1774 return (kinfo_getvmmap_core(procstat->core, cntp));

--- 447 unchanged lines hidden (view full) ---

2222}
2223
2224void
2225procstat_freeauxv(struct procstat *procstat __unused, Elf_Auxinfo *auxv)
2226{
2227
2228 free(auxv);
2229}
1770 switch(procstat->type) {
1771 case PROCSTAT_KVM:
1772 warnx("kvm method is not supported");
1773 return (NULL);
1774 case PROCSTAT_SYSCTL:
1775 return (kinfo_getvmmap(kp->ki_pid, cntp));
1776 case PROCSTAT_CORE:
1777 return (kinfo_getvmmap_core(procstat->core, cntp));

--- 447 unchanged lines hidden (view full) ---

2225}
2226
2227void
2228procstat_freeauxv(struct procstat *procstat __unused, Elf_Auxinfo *auxv)
2229{
2230
2231 free(auxv);
2232}
2233
2234static struct kinfo_kstack *
2235procstat_getkstack_sysctl(pid_t pid, int *cntp)
2236{
2237 struct kinfo_kstack *kkstp;
2238 int error, name[4];
2239 size_t len;
2240
2241 name[0] = CTL_KERN;
2242 name[1] = KERN_PROC;
2243 name[2] = KERN_PROC_KSTACK;
2244 name[3] = pid;
2245
2246 len = 0;
2247 error = sysctl(name, 4, NULL, &len, NULL, 0);
2248 if (error < 0 && errno != ESRCH && errno != EPERM && errno != ENOENT) {
2249 warn("sysctl: kern.proc.kstack: %d", pid);
2250 return (NULL);
2251 }
2252 if (error == -1 && errno == ENOENT) {
2253 warnx("sysctl: kern.proc.kstack unavailable"
2254 " (options DDB or options STACK required in kernel)");
2255 return (NULL);
2256 }
2257 if (error == -1)
2258 return (NULL);
2259 kkstp = malloc(len);
2260 if (kkstp == NULL) {
2261 warn("malloc(%zu)", len);
2262 return (NULL);
2263 }
2264 if (sysctl(name, 4, kkstp, &len, NULL, 0) == -1) {
2265 warn("sysctl: kern.proc.pid: %d", pid);
2266 free(kkstp);
2267 return (NULL);
2268 }
2269 *cntp = len / sizeof(*kkstp);
2270
2271 return (kkstp);
2272}
2273
2274struct kinfo_kstack *
2275procstat_getkstack(struct procstat *procstat, struct kinfo_proc *kp,
2276 unsigned int *cntp)
2277{
2278 switch(procstat->type) {
2279 case PROCSTAT_KVM:
2280 warnx("kvm method is not supported");
2281 return (NULL);
2282 case PROCSTAT_SYSCTL:
2283 return (procstat_getkstack_sysctl(kp->ki_pid, cntp));
2284 case PROCSTAT_CORE:
2285 warnx("core method is not supported");
2286 return (NULL);
2287 default:
2288 warnx("unknown access method: %d", procstat->type);
2289 return (NULL);
2290 }
2291}
2292
2293void
2294procstat_freekstack(struct procstat *procstat __unused,
2295 struct kinfo_kstack *kkstp)
2296{
2297
2298 free(kkstp);
2299}