hammer2 - Fix missing initialization (fix broken root mount)
[dragonfly.git] / usr.bin / systat / main.c
blob12ff2a714b92443cad9b3d27fa50d3e95f3294f0
1 /*-
2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
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 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
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.
29 * @(#) Copyright (c) 1980, 1992, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)main.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/systat/main.c,v 1.11.2.1 2001/06/06 20:26:01 tmm Exp $
34 #include <sys/user.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
38 #include <err.h>
39 #include <limits.h>
40 #include <locale.h>
41 #include <nlist.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include "systat.h"
47 #include "extern.h"
49 static struct nlist namelist[] = {
50 #define X_FIRST 0
51 #define X_HZ 0
52 { .n_name = "_hz" },
53 #define X_STATHZ 1
54 { .n_name = "_stathz" },
55 { .n_name = "" }
57 static int dellave;
59 kvm_t *kd;
60 sig_t sigtstpdfl;
61 double avenrun[3];
62 int col;
63 double naptime = 5.0;
64 int verbose = 1; /* to report kvm read errs */
65 int hz, stathz;
66 double hertz;
67 char c;
68 char *namp;
69 char hostname[MAXHOSTNAMELEN];
70 WINDOW *wnd;
71 int CMDLINE;
73 static WINDOW *wload; /* one line window for load average */
75 int
76 main(int argc, char **argv)
78 char errbuf[_POSIX2_LINE_MAX];
80 (void) setlocale(LC_TIME, "");
82 argc--, argv++;
83 while (argc > 0) {
84 if (argv[0][0] == '-') {
85 struct cmdtab *p;
87 p = lookup(&argv[0][1]);
88 if (p == (struct cmdtab *)-1)
89 errx(1, "%s: ambiguous request", &argv[0][1]);
90 if (p == NULL)
91 errx(1, "%s: unknown request", &argv[0][1]);
92 curcmd = p;
93 } else {
94 naptime = strtod(argv[0], NULL);
95 if (naptime <= 0.0)
96 naptime = 5.0;
98 argc--, argv++;
100 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
101 if (kd == NULL) {
102 error("%s", errbuf);
103 exit(1);
105 signal(SIGINT, die);
106 signal(SIGQUIT, die);
107 signal(SIGTERM, die);
110 * Initialize display. Load average appears in a one line
111 * window of its own. Current command's display appears in
112 * an overlapping sub-window of stdscr configured by the display
113 * routines to minimize update work by curses.
115 initscr();
116 CMDLINE = LINES - 1;
117 wnd = (*curcmd->c_open)();
118 if (wnd == NULL) {
119 warnx("couldn't initialize display");
120 die(0);
122 wload = newwin(1, 0, 3, 20);
123 if (wload == NULL) {
124 warnx("couldn't set up load average window");
125 die(0);
127 if (kvm_nlist(kd, namelist)) {
128 nlisterr(namelist);
129 exit(1);
131 if (namelist[X_FIRST].n_type == 0)
132 errx(1, "couldn't read namelist");
133 gethostname(hostname, sizeof (hostname));
134 NREAD(X_HZ, &hz, sizeof(hz));
135 NREAD(X_STATHZ, &stathz, sizeof(stathz));
136 hertz = stathz ? stathz : hz;
137 (*curcmd->c_init)();
138 curcmd->c_flags |= CF_INIT;
139 labels();
141 dellave = 0.0;
143 signal(SIGALRM, display);
144 display(0);
145 noecho();
146 crmode();
147 keyboard();
148 /*NOTREACHED*/
150 return EXIT_SUCCESS;
153 void
154 labels(void)
156 if (curcmd->c_flags & CF_LOADAV) {
157 mvaddstr(2, 20,
158 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
159 mvaddstr(3, 5, "Load Average");
161 (*curcmd->c_label)();
162 #ifdef notdef
163 mvprintw(21, 25, "CPU usage on %s", hostname);
164 #endif
165 refresh();
168 void
169 display(int signo __unused)
171 int i, j;
172 struct itimerval ctv;
174 /* Get the load average over the last minute. */
175 (void) getloadavg(avenrun, NELEM(avenrun));
176 (*curcmd->c_fetch)();
177 if (curcmd->c_flags & CF_LOADAV) {
178 j = 5.0*avenrun[0] + 0.5;
179 dellave = avenrun[0];
180 c = '|';
181 wmove(wload, 0, 0); wclrtoeol(wload);
182 for (i = (j > 50) ? 50 : j; i > 0; i--)
183 waddch(wload, c);
184 if (j > 50)
185 wprintw(wload, " %4.1f", avenrun[0]);
187 (*curcmd->c_refresh)();
188 if (curcmd->c_flags & CF_LOADAV)
189 wrefresh(wload);
190 wrefresh(wnd);
191 move(CMDLINE, col);
192 refresh();
193 ctv.it_interval.tv_sec = 0;
194 ctv.it_interval.tv_usec = 0;
195 ctv.it_value.tv_sec = (int)naptime;
196 ctv.it_value.tv_usec = (naptime - (double)(int)naptime) * 1000000.0;
197 setitimer(ITIMER_REAL, &ctv, NULL);
200 void
201 load(void)
204 (void) getloadavg(avenrun, NELEM(avenrun));
205 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
206 avenrun[0], avenrun[1], avenrun[2]);
207 clrtoeol();
210 void
211 die(int signo __unused)
213 move(CMDLINE, 0);
214 clrtoeol();
215 refresh();
216 endwin();
217 exit(0);
220 #include <stdarg.h>
222 void
223 error(const char *fmt, ...)
225 va_list ap;
226 char buf[255];
227 int oy, ox;
228 va_start(ap, fmt);
230 if (wnd) {
231 getyx(stdscr, oy, ox);
232 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
233 clrtoeol();
234 standout();
235 mvaddstr(CMDLINE, 0, buf);
236 standend();
237 move(oy, ox);
238 refresh();
239 } else {
240 (void) vfprintf(stderr, fmt, ap);
241 fprintf(stderr, "\n");
243 va_end(ap);
246 void
247 nlisterr(struct nlist *n_list)
249 int i, n;
251 n = 0;
252 clear();
253 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
254 for (i = 0;
255 n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++)
256 if (n_list[i].n_value == 0)
257 mvprintw(2 + ++n, 10, "%s", n_list[i].n_name);
258 move(CMDLINE, 0);
259 clrtoeol();
260 refresh();
261 endwin();
262 exit(1);