1 /*- 2 * Copyright (c) 1980, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)w.c 8.3 (Berkeley) 04/04/94"; 16 #endif /* not lint */ 17 18 /* 19 * w - print system status (who and what) 20 * 21 * This program is similar to the systat command on Tenex/Tops 10/20 22 * 23 */ 24 #include <sys/param.h> 25 #include <sys/time.h> 26 #include <sys/stat.h> 27 #include <sys/sysctl.h> 28 #include <sys/proc.h> 29 #include <sys/user.h> 30 #include <sys/ioctl.h> 31 #include <sys/socket.h> 32 #include <sys/tty.h> 33 34 #include <machine/cpu.h> 35 #include <netinet/in.h> 36 #include <arpa/inet.h> 37 38 #include <ctype.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <kvm.h> 43 #include <netdb.h> 44 #include <nlist.h> 45 #include <paths.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <tzfile.h> 50 #include <unistd.h> 51 #include <utmp.h> 52 53 #include "extern.h" 54 55 struct timeval boottime; 56 struct utmp utmp; 57 struct winsize ws; 58 kvm_t *kd; 59 time_t now; /* the current time of day */ 60 time_t uptime; /* time of last reboot & elapsed time since */ 61 int ttywidth; /* width of tty */ 62 int argwidth; /* width of tty */ 63 int header = 1; /* true if -h flag: don't print heading */ 64 int nflag; /* true if -n flag: don't convert addrs */ 65 int sortidle; /* sort bu idle time */ 66 char *sel_user; /* login of particular user selected */ 67 char domain[MAXHOSTNAMELEN]; 68 69 /* 70 * One of these per active utmp entry. 71 */ 72 struct entry { 73 struct entry *next; 74 struct utmp utmp; 75 dev_t tdev; /* dev_t of terminal */ 76 time_t idle; /* idle time of terminal in seconds */ 77 struct kinfo_proc *kp; /* `most interesting' proc */ 78 char *args; /* arg list of interesting process */ 79 } *ep, *ehead = NULL, **nextp = &ehead; 80 81 static void pr_header __P((time_t *, int)); 82 static struct stat 83 *ttystat __P((char *)); 84 static void usage __P((int)); 85 86 char *fmt_argv __P((char **, char *, int)); /* ../../bin/ps/fmt.c */ 87 88 int 89 main(argc, argv) 90 int argc; 91 char **argv; 92 { 93 extern char *__progname; 94 struct kinfo_proc *kp; 95 struct hostent *hp; 96 struct stat *stp; 97 FILE *ut; 98 u_long l; 99 int ch, i, nentries, nusers, wcmd; 100 char *memf, *nlistf, *p, *x; 101 char buf[MAXHOSTNAMELEN], errbuf[256]; 102 103 /* Are we w(1) or uptime(1)? */ 104 p = __progname; 105 if (*p == '-') 106 p++; 107 if (*p == 'u') { 108 wcmd = 0; 109 p = ""; 110 } else { 111 wcmd = 1; 112 p = "hiflM:N:nsuw"; 113 } 114 115 memf = nlistf = NULL; 116 while ((ch = getopt(argc, argv, p)) != EOF) 117 switch (ch) { 118 case 'h': 119 header = 0; 120 break; 121 case 'i': 122 sortidle = 1; 123 break; 124 case 'M': 125 header = 0; 126 memf = optarg; 127 break; 128 case 'N': 129 nlistf = optarg; 130 break; 131 case 'n': 132 nflag = 1; 133 break; 134 case 'f': case 'l': case 's': case 'u': case 'w': 135 warnx("[-flsuw] no longer supported"); 136 /* FALLTHROUGH */ 137 case '?': 138 default: 139 usage(wcmd); 140 } 141 argc -= optind; 142 argv += optind; 143 144 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL) 145 errx(1, "%s", errbuf); 146 147 (void)time(&now); 148 if ((ut = fopen(_PATH_UTMP, "r")) == NULL) 149 err(1, "%s", _PATH_UTMP); 150 151 if (*argv) 152 sel_user = *argv; 153 154 for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) { 155 if (utmp.ut_name[0] == '\0') 156 continue; 157 ++nusers; 158 if (wcmd == 0 || (sel_user && 159 strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0)) 160 continue; 161 if ((ep = calloc(1, sizeof(struct entry))) == NULL) 162 err(1, NULL); 163 *nextp = ep; 164 nextp = &(ep->next); 165 memmove(&(ep->utmp), &utmp, sizeof(struct utmp)); 166 stp = ttystat(ep->utmp.ut_line); 167 ep->tdev = stp->st_rdev; 168 #ifdef CPU_CONSDEV 169 /* 170 * If this is the console device, attempt to ascertain 171 * the true console device dev_t. 172 */ 173 if (ep->tdev == 0) { 174 int mib[2]; 175 size_t size; 176 177 mib[0] = CTL_MACHDEP; 178 mib[1] = CPU_CONSDEV; 179 size = sizeof(dev_t); 180 (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0); 181 } 182 #endif 183 if ((ep->idle = now - stp->st_atime) < 0) 184 ep->idle = 0; 185 } 186 (void)fclose(ut); 187 188 if (header || wcmd == 0) { 189 pr_header(&now, nusers); 190 if (wcmd == 0) 191 exit (0); 192 } 193 194 #define HEADER "USER TTY FROM LOGIN@ IDLE WHAT\n" 195 #define WUSED (sizeof (HEADER) - sizeof ("WHAT\n")) 196 (void)printf(HEADER); 197 198 if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL) 199 err(1, "%s", kvm_geterr(kd)); 200 for (i = 0; i < nentries; i++, kp++) { 201 struct proc *p = &kp->kp_proc; 202 struct eproc *e; 203 204 if (p->p_stat == SIDL || p->p_stat == SZOMB) 205 continue; 206 e = &kp->kp_eproc; 207 for (ep = ehead; ep != NULL; ep = ep->next) { 208 if (ep->tdev == e->e_tdev && e->e_pgid == e->e_tpgid) { 209 /* 210 * Proc is in foreground of this terminal 211 */ 212 if (proc_compare(&ep->kp->kp_proc, p)) 213 ep->kp = kp; 214 break; 215 } 216 } 217 } 218 if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 && 219 ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 && 220 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0) 221 ttywidth = 79; 222 else 223 ttywidth = ws.ws_col - 1; 224 argwidth = ttywidth - WUSED; 225 if (argwidth < 4) 226 argwidth = 8; 227 for (ep = ehead; ep != NULL; ep = ep->next) { 228 if (ep->kp == NULL) { 229 ep->args = "-"; 230 continue; 231 } 232 ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth), 233 ep->kp->kp_proc.p_comm, MAXCOMLEN); 234 if (ep->args == NULL) 235 err(1, NULL); 236 } 237 /* sort by idle time */ 238 if (sortidle && ehead != NULL) { 239 struct entry *from = ehead, *save; 240 241 ehead = NULL; 242 while (from != NULL) { 243 for (nextp = &ehead; 244 (*nextp) && from->idle >= (*nextp)->idle; 245 nextp = &(*nextp)->next) 246 continue; 247 save = from; 248 from = from->next; 249 save->next = *nextp; 250 *nextp = save; 251 } 252 } 253 254 if (!nflag) 255 if (gethostname(domain, sizeof(domain) - 1) < 0 || 256 (p = strchr(domain, '.')) == 0) 257 domain[0] = '\0'; 258 else { 259 domain[sizeof(domain) - 1] = '\0'; 260 memmove(domain, p, strlen(p) + 1); 261 } 262 263 for (ep = ehead; ep != NULL; ep = ep->next) { 264 p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-"; 265 if (x = strchr(p, ':')) 266 *x++ = '\0'; 267 if (!nflag && isdigit(*p) && 268 (long)(l = inet_addr(p)) != -1 && 269 (hp = gethostbyaddr((char *)&l, sizeof(l), AF_INET))) { 270 if (domain[0] != '\0') { 271 p = hp->h_name; 272 p += strlen(hp->h_name); 273 p -= strlen(domain); 274 if (p > hp->h_name && strcmp(p, domain) == 0) 275 *p = '\0'; 276 } 277 p = hp->h_name; 278 } 279 if (x) { 280 (void)snprintf(buf, sizeof(buf), "%s:%s", p, x); 281 p = buf; 282 } 283 (void)printf("%-*.*s %-2.2s %-*.*s ", 284 UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name, 285 strncmp(ep->utmp.ut_line, "tty", 3) ? 286 ep->utmp.ut_line : ep->utmp.ut_line + 3, 287 UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-"); 288 pr_attime(&ep->utmp.ut_time, &now); 289 pr_idle(ep->idle); 290 (void)printf("%.*s\n", argwidth, ep->args); 291 } 292 exit(0); 293 } 294 295 static void 296 pr_header(nowp, nusers) 297 time_t *nowp; 298 int nusers; 299 { 300 double avenrun[3]; 301 time_t uptime; 302 int days, hrs, i, mins; 303 int mib[2]; 304 size_t size; 305 char buf[256]; 306 307 /* 308 * Print time of day. 309 * 310 * SCCS forces the string manipulation below, as it replaces 311 * %, M, and % in a character string with the file name. 312 */ 313 (void)strftime(buf, sizeof(buf), 314 __CONCAT("%l:%","M%p"), localtime(nowp)); 315 (void)printf("%s ", buf); 316 317 /* 318 * Print how long system has been up. 319 * (Found by looking getting "boottime" from the kernel) 320 */ 321 mib[0] = CTL_KERN; 322 mib[1] = KERN_BOOTTIME; 323 size = sizeof(boottime); 324 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && 325 boottime.tv_sec != 0) { 326 uptime = now - boottime.tv_sec; 327 uptime += 30; 328 days = uptime / SECSPERDAY; 329 uptime %= SECSPERDAY; 330 hrs = uptime / SECSPERHOUR; 331 uptime %= SECSPERHOUR; 332 mins = uptime / SECSPERMIN; 333 (void)printf(" up"); 334 if (days > 0) 335 (void)printf(" %d day%s,", days, days > 1 ? "s" : ""); 336 if (hrs > 0 && mins > 0) 337 (void)printf(" %2d:%02d,", hrs, mins); 338 else { 339 if (hrs > 0) 340 (void)printf(" %d hr%s,", 341 hrs, hrs > 1 ? "s" : ""); 342 if (mins > 0) 343 (void)printf(" %d min%s,", 344 mins, mins > 1 ? "s" : ""); 345 } 346 } 347 348 /* Print number of users logged in to system */ 349 (void)printf(" %d user%s", nusers, nusers > 1 ? "s" : ""); 350 351 /* 352 * Print 1, 5, and 15 minute load averages. 353 */ 354 if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) 355 (void)printf(", no load average information available\n"); 356 else { 357 (void)printf(", load averages:"); 358 for (i = 0; i < (sizeof(avenrun) / sizeof(avenrun[0])); i++) { 359 if (i > 0) 360 (void)printf(","); 361 (void)printf(" %.2f", avenrun[i]); 362 } 363 (void)printf("\n"); 364 } 365 } 366 367 static struct stat * 368 ttystat(line) 369 char *line; 370 { 371 static struct stat sb; 372 char ttybuf[MAXPATHLEN]; 373 374 (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s", _PATH_DEV, line); 375 if (stat(ttybuf, &sb)) 376 err(1, "%s", ttybuf); 377 return (&sb); 378 } 379 380 static void 381 usage(wcmd) 382 int wcmd; 383 { 384 if (wcmd) 385 (void)fprintf(stderr, 386 "usage: w: [-hin] [-M core] [-N system] [user]\n"); 387 else 388 (void)fprintf(stderr, "uptime\n"); 389 exit (1); 390 } 391