1 /* $NetBSD: main.c,v 1.44 2009/07/14 21:08:31 apb Exp $ */
4 * Copyright (c) 1980, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\
35 The Regents of the University of California. All rights reserved.");
37 static char sccsid
[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
39 __RCSID("$NetBSD: main.c,v 1.44 2009/07/14 21:08:31 apb Exp $");
42 #include <sys/param.h>
54 #include <sys/ioctl.h>
59 static struct nlist namelist
[] = {
64 { .n_name
= "_stathz" },
66 { .n_name
= "_maxslp" },
78 int verbose
= 1; /* to report kvm read errs */
79 int hz
, stathz
, maxslp
;
82 char hostname
[MAXHOSTNAMELEN
+ 1];
85 int turns
= 2; /* stay how many refresh-turns in 'all' mode? */
88 sig_atomic_t needsredraw
= 0;
91 static WINDOW
*wload
; /* one line window for load average */
93 static void (*sv_stop_handler
)(int);
95 static void stop(int);
96 static void usage(void);
97 int main(int, char **);
99 gid_t egid
; /* XXX needed by initiostat() and initkre() */
102 main(int argc
, char **argv
)
105 char errbuf
[_POSIX2_LINE_MAX
];
110 (void)setegid(getgid());
112 while ((ch
= getopt(argc
, argv
, "M:N:nw:t:")) != -1)
125 if ((naptime
= atoi(optarg
)) <= 0)
126 errx(1, "interval <= 0.");
129 if ((turns
= atoi(optarg
)) <= 0)
130 errx(1, "turns <= 0.");
140 for ( ; argc
> 0; argc
--, argv
++) {
144 if (isdigit((unsigned char)argv
[0][0])) {
145 naptime
= atoi(argv
[0]);
151 for (p
= modes
; p
->c_name
; p
++) {
152 if (strstr(p
->c_name
, argv
[0]) == p
->c_name
) {
158 if (strstr(all
, argv
[0]) == all
) {
165 if (!modefound
&& !allflag
)
166 error("%s: Unknown command.", argv
[0]);
170 * Discard setgid privileges. If not the running kernel, we toss
171 * them away totally so that bad guys can't print interesting stuff
172 * from kernel memory, otherwise switch back to kmem for the
173 * duration of the kvm_openfiles() call.
175 if (nlistf
!= NULL
|| memf
!= NULL
)
176 (void)setgid(getgid());
180 kd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, errbuf
);
186 /* Get rid of privs for now. */
187 if (nlistf
== NULL
&& memf
== NULL
)
188 (void)setegid(getgid());
190 if (kvm_nlist(kd
, namelist
)) {
192 errx(1, "%s: no namelist", nlistf
);
194 errx(1, "no namelist");
198 signal(SIGQUIT
, die
);
199 signal(SIGTERM
, die
);
200 sv_stop_handler
= signal(SIGTSTP
, stop
);
203 * Initialize display. Load average appears in a one line
204 * window of its own. Current command's display appears in
205 * an overlapping sub-window of stdscr configured by the display
206 * routines to minimize update work by curses.
208 if (initscr() == NULL
)
210 warnx("couldn't initialize screen");
215 wnd
= (*curmode
->c_open
)();
217 warnx("couldn't initialize display");
220 wload
= newwin(1, 0, 3, 20);
222 warnx("couldn't set up load average window");
225 gethostname(hostname
, sizeof (hostname
));
226 hostname
[sizeof(hostname
) - 1] = '\0';
227 NREAD(X_HZ
, &hz
, sizeof hz
);
228 NREAD(X_STATHZ
, &stathz
, sizeof stathz
);
229 NREAD(X_MAXSLP
, &maxslp
, sizeof maxslp
);
230 (*curmode
->c_init
)();
231 curmode
->c_flags
|= CF_INIT
;
246 fprintf(stderr
, "usage: systat [-n] [-M core] [-N system] [-w wait] "
247 "[-t turns]\n\t\t[display] [refresh-interval]\n");
255 if (curmode
->c_flags
& CF_LOADAV
) {
257 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
258 mvaddstr(3, 5, "Load Average");
260 (*curmode
->c_label
)();
262 mvprintw(21, 25, "CPU usage on %s", hostname
);
275 if (signo
== SIGALRM
&& skip
-- > 0)
276 /* Don't display on this timeout */
279 /* Get the load average over the last minute. */
280 (void)getloadavg(avenrun
, sizeof(avenrun
) / sizeof(avenrun
[0]));
281 (*curmode
->c_fetch
)();
282 if (curmode
->c_flags
& CF_LOADAV
) {
283 j
= 5.0*avenrun
[0] + 0.5;
284 dellave
-= avenrun
[0];
293 dellave
= avenrun
[0];
296 whline(wload
, c
, (j
> 50) ? 50 : j
);
298 wprintw(wload
, " %4.1f", avenrun
[0]);
300 (*curmode
->c_refresh
)();
301 if (curmode
->c_flags
& CF_LOADAV
)
307 if (allflag
&& signo
==SIGALRM
) {
308 if (allcounter
>= turns
){
311 if (p
->c_name
== NULL
)
319 /* curses timeout() uses VTIME, limited to 255 1/10th secs */
320 ms_delay
= naptime
* 1000;
321 if (ms_delay
< 25500) {
325 skip
= ms_delay
/ 25500;
326 timeout(ms_delay
/ (skip
+ 1));
333 resizeterm(LINES
, COLS
);
346 signal(SIGTSTP
, sv_stop_handler
);
347 /* unblock SIGTSTP */
349 sigaddset(&set
, SIGTSTP
);
350 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
353 /* must have been restarted */
354 signal(SIGTSTP
, stop
);
369 error(const char *fmt
, ...)
378 getyx(stdscr
, oy
, ox
);
379 (void) vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
382 mvaddstr(CMDLINE
, 0, buf
);
387 (void) vfprintf(stderr
, fmt
, ap
);
388 fprintf(stderr
, "\n");
394 nlisterr(struct nlist name_list
[])
400 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
402 name_list
[i
].n_name
!= NULL
&& *name_list
[i
].n_name
!= '\0'; i
++)
403 if (name_list
[i
].n_value
== 0)
404 mvprintw(2 + ++n
, 10, "%s", name_list
[i
].n_name
);