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