ps.c revision 1.96 1 1.96 christos /* $NetBSD: ps.c,v 1.96 2021/06/04 22:39:41 christos Exp $ */
2 1.13 cgd
3 1.40 simonb /*
4 1.70 apb * Copyright (c) 2000-2008 The NetBSD Foundation, Inc.
5 1.40 simonb * All rights reserved.
6 1.40 simonb *
7 1.40 simonb * This code is derived from software contributed to The NetBSD Foundation
8 1.40 simonb * by Simon Burge.
9 1.40 simonb *
10 1.40 simonb * Redistribution and use in source and binary forms, with or without
11 1.40 simonb * modification, are permitted provided that the following conditions
12 1.40 simonb * are met:
13 1.40 simonb * 1. Redistributions of source code must retain the above copyright
14 1.40 simonb * notice, this list of conditions and the following disclaimer.
15 1.40 simonb * 2. Redistributions in binary form must reproduce the above copyright
16 1.40 simonb * notice, this list of conditions and the following disclaimer in the
17 1.40 simonb * documentation and/or other materials provided with the distribution.
18 1.40 simonb *
19 1.40 simonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.40 simonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.40 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.40 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.40 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.40 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.40 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.40 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.40 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.40 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.40 simonb * POSSIBILITY OF SUCH DAMAGE.
30 1.40 simonb */
31 1.40 simonb
32 1.40 simonb /*
33 1.11 cgd * Copyright (c) 1990, 1993, 1994
34 1.11 cgd * The Regents of the University of California. All rights reserved.
35 1.1 cgd *
36 1.1 cgd * Redistribution and use in source and binary forms, with or without
37 1.1 cgd * modification, are permitted provided that the following conditions
38 1.1 cgd * are met:
39 1.1 cgd * 1. Redistributions of source code must retain the above copyright
40 1.1 cgd * notice, this list of conditions and the following disclaimer.
41 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 cgd * notice, this list of conditions and the following disclaimer in the
43 1.1 cgd * documentation and/or other materials provided with the distribution.
44 1.50 agc * 3. Neither the name of the University nor the names of its contributors
45 1.1 cgd * may be used to endorse or promote products derived from this software
46 1.1 cgd * without specific prior written permission.
47 1.1 cgd *
48 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 1.1 cgd * SUCH DAMAGE.
59 1.1 cgd */
60 1.1 cgd
61 1.19 christos #include <sys/cdefs.h>
62 1.1 cgd #ifndef lint
63 1.69 lukem __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\
64 1.69 lukem The Regents of the University of California. All rights reserved.");
65 1.1 cgd #endif /* not lint */
66 1.1 cgd
67 1.1 cgd #ifndef lint
68 1.13 cgd #if 0
69 1.11 cgd static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94";
70 1.13 cgd #else
71 1.96 christos __RCSID("$NetBSD: ps.c,v 1.96 2021/06/04 22:39:41 christos Exp $");
72 1.13 cgd #endif
73 1.1 cgd #endif /* not lint */
74 1.1 cgd
75 1.1 cgd #include <sys/param.h>
76 1.1 cgd #include <sys/time.h>
77 1.1 cgd #include <sys/resource.h>
78 1.48 thorpej #include <sys/lwp.h>
79 1.1 cgd #include <sys/proc.h>
80 1.1 cgd #include <sys/stat.h>
81 1.1 cgd #include <sys/ioctl.h>
82 1.11 cgd #include <sys/sysctl.h>
83 1.11 cgd
84 1.49 dsl #include <stddef.h>
85 1.11 cgd #include <ctype.h>
86 1.11 cgd #include <err.h>
87 1.11 cgd #include <errno.h>
88 1.11 cgd #include <fcntl.h>
89 1.94 cjep #include <grp.h>
90 1.11 cgd #include <kvm.h>
91 1.17 pk #include <limits.h>
92 1.68 christos #include <locale.h>
93 1.86 rin #include <math.h>
94 1.1 cgd #include <nlist.h>
95 1.11 cgd #include <paths.h>
96 1.25 mycroft #include <pwd.h>
97 1.1 cgd #include <stdio.h>
98 1.1 cgd #include <stdlib.h>
99 1.1 cgd #include <string.h>
100 1.11 cgd #include <unistd.h>
101 1.11 cgd
102 1.1 cgd #include "ps.h"
103 1.1 cgd
104 1.40 simonb /*
105 1.40 simonb * ARGOPTS must contain all option characters that take arguments
106 1.40 simonb * (except for 't'!) - it is used in kludge_oldps_options()
107 1.40 simonb */
108 1.94 cjep #define GETOPTSTR "aAcCdegG:hjk:LlM:mN:O:o:p:rSsTt:U:uvW:wx"
109 1.94 cjep #define ARGOPTS "GkMNOopUW"
110 1.40 simonb
111 1.61 apb struct varlist displaylist = SIMPLEQ_HEAD_INITIALIZER(displaylist);
112 1.61 apb struct varlist sortlist = SIMPLEQ_HEAD_INITIALIZER(sortlist);
113 1.1 cgd
114 1.1 cgd int eval; /* exit value */
115 1.1 cgd int sumrusage; /* -S */
116 1.1 cgd int termwidth; /* width of screen (0 == infinity) */
117 1.1 cgd int totwidth; /* calculated width of requested variables */
118 1.1 cgd
119 1.48 thorpej int needcomm, needenv, commandonly;
120 1.33 simonb uid_t myuid;
121 1.1 cgd
122 1.48 thorpej static struct kinfo_lwp
123 1.52 simonb *pick_representative_lwp(struct kinfo_proc2 *,
124 1.52 simonb struct kinfo_lwp *, int);
125 1.38 simonb static struct kinfo_proc2
126 1.52 simonb *getkinfo_kvm(kvm_t *, int, int, int *);
127 1.86 rin static struct pinfo
128 1.86 rin *setpinfo(struct kinfo_proc2 *, int, int, int);
129 1.52 simonb static char *kludge_oldps_options(char *);
130 1.52 simonb static int pscomp(const void *, const void *);
131 1.52 simonb static void scanvars(void);
132 1.76 joerg __dead static void usage(void);
133 1.60 christos static int parsenum(const char *, const char *);
134 1.87 christos static void descendant_sort(struct pinfo *, int);
135 1.1 cgd
136 1.1 cgd char dfmt[] = "pid tt state time command";
137 1.1 cgd char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
138 1.1 cgd char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
139 1.53 simonb char sfmt[] = "uid pid ppid cpu lid nlwp pri nice vsz rss wchan lstate tt "
140 1.79 mlelstv "ltime command";
141 1.1 cgd char ufmt[] = "user pid %cpu %mem vsz rss tt state start time command";
142 1.11 cgd char vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command";
143 1.11 cgd
144 1.64 yamt const char *default_fmt = dfmt;
145 1.64 yamt
146 1.61 apb struct varent *Opos = NULL; /* -O flag inserts after this point */
147 1.61 apb
148 1.11 cgd kvm_t *kd;
149 1.1 cgd
150 1.78 joerg static long long
151 1.78 joerg ttyname2dev(const char *ttname, int *xflg, int *what)
152 1.78 joerg {
153 1.78 joerg struct stat sb;
154 1.78 joerg const char *ttypath;
155 1.78 joerg char pathbuf[MAXPATHLEN];
156 1.78 joerg
157 1.78 joerg ttypath = NULL;
158 1.78 joerg if (strcmp(ttname, "?") == 0) {
159 1.78 joerg *xflg = 1;
160 1.78 joerg return KERN_PROC_TTY_NODEV;
161 1.78 joerg }
162 1.78 joerg if (strcmp(ttname, "-") == 0)
163 1.78 joerg return KERN_PROC_TTY_REVOKE;
164 1.78 joerg
165 1.78 joerg if (strcmp(ttname, "co") == 0)
166 1.78 joerg ttypath = _PATH_CONSOLE;
167 1.78 joerg else if (strncmp(ttname, "pts/", 4) == 0 ||
168 1.78 joerg strncmp(ttname, "tty", 3) == 0) {
169 1.78 joerg (void)snprintf(pathbuf,
170 1.78 joerg sizeof(pathbuf), "%s%s", _PATH_DEV, ttname);
171 1.78 joerg ttypath = pathbuf;
172 1.78 joerg } else if (*ttname != '/') {
173 1.78 joerg (void)snprintf(pathbuf,
174 1.78 joerg sizeof(pathbuf), "%s%s", _PATH_TTY, ttname);
175 1.78 joerg ttypath = pathbuf;
176 1.78 joerg } else
177 1.78 joerg ttypath = ttname;
178 1.78 joerg *what = KERN_PROC_TTY;
179 1.78 joerg if (stat(ttypath, &sb) == -1) {
180 1.82 dholland devmajor_t pts;
181 1.82 dholland int serrno;
182 1.78 joerg
183 1.82 dholland serrno = errno;
184 1.82 dholland pts = getdevmajor("pts", S_IFCHR);
185 1.78 joerg if (pts != NODEVMAJOR && strncmp(ttname, "pts/", 4) == 0) {
186 1.78 joerg int ptsminor = atoi(ttname + 4);
187 1.78 joerg
188 1.78 joerg snprintf(pathbuf, sizeof(pathbuf), "pts/%d", ptsminor);
189 1.78 joerg if (strcmp(pathbuf, ttname) == 0 && ptsminor >= 0)
190 1.78 joerg return makedev(pts, ptsminor);
191 1.78 joerg }
192 1.82 dholland errno = serrno;
193 1.90 christos err(EXIT_FAILURE, "%s", ttypath);
194 1.78 joerg }
195 1.78 joerg if (!S_ISCHR(sb.st_mode))
196 1.90 christos errx(EXIT_FAILURE, "%s: not a terminal", ttypath);
197 1.78 joerg return sb.st_rdev;
198 1.78 joerg }
199 1.78 joerg
200 1.11 cgd int
201 1.52 simonb main(int argc, char *argv[])
202 1.1 cgd {
203 1.86 rin struct kinfo_proc2 *kinfo;
204 1.86 rin struct pinfo *pinfo;
205 1.11 cgd struct varent *vent;
206 1.1 cgd struct winsize ws;
207 1.48 thorpej struct kinfo_lwp *kl, *l;
208 1.87 christos int ch, i, j, fmt, lineno, descendancy, nentries, nlwps;
209 1.72 christos long long flag;
210 1.86 rin int calc_pcpu, prtheader, wflag, what, xflg, rawcpu, showlwps;
211 1.17 pk char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX];
212 1.26 kim char *ttname;
213 1.1 cgd
214 1.68 christos setprogname(argv[0]);
215 1.68 christos (void)setlocale(LC_ALL, "");
216 1.68 christos
217 1.38 simonb if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
218 1.38 simonb ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
219 1.38 simonb ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) ||
220 1.1 cgd ws.ws_col == 0)
221 1.1 cgd termwidth = 79;
222 1.1 cgd else
223 1.1 cgd termwidth = ws.ws_col - 1;
224 1.1 cgd
225 1.1 cgd if (argc > 1)
226 1.1 cgd argv[1] = kludge_oldps_options(argv[1]);
227 1.1 cgd
228 1.89 kamil descendancy = fmt = prtheader = wflag = xflg = rawcpu = showlwps = 0;
229 1.25 mycroft what = KERN_PROC_UID;
230 1.33 simonb flag = myuid = getuid();
231 1.1 cgd memf = nlistf = swapf = NULL;
232 1.81 dholland
233 1.40 simonb while ((ch = getopt(argc, argv, GETOPTSTR)) != -1)
234 1.1 cgd switch((char)ch) {
235 1.70 apb case 'A':
236 1.70 apb /* "-A" shows all processes, like "-ax" */
237 1.70 apb xflg = 1;
238 1.70 apb /*FALLTHROUGH*/
239 1.1 cgd case 'a':
240 1.25 mycroft what = KERN_PROC_ALL;
241 1.25 mycroft flag = 0;
242 1.1 cgd break;
243 1.12 mycroft case 'c':
244 1.12 mycroft commandonly = 1;
245 1.12 mycroft break;
246 1.87 christos case 'd':
247 1.87 christos descendancy = 1;
248 1.87 christos break;
249 1.11 cgd case 'e': /* XXX set ufmt */
250 1.11 cgd needenv = 1;
251 1.11 cgd break;
252 1.1 cgd case 'C':
253 1.1 cgd rawcpu = 1;
254 1.1 cgd break;
255 1.1 cgd case 'g':
256 1.11 cgd break; /* no-op */
257 1.94 cjep case 'G':
258 1.94 cjep if (*optarg != '\0') {
259 1.94 cjep struct group *gr;
260 1.94 cjep
261 1.94 cjep what = KERN_PROC_GID;
262 1.94 cjep gr = getgrnam(optarg);
263 1.94 cjep if (gr == NULL) {
264 1.96 christos flag = parsenum(optarg, "group id");
265 1.96 christos } else
266 1.96 christos flag = gr->gr_gid;
267 1.94 cjep }
268 1.94 cjep break;
269 1.94 cjep
270 1.1 cgd case 'h':
271 1.1 cgd prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
272 1.1 cgd break;
273 1.1 cgd case 'j':
274 1.1 cgd parsefmt(jfmt);
275 1.15 mycroft fmt = 1;
276 1.1 cgd jfmt[0] = '\0';
277 1.1 cgd break;
278 1.49 dsl case 'k':
279 1.49 dsl parsesort(optarg);
280 1.49 dsl break;
281 1.28 bgrayson case 'K':
282 1.47 jdolecek break; /* no-op - was dontuseprocfs */
283 1.11 cgd case 'L':
284 1.1 cgd showkey();
285 1.85 rin return 0;
286 1.1 cgd case 'l':
287 1.1 cgd parsefmt(lfmt);
288 1.15 mycroft fmt = 1;
289 1.1 cgd lfmt[0] = '\0';
290 1.1 cgd break;
291 1.1 cgd case 'M':
292 1.1 cgd memf = optarg;
293 1.1 cgd break;
294 1.1 cgd case 'm':
295 1.49 dsl parsesort("vsz");
296 1.1 cgd break;
297 1.1 cgd case 'N':
298 1.1 cgd nlistf = optarg;
299 1.1 cgd break;
300 1.1 cgd case 'O':
301 1.61 apb /*
302 1.61 apb * If this is not the first -O option, insert
303 1.61 apb * just after the previous one.
304 1.61 apb *
305 1.61 apb * If there is no format yet, start with the default
306 1.61 apb * format, and insert after the pid column.
307 1.61 apb *
308 1.61 apb * If there is already a format, insert after
309 1.61 apb * the pid column, or at the end if there's no
310 1.61 apb * pid column.
311 1.61 apb */
312 1.61 apb if (!Opos) {
313 1.61 apb if (!fmt)
314 1.64 yamt parsefmt(default_fmt);
315 1.61 apb Opos = varlist_find(&displaylist, "pid");
316 1.61 apb }
317 1.61 apb parsefmt_insert(optarg, &Opos);
318 1.15 mycroft fmt = 1;
319 1.1 cgd break;
320 1.1 cgd case 'o':
321 1.1 cgd parsefmt(optarg);
322 1.15 mycroft fmt = 1;
323 1.1 cgd break;
324 1.1 cgd case 'p':
325 1.25 mycroft what = KERN_PROC_PID;
326 1.60 christos flag = parsenum(optarg, "process id");
327 1.1 cgd xflg = 1;
328 1.1 cgd break;
329 1.1 cgd case 'r':
330 1.49 dsl parsesort("%cpu");
331 1.1 cgd break;
332 1.1 cgd case 'S':
333 1.1 cgd sumrusage = 1;
334 1.1 cgd break;
335 1.48 thorpej case 's':
336 1.48 thorpej /* -L was already taken... */
337 1.48 thorpej showlwps = 1;
338 1.64 yamt default_fmt = sfmt;
339 1.48 thorpej break;
340 1.1 cgd case 'T':
341 1.22 mycroft if ((ttname = ttyname(STDIN_FILENO)) == NULL)
342 1.90 christos errx(EXIT_FAILURE, "stdin: not a terminal");
343 1.78 joerg flag = ttyname2dev(ttname, &xflg, &what);
344 1.78 joerg break;
345 1.22 mycroft case 't':
346 1.78 joerg flag = ttyname2dev(optarg, &xflg, &what);
347 1.1 cgd break;
348 1.25 mycroft case 'U':
349 1.25 mycroft if (*optarg != '\0') {
350 1.25 mycroft struct passwd *pw;
351 1.25 mycroft
352 1.25 mycroft what = KERN_PROC_UID;
353 1.25 mycroft pw = getpwnam(optarg);
354 1.25 mycroft if (pw == NULL) {
355 1.96 christos flag = parsenum(optarg, "user id");
356 1.25 mycroft } else
357 1.25 mycroft flag = pw->pw_uid;
358 1.25 mycroft }
359 1.25 mycroft break;
360 1.1 cgd case 'u':
361 1.1 cgd parsefmt(ufmt);
362 1.49 dsl parsesort("%cpu");
363 1.15 mycroft fmt = 1;
364 1.1 cgd ufmt[0] = '\0';
365 1.1 cgd break;
366 1.1 cgd case 'v':
367 1.1 cgd parsefmt(vfmt);
368 1.49 dsl parsesort("vsz");
369 1.15 mycroft fmt = 1;
370 1.1 cgd vfmt[0] = '\0';
371 1.1 cgd break;
372 1.1 cgd case 'W':
373 1.1 cgd swapf = optarg;
374 1.1 cgd break;
375 1.1 cgd case 'w':
376 1.6 cgd if (wflag)
377 1.6 cgd termwidth = UNLIMITED;
378 1.6 cgd else if (termwidth < 131)
379 1.1 cgd termwidth = 131;
380 1.11 cgd wflag++;
381 1.1 cgd break;
382 1.1 cgd case 'x':
383 1.1 cgd xflg = 1;
384 1.1 cgd break;
385 1.1 cgd case '?':
386 1.1 cgd default:
387 1.1 cgd usage();
388 1.1 cgd }
389 1.1 cgd argc -= optind;
390 1.1 cgd argv += optind;
391 1.1 cgd
392 1.1 cgd #define BACKWARD_COMPATIBILITY
393 1.1 cgd #ifdef BACKWARD_COMPATIBILITY
394 1.1 cgd if (*argv) {
395 1.1 cgd nlistf = *argv;
396 1.1 cgd if (*++argv) {
397 1.1 cgd memf = *argv;
398 1.1 cgd if (*++argv)
399 1.1 cgd swapf = *argv;
400 1.1 cgd }
401 1.1 cgd }
402 1.1 cgd #endif
403 1.11 cgd
404 1.49 dsl if (memf == NULL) {
405 1.49 dsl kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
406 1.38 simonb donlist_sysctl();
407 1.38 simonb } else
408 1.38 simonb kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf);
409 1.52 simonb
410 1.85 rin if (kd == NULL)
411 1.90 christos errx(EXIT_FAILURE, "%s", errbuf);
412 1.21 mrg
413 1.15 mycroft if (!fmt)
414 1.64 yamt parsefmt(default_fmt);
415 1.1 cgd
416 1.49 dsl /* Add default sort criteria */
417 1.49 dsl parsesort("tdev,pid");
418 1.86 rin calc_pcpu = 0;
419 1.61 apb SIMPLEQ_FOREACH(vent, &sortlist, next) {
420 1.49 dsl if (vent->var->flag & LWP || vent->var->type == UNSPECIFIED)
421 1.83 christos warnx("Cannot sort on %s, sort key ignored",
422 1.49 dsl vent->var->name);
423 1.86 rin if (vent->var->type == PCPU)
424 1.86 rin calc_pcpu = 1;
425 1.49 dsl }
426 1.86 rin if (!calc_pcpu)
427 1.86 rin SIMPLEQ_FOREACH(vent, &displaylist, next)
428 1.86 rin if (vent->var->type == PCPU) {
429 1.86 rin calc_pcpu = 1;
430 1.86 rin break;
431 1.86 rin }
432 1.49 dsl
433 1.1 cgd /*
434 1.39 simonb * scan requested variables, noting what structures are needed.
435 1.1 cgd */
436 1.1 cgd scanvars();
437 1.29 jdolecek
438 1.1 cgd /*
439 1.1 cgd * select procs
440 1.1 cgd */
441 1.48 thorpej if (!(kinfo = getkinfo_kvm(kd, what, flag, &nentries)))
442 1.91 christos errx(EXIT_FAILURE, "%s", kvm_geterr(kd));
443 1.39 simonb if (nentries == 0) {
444 1.39 simonb printheader();
445 1.85 rin return 1;
446 1.39 simonb }
447 1.86 rin pinfo = setpinfo(kinfo, nentries, calc_pcpu, rawcpu);
448 1.86 rin
449 1.1 cgd /*
450 1.1 cgd * sort proc list
451 1.1 cgd */
452 1.86 rin qsort(pinfo, nentries, sizeof(struct pinfo), pscomp);
453 1.87 christos
454 1.87 christos /*
455 1.87 christos * We want things in descendant order
456 1.87 christos */
457 1.87 christos if (descendancy)
458 1.87 christos descendant_sort(pinfo, nentries);
459 1.87 christos
460 1.1 cgd /*
461 1.39 simonb * For each proc, call each variable output function in
462 1.39 simonb * "setwidth" mode to determine the widest element of
463 1.39 simonb * the column.
464 1.39 simonb */
465 1.81 dholland
466 1.81 dholland for (i = 0; i < nentries; i++) {
467 1.86 rin struct pinfo *pi = &pinfo[i];
468 1.86 rin struct kinfo_proc2 *ki = pi->ki;
469 1.81 dholland
470 1.81 dholland if (xflg == 0 && (ki->p_tdev == (uint32_t)NODEV ||
471 1.81 dholland (ki->p_flag & P_CONTROLT) == 0))
472 1.81 dholland continue;
473 1.81 dholland
474 1.81 dholland kl = kvm_getlwps(kd, ki->p_pid, ki->p_paddr,
475 1.81 dholland sizeof(struct kinfo_lwp), &nlwps);
476 1.81 dholland if (kl == 0)
477 1.81 dholland nlwps = 0;
478 1.81 dholland if (showlwps == 0) {
479 1.81 dholland l = pick_representative_lwp(ki, kl, nlwps);
480 1.81 dholland SIMPLEQ_FOREACH(vent, &displaylist, next)
481 1.86 rin OUTPUT(vent, l, pi, ki, WIDTHMODE);
482 1.81 dholland } else {
483 1.81 dholland /* The printing is done with the loops
484 1.81 dholland * reversed, but here we don't need that,
485 1.81 dholland * and this improves the code locality a bit.
486 1.81 dholland */
487 1.81 dholland SIMPLEQ_FOREACH(vent, &displaylist, next)
488 1.81 dholland for (j = 0; j < nlwps; j++)
489 1.86 rin OUTPUT(vent, &kl[j], pi, ki, WIDTHMODE);
490 1.39 simonb }
491 1.81 dholland }
492 1.39 simonb /*
493 1.39 simonb * Print header - AFTER determining process field widths.
494 1.39 simonb * printheader() also adds up the total width of all
495 1.39 simonb * fields the first time it's called.
496 1.39 simonb */
497 1.39 simonb printheader();
498 1.39 simonb /*
499 1.39 simonb * For each proc, call each variable output function in
500 1.39 simonb * print mode.
501 1.1 cgd */
502 1.1 cgd for (i = lineno = 0; i < nentries; i++) {
503 1.86 rin struct pinfo *pi = &pinfo[i];
504 1.86 rin struct kinfo_proc2 *ki = pi->ki;
505 1.14 mycroft
506 1.72 christos if (xflg == 0 && (ki->p_tdev == (uint32_t)NODEV ||
507 1.63 pavel (ki->p_flag & P_CONTROLT ) == 0))
508 1.1 cgd continue;
509 1.48 thorpej kl = kvm_getlwps(kd, ki->p_pid, (u_long)ki->p_paddr,
510 1.48 thorpej sizeof(struct kinfo_lwp), &nlwps);
511 1.48 thorpej if (kl == 0)
512 1.48 thorpej nlwps = 0;
513 1.48 thorpej if (showlwps == 0) {
514 1.48 thorpej l = pick_representative_lwp(ki, kl, nlwps);
515 1.61 apb SIMPLEQ_FOREACH(vent, &displaylist, next) {
516 1.86 rin OUTPUT(vent, l, pi, ki, PRINTMODE);
517 1.61 apb if (SIMPLEQ_NEXT(vent, next) != NULL)
518 1.48 thorpej (void)putchar(' ');
519 1.48 thorpej }
520 1.11 cgd (void)putchar('\n');
521 1.48 thorpej if (prtheader && lineno++ == prtheader - 4) {
522 1.48 thorpej (void)putchar('\n');
523 1.48 thorpej printheader();
524 1.48 thorpej lineno = 0;
525 1.48 thorpej }
526 1.48 thorpej } else {
527 1.48 thorpej for (j = 0; j < nlwps; j++) {
528 1.61 apb SIMPLEQ_FOREACH(vent, &displaylist, next) {
529 1.86 rin OUTPUT(vent, &kl[j], pi, ki, PRINTMODE);
530 1.61 apb if (SIMPLEQ_NEXT(vent, next) != NULL)
531 1.48 thorpej (void)putchar(' ');
532 1.48 thorpej }
533 1.48 thorpej (void)putchar('\n');
534 1.48 thorpej if (prtheader && lineno++ == prtheader - 4) {
535 1.48 thorpej (void)putchar('\n');
536 1.48 thorpej printheader();
537 1.48 thorpej lineno = 0;
538 1.48 thorpej }
539 1.48 thorpej }
540 1.1 cgd }
541 1.1 cgd }
542 1.92 kamil
543 1.93 kamil #ifdef __NO_LEAKS
544 1.92 kamil free(pinfo);
545 1.93 kamil #endif
546 1.92 kamil
547 1.85 rin return eval;
548 1.1 cgd }
549 1.1 cgd
550 1.48 thorpej static struct kinfo_lwp *
551 1.52 simonb pick_representative_lwp(struct kinfo_proc2 *ki, struct kinfo_lwp *kl, int nlwps)
552 1.48 thorpej {
553 1.48 thorpej int i, onproc, running, sleeping, stopped, suspended;
554 1.48 thorpej static struct kinfo_lwp zero_lwp;
555 1.48 thorpej
556 1.48 thorpej if (kl == 0)
557 1.48 thorpej return &zero_lwp;
558 1.52 simonb
559 1.48 thorpej /* Trivial case: only one LWP */
560 1.48 thorpej if (nlwps == 1)
561 1.48 thorpej return kl;
562 1.48 thorpej
563 1.48 thorpej switch (ki->p_realstat) {
564 1.48 thorpej case SSTOP:
565 1.48 thorpej case SACTIVE:
566 1.48 thorpej /* Pick the most live LWP */
567 1.48 thorpej onproc = running = sleeping = stopped = suspended = -1;
568 1.48 thorpej for (i = 0; i < nlwps; i++) {
569 1.48 thorpej switch (kl[i].l_stat) {
570 1.48 thorpej case LSONPROC:
571 1.48 thorpej onproc = i;
572 1.48 thorpej break;
573 1.48 thorpej case LSRUN:
574 1.48 thorpej running = i;
575 1.48 thorpej break;
576 1.48 thorpej case LSSLEEP:
577 1.48 thorpej sleeping = i;
578 1.48 thorpej break;
579 1.48 thorpej case LSSTOP:
580 1.48 thorpej stopped = i;
581 1.48 thorpej break;
582 1.48 thorpej case LSSUSPENDED:
583 1.48 thorpej suspended = i;
584 1.48 thorpej break;
585 1.48 thorpej }
586 1.48 thorpej }
587 1.48 thorpej if (onproc != -1)
588 1.48 thorpej return &kl[onproc];
589 1.48 thorpej if (running != -1)
590 1.48 thorpej return &kl[running];
591 1.48 thorpej if (sleeping != -1)
592 1.48 thorpej return &kl[sleeping];
593 1.48 thorpej if (stopped != -1)
594 1.48 thorpej return &kl[stopped];
595 1.48 thorpej if (suspended != -1)
596 1.48 thorpej return &kl[suspended];
597 1.48 thorpej break;
598 1.48 thorpej case SZOMB:
599 1.48 thorpej /* First will do */
600 1.48 thorpej return kl;
601 1.48 thorpej break;
602 1.48 thorpej }
603 1.48 thorpej /* Error condition! */
604 1.83 christos warnx("Inconsistent LWP state for process %d", ki->p_pid);
605 1.48 thorpej return kl;
606 1.48 thorpej }
607 1.52 simonb
608 1.38 simonb static struct kinfo_proc2 *
609 1.52 simonb getkinfo_kvm(kvm_t *kdp, int what, int flag, int *nentriesp)
610 1.29 jdolecek {
611 1.52 simonb
612 1.45 lukem return (kvm_getproc2(kdp, what, flag, sizeof(struct kinfo_proc2),
613 1.38 simonb nentriesp));
614 1.29 jdolecek }
615 1.29 jdolecek
616 1.86 rin static struct pinfo *
617 1.86 rin setpinfo(struct kinfo_proc2 *ki, int nentries, int calc_pcpu, int rawcpu)
618 1.86 rin {
619 1.86 rin struct pinfo *pi;
620 1.86 rin int i;
621 1.86 rin
622 1.87 christos pi = calloc(nentries, sizeof(*pi));
623 1.86 rin if (pi == NULL)
624 1.90 christos err(EXIT_FAILURE, "calloc");
625 1.86 rin
626 1.86 rin if (calc_pcpu && !nlistread)
627 1.86 rin donlist();
628 1.86 rin
629 1.86 rin for (i = 0; i < nentries; i++) {
630 1.86 rin pi[i].ki = &ki[i];
631 1.86 rin if (!calc_pcpu)
632 1.86 rin continue;
633 1.88 rin if (ki[i].p_swtime == 0 || ki[i].p_realstat == SZOMB) {
634 1.86 rin pi[i].pcpu = 0.0;
635 1.86 rin continue;
636 1.86 rin }
637 1.86 rin pi[i].pcpu = 100.0 * (double)ki[i].p_pctcpu / fscale;
638 1.86 rin if (!rawcpu)
639 1.86 rin pi[i].pcpu /= 1.0 - exp(ki[i].p_swtime * log_ccpu);
640 1.86 rin }
641 1.86 rin
642 1.86 rin return pi;
643 1.86 rin }
644 1.86 rin
645 1.11 cgd static void
646 1.52 simonb scanvars(void)
647 1.1 cgd {
648 1.11 cgd struct varent *vent;
649 1.11 cgd VAR *v;
650 1.1 cgd
651 1.61 apb SIMPLEQ_FOREACH(vent, &displaylist, next) {
652 1.1 cgd v = vent->var;
653 1.43 matt if (v->flag & COMM) {
654 1.1 cgd needcomm = 1;
655 1.43 matt break;
656 1.43 matt }
657 1.1 cgd }
658 1.11 cgd }
659 1.1 cgd
660 1.11 cgd static int
661 1.49 dsl pscomp(const void *a, const void *b)
662 1.1 cgd {
663 1.86 rin const struct pinfo *pa = (const struct pinfo *)a;
664 1.86 rin const struct pinfo *pb = (const struct pinfo *)b;
665 1.86 rin const struct kinfo_proc2 *ka = pa->ki;
666 1.86 rin const struct kinfo_proc2 *kb = pb->ki;
667 1.38 simonb
668 1.1 cgd int i;
669 1.49 dsl int64_t i64;
670 1.49 dsl VAR *v;
671 1.49 dsl struct varent *ve;
672 1.59 christos const sigset_t *sa, *sb;
673 1.49 dsl
674 1.74 mrg #define V_SIZE(k) ((k)->p_vm_msize)
675 1.54 simonb #define RDIFF_N(t, n) \
676 1.59 christos if (((const t *)((const char *)ka + v->off))[n] > ((const t *)((const char *)kb + v->off))[n]) \
677 1.49 dsl return 1; \
678 1.59 christos if (((const t *)((const char *)ka + v->off))[n] < ((const t *)((const char *)kb + v->off))[n]) \
679 1.49 dsl return -1;
680 1.49 dsl
681 1.54 simonb #define RDIFF(type) RDIFF_N(type, 0); continue
682 1.49 dsl
683 1.61 apb SIMPLEQ_FOREACH(ve, &sortlist, next) {
684 1.49 dsl v = ve->var;
685 1.49 dsl if (v->flag & LWP)
686 1.49 dsl /* LWP structure not available (yet) */
687 1.49 dsl continue;
688 1.49 dsl /* Sort on pvar() fields, + a few others */
689 1.49 dsl switch (v->type) {
690 1.49 dsl case CHAR:
691 1.49 dsl RDIFF(char);
692 1.49 dsl case UCHAR:
693 1.49 dsl RDIFF(u_char);
694 1.49 dsl case SHORT:
695 1.49 dsl RDIFF(short);
696 1.49 dsl case USHORT:
697 1.49 dsl RDIFF(ushort);
698 1.49 dsl case INT:
699 1.49 dsl RDIFF(int);
700 1.49 dsl case UINT:
701 1.49 dsl RDIFF(uint);
702 1.49 dsl case LONG:
703 1.49 dsl RDIFF(long);
704 1.49 dsl case ULONG:
705 1.49 dsl RDIFF(ulong);
706 1.49 dsl case INT32:
707 1.49 dsl RDIFF(int32_t);
708 1.49 dsl case UINT32:
709 1.49 dsl RDIFF(uint32_t);
710 1.49 dsl case SIGLIST:
711 1.86 rin sa = (const void *)((const char *)ka + v->off);
712 1.86 rin sb = (const void *)((const char *)kb + v->off);
713 1.49 dsl i = 0;
714 1.49 dsl do {
715 1.49 dsl if (sa->__bits[i] > sb->__bits[i])
716 1.49 dsl return 1;
717 1.49 dsl if (sa->__bits[i] < sb->__bits[i])
718 1.49 dsl return -1;
719 1.49 dsl i++;
720 1.73 lukem } while (i < (int)__arraycount(sa->__bits));
721 1.49 dsl continue;
722 1.49 dsl case INT64:
723 1.49 dsl RDIFF(int64_t);
724 1.49 dsl case KPTR:
725 1.49 dsl case KPTR24:
726 1.49 dsl case UINT64:
727 1.49 dsl RDIFF(uint64_t);
728 1.49 dsl case TIMEVAL:
729 1.49 dsl /* compare xxx_sec then xxx_usec */
730 1.49 dsl RDIFF_N(uint32_t, 0);
731 1.49 dsl RDIFF_N(uint32_t, 1);
732 1.49 dsl continue;
733 1.49 dsl case CPUTIME:
734 1.49 dsl i64 = ka->p_rtime_sec * 1000000 + ka->p_rtime_usec;
735 1.49 dsl i64 -= kb->p_rtime_sec * 1000000 + kb->p_rtime_usec;
736 1.49 dsl if (sumrusage) {
737 1.49 dsl i64 += ka->p_uctime_sec * 1000000
738 1.49 dsl + ka->p_uctime_usec;
739 1.49 dsl i64 -= kb->p_uctime_sec * 1000000
740 1.49 dsl + kb->p_uctime_usec;
741 1.49 dsl }
742 1.49 dsl if (i64 != 0)
743 1.49 dsl return i64 > 0 ? 1 : -1;
744 1.49 dsl continue;
745 1.49 dsl case PCPU:
746 1.86 rin i = pb->pcpu - pa->pcpu;
747 1.49 dsl if (i != 0)
748 1.49 dsl return i;
749 1.49 dsl continue;
750 1.49 dsl case VSIZE:
751 1.49 dsl i = V_SIZE(kb) - V_SIZE(ka);
752 1.49 dsl if (i != 0)
753 1.49 dsl return i;
754 1.49 dsl continue;
755 1.49 dsl
756 1.49 dsl default:
757 1.49 dsl /* Ignore everything else */
758 1.49 dsl break;
759 1.49 dsl }
760 1.49 dsl }
761 1.49 dsl return 0;
762 1.1 cgd
763 1.49 dsl #undef VSIZE
764 1.1 cgd }
765 1.1 cgd
766 1.1 cgd /*
767 1.1 cgd * ICK (all for getopt), would rather hide the ugliness
768 1.1 cgd * here than taint the main code.
769 1.1 cgd *
770 1.1 cgd * ps foo -> ps -foo
771 1.1 cgd * ps 34 -> ps -p34
772 1.1 cgd *
773 1.25 mycroft * The old convention that 't' with no trailing tty arg means the user's
774 1.1 cgd * tty, is only supported if argv[1] doesn't begin with a '-'. This same
775 1.1 cgd * feature is available with the option 'T', which takes no argument.
776 1.1 cgd */
777 1.11 cgd static char *
778 1.52 simonb kludge_oldps_options(char *s)
779 1.1 cgd {
780 1.1 cgd size_t len;
781 1.1 cgd char *newopts, *ns, *cp;
782 1.1 cgd
783 1.1 cgd len = strlen(s);
784 1.16 thorpej if ((newopts = ns = malloc(len + 3)) == NULL)
785 1.90 christos err(EXIT_FAILURE, NULL);
786 1.1 cgd /*
787 1.1 cgd * options begin with '-'
788 1.1 cgd */
789 1.1 cgd if (*s != '-')
790 1.1 cgd *ns++ = '-'; /* add option flag */
791 1.1 cgd /*
792 1.1 cgd * gaze to end of argv[1]
793 1.1 cgd */
794 1.1 cgd cp = s + len - 1;
795 1.1 cgd /*
796 1.40 simonb * if the last letter is a 't' flag and there are no other option
797 1.40 simonb * characters that take arguments (eg U, p, o) in the option
798 1.40 simonb * string and the option string doesn't start with a '-' then
799 1.40 simonb * convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
800 1.1 cgd */
801 1.40 simonb if (*cp == 't' && *s != '-' && strpbrk(s, ARGOPTS) == NULL)
802 1.1 cgd *cp = 'T';
803 1.1 cgd else {
804 1.1 cgd /*
805 1.1 cgd * otherwise check for trailing number, which *may* be a
806 1.1 cgd * pid.
807 1.1 cgd */
808 1.55 dsl while (cp >= s && isdigit((unsigned char)*cp))
809 1.1 cgd --cp;
810 1.1 cgd }
811 1.1 cgd cp++;
812 1.11 cgd memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */
813 1.1 cgd ns += cp - s;
814 1.1 cgd /*
815 1.1 cgd * if there's a trailing number, and not a preceding 'p' (pid) or
816 1.1 cgd * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
817 1.1 cgd */
818 1.55 dsl if (isdigit((unsigned char)*cp) &&
819 1.25 mycroft (cp == s || (cp[-1] != 'U' && cp[-1] != 't' && cp[-1] != 'p' &&
820 1.56 atatat cp[-1] != '/' && (cp - 1 == s || cp[-2] != 't'))))
821 1.1 cgd *ns++ = 'p';
822 1.18 mrg /* and append the number */
823 1.40 simonb (void)strcpy(ns, cp); /* XXX strcpy is safe here */
824 1.1 cgd
825 1.1 cgd return (newopts);
826 1.1 cgd }
827 1.1 cgd
828 1.60 christos static int
829 1.60 christos parsenum(const char *str, const char *msg)
830 1.60 christos {
831 1.60 christos char *ep;
832 1.60 christos unsigned long ul;
833 1.60 christos
834 1.60 christos ul = strtoul(str, &ep, 0);
835 1.60 christos
836 1.60 christos if (*str == '\0' || *ep != '\0')
837 1.90 christos errx(EXIT_FAILURE, "Invalid %s: `%s'", msg, str);
838 1.60 christos
839 1.60 christos if (ul > INT_MAX)
840 1.90 christos errx(EXIT_FAILURE, "Out of range %s: `%s'", msg, str);
841 1.60 christos
842 1.60 christos return (int)ul;
843 1.60 christos }
844 1.60 christos
845 1.11 cgd static void
846 1.87 christos descendant_sort(struct pinfo *ki, int items)
847 1.87 christos {
848 1.87 christos int dst, lvl, maxlvl, n, ndst, nsrc, siblings, src;
849 1.87 christos unsigned char *path;
850 1.87 christos struct pinfo kn;
851 1.87 christos
852 1.87 christos /*
853 1.87 christos * First, sort the entries by descendancy, tracking the descendancy
854 1.87 christos * depth in the level field.
855 1.87 christos */
856 1.87 christos src = 0;
857 1.87 christos maxlvl = 0;
858 1.87 christos while (src < items) {
859 1.87 christos if (ki[src].level) {
860 1.87 christos src++;
861 1.87 christos continue;
862 1.87 christos }
863 1.87 christos for (nsrc = 1; src + nsrc < items; nsrc++)
864 1.87 christos if (!ki[src + nsrc].level)
865 1.87 christos break;
866 1.87 christos
867 1.87 christos for (dst = 0; dst < items; dst++) {
868 1.87 christos if (ki[dst].ki->p_pid == ki[src].ki->p_pid)
869 1.87 christos continue;
870 1.87 christos if (ki[dst].ki->p_pid == ki[src].ki->p_ppid)
871 1.87 christos break;
872 1.87 christos }
873 1.87 christos
874 1.87 christos if (dst == items) {
875 1.87 christos src += nsrc;
876 1.87 christos continue;
877 1.87 christos }
878 1.87 christos
879 1.87 christos for (ndst = 1; dst + ndst < items; ndst++)
880 1.87 christos if (ki[dst + ndst].level <= ki[dst].level)
881 1.87 christos break;
882 1.87 christos
883 1.87 christos for (n = src; n < src + nsrc; n++) {
884 1.87 christos ki[n].level += ki[dst].level + 1;
885 1.87 christos if (maxlvl < ki[n].level)
886 1.87 christos maxlvl = ki[n].level;
887 1.87 christos }
888 1.87 christos
889 1.87 christos while (nsrc) {
890 1.87 christos if (src < dst) {
891 1.87 christos kn = ki[src];
892 1.87 christos memmove(ki + src, ki + src + 1,
893 1.87 christos (dst - src + ndst - 1) * sizeof *ki);
894 1.87 christos ki[dst + ndst - 1] = kn;
895 1.87 christos nsrc--;
896 1.87 christos dst--;
897 1.87 christos ndst++;
898 1.87 christos } else if (src != dst + ndst) {
899 1.87 christos kn = ki[src];
900 1.87 christos memmove(ki + dst + ndst + 1, ki + dst + ndst,
901 1.87 christos (src - dst - ndst) * sizeof *ki);
902 1.87 christos ki[dst + ndst] = kn;
903 1.87 christos ndst++;
904 1.87 christos nsrc--;
905 1.87 christos src++;
906 1.87 christos } else {
907 1.87 christos ndst += nsrc;
908 1.87 christos src += nsrc;
909 1.87 christos nsrc = 0;
910 1.87 christos }
911 1.87 christos }
912 1.87 christos }
913 1.87 christos
914 1.87 christos /*
915 1.87 christos * Now populate prefix (instead of level) with the command
916 1.87 christos * prefix used to show descendancies.
917 1.87 christos */
918 1.87 christos path = malloc((maxlvl + 7) / 8);
919 1.87 christos memset(path, '\0', (maxlvl + 7) / 8);
920 1.87 christos for (src = 0; src < items; src++) {
921 1.87 christos if ((lvl = ki[src].level) == 0) {
922 1.87 christos ki[src].prefix = NULL;
923 1.87 christos continue;
924 1.87 christos }
925 1.87 christos if ((ki[src].prefix = malloc(lvl * 2 + 1)) == NULL)
926 1.90 christos errx(EXIT_FAILURE, "malloc failed");
927 1.87 christos for (n = 0; n < lvl - 2; n++) {
928 1.87 christos ki[src].prefix[n * 2] =
929 1.87 christos path[n / 8] & 1 << (n % 8) ? '|' : ' ';
930 1.87 christos ki[src].prefix[n * 2 + 1] = ' ';
931 1.87 christos
932 1.87 christos }
933 1.87 christos if (n == lvl - 2) {
934 1.87 christos /* Have I any more siblings? */
935 1.87 christos for (siblings = 0, dst = src + 1; dst < items; dst++) {
936 1.87 christos if (ki[dst].level > lvl)
937 1.87 christos continue;
938 1.87 christos if (ki[dst].level == lvl)
939 1.87 christos siblings = 1;
940 1.87 christos break;
941 1.87 christos }
942 1.87 christos if (siblings)
943 1.87 christos path[n / 8] |= 1 << (n % 8);
944 1.87 christos else
945 1.87 christos path[n / 8] &= ~(1 << (n % 8));
946 1.87 christos ki[src].prefix[n * 2] = siblings ? '|' : '`';
947 1.87 christos ki[src].prefix[n * 2 + 1] = '-';
948 1.87 christos n++;
949 1.87 christos }
950 1.87 christos strcpy(ki[src].prefix + n * 2, "- ");
951 1.87 christos }
952 1.87 christos free(path);
953 1.87 christos }
954 1.87 christos
955 1.87 christos static void
956 1.52 simonb usage(void)
957 1.1 cgd {
958 1.1 cgd
959 1.11 cgd (void)fprintf(stderr,
960 1.11 cgd "usage:\t%s\n\t %s\n\t%s\n",
961 1.95 wiz "ps [-AaCcdehjlmrSsTuvwx] [-G group] [-k key] [-M core] [-N system]",
962 1.95 wiz "[-O fmt] [-o fmt] [-p pid] [-t tty] [-U user] [-W swap]",
963 1.71 wiz "ps -L");
964 1.1 cgd exit(1);
965 1.23 mycroft /* NOTREACHED */
966 1.1 cgd }
967