xref: /illumos-gate/usr/src/cmd/acct/lib/devtolin.c (revision c9d5afdc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
25414388d7Ssl108498 /*
26414388d7Ssl108498  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27414388d7Ssl108498  * Use is subject to license terms.
28414388d7Ssl108498  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  *	convert device to linename (as in /dev/linename)
327c478bd9Sstevel@tonic-gate  *	return ptr to LSZ-byte string, "?" if not found
337c478bd9Sstevel@tonic-gate  *	device must be character device
347c478bd9Sstevel@tonic-gate  *	maintains small list in tlist structure for speed
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include "acctdef.h"
417c478bd9Sstevel@tonic-gate #include <dirent.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate #include <sys/stat.h>
447c478bd9Sstevel@tonic-gate 
45414388d7Ssl108498 static int tsize1;
467c478bd9Sstevel@tonic-gate static struct tlist {
477c478bd9Sstevel@tonic-gate 	char	tname[LSZ];	/* linename */
487c478bd9Sstevel@tonic-gate 	dev_t	tdev;		/* device */
497c478bd9Sstevel@tonic-gate } tl[TSIZE1];
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate char	*strncpy();
527c478bd9Sstevel@tonic-gate dev_t	lintodev();
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static char dev_dir[] = "/dev";
55*c9d5afdcSToomas Soome static char *def_srch_dirs[] = {
56*c9d5afdcSToomas Soome 	"/dev/term",
577c478bd9Sstevel@tonic-gate 	"/dev/pts",
587c478bd9Sstevel@tonic-gate 	"/dev/xt",
59*c9d5afdcSToomas Soome 	NULL
60*c9d5afdcSToomas Soome };
617c478bd9Sstevel@tonic-gate char file_name[MAX_DEV_PATH];	/* name being returned */
627c478bd9Sstevel@tonic-gate 
63414388d7Ssl108498 static int srch_dir();
64414388d7Ssl108498 
657c478bd9Sstevel@tonic-gate char *
devtolin(dev_t device)66*c9d5afdcSToomas Soome devtolin(dev_t device)
677c478bd9Sstevel@tonic-gate {
68*c9d5afdcSToomas Soome 	struct tlist *tp;
697c478bd9Sstevel@tonic-gate 	char **srch_dirs;	/* priority directories to search first */
707c478bd9Sstevel@tonic-gate 	int found = 0;
717c478bd9Sstevel@tonic-gate 	int dirno = 0;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	for (tp = tl; tp < &tl[tsize1]; tp++)
747c478bd9Sstevel@tonic-gate 		if (device == tp->tdev)
757c478bd9Sstevel@tonic-gate 			return (tp->tname);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	srch_dirs = def_srch_dirs;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	while ((!found) && (srch_dirs[dirno] != NULL)) {
80*c9d5afdcSToomas Soome 		/*
81*c9d5afdcSToomas Soome 		 * if /dev is one of the priority directories we should only
82*c9d5afdcSToomas Soome 		 * search its top level for now (set depth = MAX_SEARCH_DEPTH)
83*c9d5afdcSToomas Soome 		 */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 		found = srch_dir(device, srch_dirs[dirno],
867c478bd9Sstevel@tonic-gate 		    ((strcmp(srch_dirs[dirno], dev_dir) == 0) ?
87*c9d5afdcSToomas Soome 		    MAX_SRCH_DEPTH : 1), NULL);
887c478bd9Sstevel@tonic-gate 		dirno++;
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
91*c9d5afdcSToomas Soome 	/*
92*c9d5afdcSToomas Soome 	 * if not yet found search remaining /dev directory skipping the
93*c9d5afdcSToomas Soome 	 * priority directories
94*c9d5afdcSToomas Soome 	 */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (!found)
977c478bd9Sstevel@tonic-gate 		found = srch_dir(device, dev_dir, 0, srch_dirs);
987c478bd9Sstevel@tonic-gate 
99*c9d5afdcSToomas Soome 	/*
100*c9d5afdcSToomas Soome 	 * if found then put it (without the "/dev/" prefix) in the tlist
101*c9d5afdcSToomas Soome 	 * structure and return the path name without the "/dev/" prefix
102*c9d5afdcSToomas Soome 	 */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (found) {
1057c478bd9Sstevel@tonic-gate 		if (tsize1 < TSIZE1) {
1067c478bd9Sstevel@tonic-gate 			tp->tdev = device;
1077c478bd9Sstevel@tonic-gate 			CPYN(tp->tname, file_name+5);
1087c478bd9Sstevel@tonic-gate 			tsize1++;
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		return (file_name+5);
111*c9d5afdcSToomas Soome 	} else {
112*c9d5afdcSToomas Soome 		/*
113*c9d5afdcSToomas Soome 		 * if not found put "?" in the tlist structure for that device
114*c9d5afdcSToomas Soome 		 * and return "?"
115*c9d5afdcSToomas Soome 		 */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 		if (tsize1 < TSIZE1) {
1187c478bd9Sstevel@tonic-gate 			tp->tdev = device;
1197c478bd9Sstevel@tonic-gate 			CPYN(tp->tname, "?");
1207c478bd9Sstevel@tonic-gate 			tsize1++;
1217c478bd9Sstevel@tonic-gate 		}
122*c9d5afdcSToomas Soome 	}
1237c478bd9Sstevel@tonic-gate 	return ("?");
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
126*c9d5afdcSToomas Soome /*
127*c9d5afdcSToomas Soome  * Arguments:
128*c9d5afdcSToomas Soome  *	device		device we are looking for
129*c9d5afdcSToomas Soome  *	path		current path
130*c9d5afdcSToomas Soome  *	depth		current depth
131*c9d5afdcSToomas Soome  *	skip_dirs	directories that don't need searched
132*c9d5afdcSToomas Soome  */
1337c478bd9Sstevel@tonic-gate static int
srch_dir(dev_t device,char * path,int depth,char * skip_dirs[])134*c9d5afdcSToomas Soome srch_dir(dev_t device, char *path, int depth, char *skip_dirs[])
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	DIR *fdev;
1377c478bd9Sstevel@tonic-gate 	struct dirent *d;
1387c478bd9Sstevel@tonic-gate 	int dirno = 0;
1397c478bd9Sstevel@tonic-gate 	int found = 0;
1407c478bd9Sstevel@tonic-gate 	int path_len;
1417c478bd9Sstevel@tonic-gate 	char *last_comp;
1427c478bd9Sstevel@tonic-gate 	struct stat sb;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/* do we need to search this directory? */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if ((skip_dirs != NULL) && (depth != 0))
1477c478bd9Sstevel@tonic-gate 		while (skip_dirs[dirno] != NULL)
1487c478bd9Sstevel@tonic-gate 			if (strcmp(skip_dirs[dirno++], path) == 0)
1497c478bd9Sstevel@tonic-gate 				return (0);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/* open the directory */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if ((fdev = opendir(path)) == NULL)
1557c478bd9Sstevel@tonic-gate 		return (0);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/* initialize file name using path name */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	path_len = strlen(path);
1607c478bd9Sstevel@tonic-gate 	strcpy(file_name, path);
1617c478bd9Sstevel@tonic-gate 	last_comp = file_name + path_len;
1627c478bd9Sstevel@tonic-gate 	*last_comp++ = '/';
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/* start searching this directory */
1657c478bd9Sstevel@tonic-gate 
166*c9d5afdcSToomas Soome 	while ((!found) && ((d = readdir(fdev)) != NULL)) {
1677c478bd9Sstevel@tonic-gate 		if (d->d_ino != 0) {
1687c478bd9Sstevel@tonic-gate 
169*c9d5afdcSToomas Soome 			/*
170*c9d5afdcSToomas Soome 			 * if name would not be too long append it to
171*c9d5afdcSToomas Soome 			 * directory name, otherwise skip this entry
172*c9d5afdcSToomas Soome 			 */
1737c478bd9Sstevel@tonic-gate 
174*c9d5afdcSToomas Soome 			if ((int)(path_len + strlen(d->d_name) + 2) >
175*c9d5afdcSToomas Soome 			    MAX_DEV_PATH)
1767c478bd9Sstevel@tonic-gate 				continue;
1777c478bd9Sstevel@tonic-gate 			else
1787c478bd9Sstevel@tonic-gate 				strcpy(last_comp, d->d_name);
1797c478bd9Sstevel@tonic-gate 
180*c9d5afdcSToomas Soome 			/*
181*c9d5afdcSToomas Soome 			 * if this directory entry has the device number we
182*c9d5afdcSToomas Soome 			 * need, then the name is found. Otherwise if it's a
183*c9d5afdcSToomas Soome 			 * directory (not . or ..) and we haven't gone too
184*c9d5afdcSToomas Soome 			 * deep, recurse.
185*c9d5afdcSToomas Soome 			 */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 			if (lintodev(file_name+5) == device) {
1887c478bd9Sstevel@tonic-gate 				found = 1;
1897c478bd9Sstevel@tonic-gate 				break;
1907c478bd9Sstevel@tonic-gate 			} else if ((depth < MAX_SRCH_DEPTH) &&
1917c478bd9Sstevel@tonic-gate 			    (strcmp(d->d_name, ".") != 0) &&
1927c478bd9Sstevel@tonic-gate 			    (strcmp(d->d_name, "..") != 0) &&
1937c478bd9Sstevel@tonic-gate 			    (stat(file_name, &sb) != -1) &&
194*c9d5afdcSToomas Soome 			    ((sb.st_mode & S_IFMT) == S_IFDIR)) {
195*c9d5afdcSToomas Soome 				found = srch_dir(device, file_name, depth+1,
196*c9d5afdcSToomas Soome 				    skip_dirs);
197*c9d5afdcSToomas Soome 			}
198*c9d5afdcSToomas Soome 		}
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	closedir(fdev);
2017c478bd9Sstevel@tonic-gate 	return (found);
2027c478bd9Sstevel@tonic-gate }
203