1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999-2000 by Sun Microsystems, Inc. 24*7c478bd9Sstevel@tonic-gate * All rights reserved. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include "lastcomm.h" 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate /* 32*7c478bd9Sstevel@tonic-gate * lc_utils contains utility functions used by both the basic and extended 33*7c478bd9Sstevel@tonic-gate * accounting components of lastcomm. getdev(), on its first call, builds 34*7c478bd9Sstevel@tonic-gate * the set of tty device name to dev_t mappings. 35*7c478bd9Sstevel@tonic-gate */ 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #define N_DEVS 43 /* hash value for device names */ 38*7c478bd9Sstevel@tonic-gate #define NDEVS 500 /* max number of file names in /dev */ 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #define HASH(d) (((int)d) % N_DEVS) /* hash function */ 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate struct devhash { 43*7c478bd9Sstevel@tonic-gate dev_t dev_dev; 44*7c478bd9Sstevel@tonic-gate char dev_name [PATHNAMLEN]; 45*7c478bd9Sstevel@tonic-gate struct devhash *dev_nxt; 46*7c478bd9Sstevel@tonic-gate }; 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate static struct devhash *dev_hash[N_DEVS]; 49*7c478bd9Sstevel@tonic-gate static struct devhash *dev_chain; 50*7c478bd9Sstevel@tonic-gate static int ndevs = NDEVS; 51*7c478bd9Sstevel@tonic-gate static struct devhash *hashtab; 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate /* 54*7c478bd9Sstevel@tonic-gate * Default search list, used if /etc/ttysrch unavailable or unparsable. 55*7c478bd9Sstevel@tonic-gate */ 56*7c478bd9Sstevel@tonic-gate static char *def_srch_dirs[] = { 57*7c478bd9Sstevel@tonic-gate "/dev/term", 58*7c478bd9Sstevel@tonic-gate "/dev/pts", 59*7c478bd9Sstevel@tonic-gate "/dev/xt", 60*7c478bd9Sstevel@tonic-gate NULL 61*7c478bd9Sstevel@tonic-gate }; 62*7c478bd9Sstevel@tonic-gate static char *raw_sf; /* buffer containing raw image of the search file */ 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate #define SRCH_FILE_NAME "/etc/ttysrch" 65*7c478bd9Sstevel@tonic-gate /* 66*7c478bd9Sstevel@tonic-gate * /etc/ttysrch tokens. 67*7c478bd9Sstevel@tonic-gate */ 68*7c478bd9Sstevel@tonic-gate #define COMMENT_CHAR '#' 69*7c478bd9Sstevel@tonic-gate #define EOLN '\n' 70*7c478bd9Sstevel@tonic-gate /* 71*7c478bd9Sstevel@tonic-gate * /etc/ttysrch parser states. 72*7c478bd9Sstevel@tonic-gate */ 73*7c478bd9Sstevel@tonic-gate #define START_STATE 1 74*7c478bd9Sstevel@tonic-gate #define COMMENT_STATE 2 75*7c478bd9Sstevel@tonic-gate #define DIRNAME_STATE 3 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate /* 78*7c478bd9Sstevel@tonic-gate * The following 2 routines are modified version of get_pri_dirs 79*7c478bd9Sstevel@tonic-gate * and srch_dir in ttyname.c. 80*7c478bd9Sstevel@tonic-gate */ 81*7c478bd9Sstevel@tonic-gate static char ** 82*7c478bd9Sstevel@tonic-gate get_pri_dirs() 83*7c478bd9Sstevel@tonic-gate { 84*7c478bd9Sstevel@tonic-gate int bcount = 0; 85*7c478bd9Sstevel@tonic-gate int c; 86*7c478bd9Sstevel@tonic-gate int sf_lines = 0; /* number of lines in search file */ 87*7c478bd9Sstevel@tonic-gate int dirno = 0; 88*7c478bd9Sstevel@tonic-gate int state; 89*7c478bd9Sstevel@tonic-gate FILE *sf; 90*7c478bd9Sstevel@tonic-gate char **pri_dirs; /* priority search list */ 91*7c478bd9Sstevel@tonic-gate char *sfp; /* pointer inside the raw image buffer */ 92*7c478bd9Sstevel@tonic-gate struct stat sfsb; /* search file's stat structure buffer */ 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate if ((sf = fopen(SRCH_FILE_NAME, "r")) == NULL) 96*7c478bd9Sstevel@tonic-gate return (def_srch_dirs); 97*7c478bd9Sstevel@tonic-gate if (stat(SRCH_FILE_NAME, &sfsb) < 0) { 98*7c478bd9Sstevel@tonic-gate (void) fclose(sf); 99*7c478bd9Sstevel@tonic-gate return (def_srch_dirs); 100*7c478bd9Sstevel@tonic-gate } 101*7c478bd9Sstevel@tonic-gate raw_sf = malloc(sfsb.st_size + 1); 102*7c478bd9Sstevel@tonic-gate sfp = raw_sf; 103*7c478bd9Sstevel@tonic-gate while ((bcount++ < sfsb.st_size) && ((c = getc(sf)) != EOF)) { 104*7c478bd9Sstevel@tonic-gate *sfp++ = (char)c; 105*7c478bd9Sstevel@tonic-gate if (c == EOLN) 106*7c478bd9Sstevel@tonic-gate sf_lines++; 107*7c478bd9Sstevel@tonic-gate } 108*7c478bd9Sstevel@tonic-gate (void) fclose(sf); 109*7c478bd9Sstevel@tonic-gate *sfp = EOLN; 110*7c478bd9Sstevel@tonic-gate pri_dirs = malloc(++sf_lines * sizeof (char *)); 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate sfp = raw_sf; 113*7c478bd9Sstevel@tonic-gate state = START_STATE; 114*7c478bd9Sstevel@tonic-gate while (--bcount) { 115*7c478bd9Sstevel@tonic-gate switch (state) { 116*7c478bd9Sstevel@tonic-gate case START_STATE: 117*7c478bd9Sstevel@tonic-gate if (*sfp == COMMENT_CHAR) { 118*7c478bd9Sstevel@tonic-gate state = COMMENT_STATE; 119*7c478bd9Sstevel@tonic-gate } else if (!isspace(*sfp)) { 120*7c478bd9Sstevel@tonic-gate state = DIRNAME_STATE; 121*7c478bd9Sstevel@tonic-gate pri_dirs[dirno++] = sfp; 122*7c478bd9Sstevel@tonic-gate } 123*7c478bd9Sstevel@tonic-gate break; 124*7c478bd9Sstevel@tonic-gate case COMMENT_STATE: 125*7c478bd9Sstevel@tonic-gate if (*sfp == EOLN) 126*7c478bd9Sstevel@tonic-gate state = START_STATE; 127*7c478bd9Sstevel@tonic-gate break; 128*7c478bd9Sstevel@tonic-gate case DIRNAME_STATE: 129*7c478bd9Sstevel@tonic-gate if (*sfp == EOLN) { 130*7c478bd9Sstevel@tonic-gate *sfp = '\0'; 131*7c478bd9Sstevel@tonic-gate state = START_STATE; 132*7c478bd9Sstevel@tonic-gate } else if (isspace(*sfp)) { 133*7c478bd9Sstevel@tonic-gate *sfp = '\0'; 134*7c478bd9Sstevel@tonic-gate state = COMMENT_STATE; 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate break; 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate } /* switch */ 139*7c478bd9Sstevel@tonic-gate sfp++; 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate *sfp = '\0'; 143*7c478bd9Sstevel@tonic-gate pri_dirs[dirno] = NULL; 144*7c478bd9Sstevel@tonic-gate return (pri_dirs); 145*7c478bd9Sstevel@tonic-gate } 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate /* 148*7c478bd9Sstevel@tonic-gate * Build a chain of character devices in dev_chain, starting with the given 149*7c478bd9Sstevel@tonic-gate * path. 150*7c478bd9Sstevel@tonic-gate */ 151*7c478bd9Sstevel@tonic-gate static int 152*7c478bd9Sstevel@tonic-gate srch_dir(char *path) 153*7c478bd9Sstevel@tonic-gate { 154*7c478bd9Sstevel@tonic-gate DIR *dirp; 155*7c478bd9Sstevel@tonic-gate struct dirent *direntp; 156*7c478bd9Sstevel@tonic-gate struct stat st; 157*7c478bd9Sstevel@tonic-gate char file_name[PATHNAMLEN]; 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate if ((dirp = opendir(path)) == NULL) 160*7c478bd9Sstevel@tonic-gate return (0); 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate if ((readdir(dirp) == NULL) || (readdir(dirp) == NULL)) 163*7c478bd9Sstevel@tonic-gate return (0); 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate while ((direntp = readdir(dirp)) != NULL) { 166*7c478bd9Sstevel@tonic-gate (void) strcpy(file_name, path); 167*7c478bd9Sstevel@tonic-gate (void) strcat(file_name, "/"); 168*7c478bd9Sstevel@tonic-gate (void) strcat(file_name, direntp->d_name); 169*7c478bd9Sstevel@tonic-gate if (stat((const char *)file_name, &st) < 0) 170*7c478bd9Sstevel@tonic-gate continue; 171*7c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) == S_IFCHR) { 172*7c478bd9Sstevel@tonic-gate (void) strcpy(hashtab->dev_name, 173*7c478bd9Sstevel@tonic-gate file_name + strlen("/dev/")); 174*7c478bd9Sstevel@tonic-gate hashtab->dev_nxt = dev_chain; 175*7c478bd9Sstevel@tonic-gate dev_chain = hashtab; 176*7c478bd9Sstevel@tonic-gate hashtab++; 177*7c478bd9Sstevel@tonic-gate if (--ndevs < 0) 178*7c478bd9Sstevel@tonic-gate return (-1); 179*7c478bd9Sstevel@tonic-gate } 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate (void) closedir(dirp); 182*7c478bd9Sstevel@tonic-gate return (1); 183*7c478bd9Sstevel@tonic-gate } 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate 186*7c478bd9Sstevel@tonic-gate static void 187*7c478bd9Sstevel@tonic-gate setupdevs() 188*7c478bd9Sstevel@tonic-gate { 189*7c478bd9Sstevel@tonic-gate int dirno = 0; 190*7c478bd9Sstevel@tonic-gate char **srch_dirs; 191*7c478bd9Sstevel@tonic-gate 192*7c478bd9Sstevel@tonic-gate hashtab = malloc(NDEVS * sizeof (struct devhash)); 193*7c478bd9Sstevel@tonic-gate if (hashtab == NULL) { 194*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No memory for device table\n")); 195*7c478bd9Sstevel@tonic-gate return; 196*7c478bd9Sstevel@tonic-gate } 197*7c478bd9Sstevel@tonic-gate 198*7c478bd9Sstevel@tonic-gate srch_dirs = get_pri_dirs(); 199*7c478bd9Sstevel@tonic-gate 200*7c478bd9Sstevel@tonic-gate while (srch_dirs[dirno] != NULL) { 201*7c478bd9Sstevel@tonic-gate if (srch_dir(srch_dirs[dirno]) < 0) 202*7c478bd9Sstevel@tonic-gate return; 203*7c478bd9Sstevel@tonic-gate dirno++; 204*7c478bd9Sstevel@tonic-gate } 205*7c478bd9Sstevel@tonic-gate 206*7c478bd9Sstevel@tonic-gate dirno = 0; 207*7c478bd9Sstevel@tonic-gate while (srch_dirs[dirno] != NULL) { 208*7c478bd9Sstevel@tonic-gate if (strcmp("/dev", srch_dirs[dirno]) == 0) 209*7c478bd9Sstevel@tonic-gate /* 210*7c478bd9Sstevel@tonic-gate * Don't search /dev twice. 211*7c478bd9Sstevel@tonic-gate */ 212*7c478bd9Sstevel@tonic-gate return; 213*7c478bd9Sstevel@tonic-gate dirno++; 214*7c478bd9Sstevel@tonic-gate } 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate char * 218*7c478bd9Sstevel@tonic-gate getdev(dev_t dev) 219*7c478bd9Sstevel@tonic-gate { 220*7c478bd9Sstevel@tonic-gate struct devhash *hp, *nhp; 221*7c478bd9Sstevel@tonic-gate struct stat statb; 222*7c478bd9Sstevel@tonic-gate char name[PATHNAMLEN]; 223*7c478bd9Sstevel@tonic-gate static dev_t lastdev = (dev_t)-1; 224*7c478bd9Sstevel@tonic-gate static char *lastname; 225*7c478bd9Sstevel@tonic-gate static int init = 0; 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate if (dev == NODEV) 228*7c478bd9Sstevel@tonic-gate return ("__"); 229*7c478bd9Sstevel@tonic-gate if (dev == lastdev) 230*7c478bd9Sstevel@tonic-gate return (lastname); 231*7c478bd9Sstevel@tonic-gate if (!init) { 232*7c478bd9Sstevel@tonic-gate setupdevs(); 233*7c478bd9Sstevel@tonic-gate init++; 234*7c478bd9Sstevel@tonic-gate } 235*7c478bd9Sstevel@tonic-gate 236*7c478bd9Sstevel@tonic-gate for (hp = dev_hash[HASH(dev)]; hp; hp = hp->dev_nxt) 237*7c478bd9Sstevel@tonic-gate if (hp->dev_dev == dev) { 238*7c478bd9Sstevel@tonic-gate lastdev = dev; 239*7c478bd9Sstevel@tonic-gate return (lastname = hp->dev_name); 240*7c478bd9Sstevel@tonic-gate } 241*7c478bd9Sstevel@tonic-gate 242*7c478bd9Sstevel@tonic-gate for (hp = dev_chain; hp; hp = nhp) { 243*7c478bd9Sstevel@tonic-gate nhp = hp->dev_nxt; 244*7c478bd9Sstevel@tonic-gate (void) strcpy(name, "/dev/"); 245*7c478bd9Sstevel@tonic-gate (void) strcat(name, hp->dev_name); 246*7c478bd9Sstevel@tonic-gate if (stat(name, &statb) < 0) /* name truncated usually */ 247*7c478bd9Sstevel@tonic-gate continue; 248*7c478bd9Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) != S_IFCHR) 249*7c478bd9Sstevel@tonic-gate continue; 250*7c478bd9Sstevel@tonic-gate hp->dev_dev = statb.st_rdev; 251*7c478bd9Sstevel@tonic-gate hp->dev_nxt = dev_hash[HASH(hp->dev_dev)]; 252*7c478bd9Sstevel@tonic-gate dev_hash[HASH(hp->dev_dev)] = hp; 253*7c478bd9Sstevel@tonic-gate if (hp->dev_dev == dev) { 254*7c478bd9Sstevel@tonic-gate dev_chain = nhp; 255*7c478bd9Sstevel@tonic-gate lastdev = dev; 256*7c478bd9Sstevel@tonic-gate return (lastname = hp->dev_name); 257*7c478bd9Sstevel@tonic-gate } 258*7c478bd9Sstevel@tonic-gate } 259*7c478bd9Sstevel@tonic-gate dev_chain = NULL; 260*7c478bd9Sstevel@tonic-gate return ("??"); 261*7c478bd9Sstevel@tonic-gate } 262*7c478bd9Sstevel@tonic-gate 263*7c478bd9Sstevel@tonic-gate char * 264*7c478bd9Sstevel@tonic-gate flagbits(int f) 265*7c478bd9Sstevel@tonic-gate { 266*7c478bd9Sstevel@tonic-gate int i = 0; 267*7c478bd9Sstevel@tonic-gate static char flags[20]; 268*7c478bd9Sstevel@tonic-gate 269*7c478bd9Sstevel@tonic-gate #define BIT(flag, ch) flags[i++] = (f & flag) ? ch : ' ' 270*7c478bd9Sstevel@tonic-gate BIT(ASU, 'S'); 271*7c478bd9Sstevel@tonic-gate BIT(AFORK, 'F'); 272*7c478bd9Sstevel@tonic-gate flags[i] = '\0'; 273*7c478bd9Sstevel@tonic-gate return (flags); 274*7c478bd9Sstevel@tonic-gate #undef BIT 275*7c478bd9Sstevel@tonic-gate } 276*7c478bd9Sstevel@tonic-gate 277*7c478bd9Sstevel@tonic-gate char * 278*7c478bd9Sstevel@tonic-gate getname(uid_t uid) 279*7c478bd9Sstevel@tonic-gate { 280*7c478bd9Sstevel@tonic-gate struct passwd *pw; 281*7c478bd9Sstevel@tonic-gate static char uidname[NMAX]; 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate if ((pw = getpwuid(uid)) == NULL) { 284*7c478bd9Sstevel@tonic-gate (void) sprintf(uidname, "%ld", uid); 285*7c478bd9Sstevel@tonic-gate return (uidname); 286*7c478bd9Sstevel@tonic-gate } 287*7c478bd9Sstevel@tonic-gate return (pw->pw_name); 288*7c478bd9Sstevel@tonic-gate } 289