File: main.c

package info (click to toggle)
lslk 1.29-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 516 kB
  • ctags: 758
  • sloc: ansic: 5,537; sh: 856; makefile: 479
file content (289 lines) | stat: -rw-r--r-- 6,616 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * main.c -- main lslk function
 *
 * V. Abell
 * Purdue University Computing Center
 */


/*
 * Copyright 1996 Purdue Research Foundation, West Lafayette, Indiana
 * 47907.  All rights reserved.
 *
 * Written by Victor A. Abell.
 *
 * This software is not subject to any license of the American Telephone
 * and Telegraph Company or the Regents of the University of California.
 *
 * Permission is granted to anyone to use this software for any purpose on
 * any computer system, and to alter it and redistribute it freely, subject
 * to the following restrictions:
 *
 * 1. Neither the authors nor Purdue University are responsible for any
 *    consequences of the use of this software.
 *
 * 2. The origin of this software must not be misrepresented, either by
 *    explicit claim or by omission.  Credit to the authors and Purdue
 *    University must appear in documentation and sources.
 *
 * 3. Altered versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 *
 * 4. This notice may not be removed or altered.
 */
#ifndef lint
static char copyright[] =
"@(#) Copyright 1996 Purdue Research Foundation.\nAll rights reserved.\n";
static char *rcsid = "$Id: main.c,v 1.6 99/11/10 14:56:01 abe Exp $";
#endif


#include "lslk.h"


/*
 * main() -- the main lslk function
 */

main(argc, argv)
	int argc;				/* argument count */
	char *argv[];				/* arguments */
{
	int c, err, i, lherr;
	struct lhaddr *lh;
	static int nlfa = 0;
	MALLOC_S len;
	char options[64];
	char *path;
	struct stat sb;
	struct llock_info **sp;
/*
 * Get the program name.
 */
	if ((Pn = strrchr(argv[0], '/')))
	    Pn++;
	else
	    Pn = argv[0];
/*
 * Common initialization.
 */
	if ((Mygid = (gid_t)getgid()) != getegid())
	    Setgid = 1;
	if ((Myuid = (uid_t)getuid()) && !geteuid())
	    Setuidroot = 1;
/*
 * Process options.
 */
	(void) sprintf(options, "abhi:%snOp:S:vw",

#if	defined(HASKOPT)
	    "k:"
#else	/* !defined(HASKOPT) */
	    ""
#endif	/* defined(HASKOPT) */

	    );

	err = 0;
	while ((c = getopt(argc, argv, options)) != EOF) {
	    switch (c) {
	    case 'a':		/* -a: AND selection options */
		Oand = 1;
		break;
	    case 'b':		/* -b: avoid kernel blocks */
		Oblock = 1;
		break;
	    case 'h':		/* -h: help */
		Ohelp = 1;
		break;
	    case 'i':		/* -i: specify Internet host names/addresses */
		if (enter_nwad(optarg))
		    err++;
		break;

#if	defined(HASKOPT)
	    case 'k':		/* -k: set optional kernel name list path  */
		Nmlst = optarg;
		break;
#endif	/* defined(HASKOPT) */

	    case 'n':		/* -n: don't convert host names to addresses */
		Oconv = 0;
		break;
	    case 'O':		/* -O avoid fork overhead *RISKY* */
		Oovhd = 1;
		break;
	    case 'p':		/* -p: specify PID numbers */
		if (enter_pid(optarg))
		    err++;
		break;
	    case 'S':		/* -S<value>: set readlink()/stat() timeout
				 *	      value */
		if ((TmLimit = atoi(optarg)) < TMLIMMIN) {
		    (void) fprintf(stderr,
			"%s: WARNING: -S time (%d) changed to %d\n",
			Pn, TmLimit, TMLIMMIN);
		    TmLimit = TMLIMMIN;
		}
		break;
	    case 'v':		/* -v: display version information */
		Overs = 1;
		break;
	    case 'w':		/* -w: suppress warnings */
		Owarn = 0;
		break;
	    case '?':
		err++;
		break;
	    default:
		(void) fprintf(stderr, "%s: unknown option character: %c\n",
		    Pn, (char)c);
		err++;
	    }
	} 
/*
 * Store file selections.
 */
	for (; optind < argc; optind++) {
	    if ((path = Readlink(argv[optind])) == (char *)NULL) {
		err++;
		continue;
	    }
	    if (statsafely(path, &sb) != 0) {
		if (path == argv[optind])
		    (void) fprintf(stderr, "%s: can't stat %s: %s\n",
			Pn, argv[optind], strerror(errno));
		else
		    (void) fprintf(stderr,
			"%s: can't stat %s (symbolic link to %s): %s\n",
			Pn, argv[optind], path, strerror(errno));
		err++;
		continue;
	    }
	/*
	 * Allocate local file table space, as required.
	 */
	    if (NLfile >= nlfa) {
		nlfa += LFILEAU;
		len = (MALLOC_S)(nlfa * sizeof(struct lfile));
		if (Lfile)
		    Lfile = (struct lfile *)realloc(Lfile, len);
		else
		    Lfile = (struct lfile *)malloc(len);
		if (!Lfile) {
		    (void) fprintf(stderr, "%s: no space for %d local files\n",
			Pn, nlfa);
		    Exit(1);
		}
	    }
	/*
	 * Store local file information.
	 */
	    Lfile[NLfile].path = path;
	    Lfile[NLfile].dev = sb.st_dev;
	    Lfile[NLfile].inum = sb.st_ino;
	    Lfile[NLfile].f = 0;
	    NLfile++;
	}
/*
 * Convert specified host names to network numbers; convert specified
 * network numbers to host names.
 */
	if (Oconv && Nwad) {
	    for (i = 0; i < NNwad; i++) {
		if (!Nwad[i].hn && Nwad[i].na) {
		    lh = get_haddr(1, (char *)NULL, Nwad[i].na, &lherr);
		    Nwad[i].chn = Nwad[i].hn = lh->hn;
		    if (lherr && Owarn) {
			(void) fprintf(stderr,
			    "%s: WARNING: can't get name of %s\n",
			    Pn, lh->hn);
		    }
		} else if (Nwad[i].hn && !Nwad[i].na) {
		    lh = get_haddr(0, Nwad[i].hn, (unsigned long)0, &lherr);
		    Nwad[i].na = lh->na;
		    Nwad[i].chn = lh->hn;
		    if (lherr && Owarn) {
			(void) fprintf(stderr,
			   "%s: WARNING: can't get address of %s\n",
			   Pn, lh->hn);
		    }
		}
	    }
	}
/*
 * Check argument processing results.
 */
	if (err || Ohelp || Overs)
	    usage(err);
/*
 * Identify the selection options.
 */
	Selopt = 0;
	if (Nwad)
	    Selopt |= SELNWAD;
	if (Lfile)
	    Selopt |= SELFILE;
	if (Pid)
	    Selopt |= SELPID;
/*
 * Construct the local mount table.
 */
	if (readmnt())
	    Exit(1);
/*
 * Do dialect-specific initialization.
 */
	(void) initialize();
/*
 * Gather local lock information and print it.
 */
	(void) gather_lock_info();
/*
 * Sort -- or at least build a pointer array to the lock information.
 */
	if (NLockU) {
	    len = (MALLOC_S)(NLockU * sizeof(struct llock_info *));
	    if (!(sp = (struct llock_info **)malloc(len))) {
		(void) fprintf(stderr,
		    "%s: no space for lock info pointers\n", Pn);
		Exit(1);
	    }
	    for (i = 0; i < NLockU; i++) {
		sp[i] = &Lock[i];
	    }
	    qsort((QSORT_P *)sp, (size_t)NLockU,
		(size_t)sizeof(struct llock_info *), comppid);
	    (void) print_lock_info(sp);
	}
/*
 * If file paths were specified, see if all were located and return
 * a 1 if they weren't.
 */
	if (NLfile) {
	    for (i = 0; i < NLfile; i++) {
		if (!Lfile[i].f)
		    Exit(1);
	    }
	}
	Exit(0);
}


/*
 * comppid() - compare PIDs
 */

int
comppid(a1, a2)
	COMP_P *a1, *a2;
{
	struct llock_info **p1 = (struct llock_info **)a1;
	struct llock_info **p2 = (struct llock_info **)a2;

	if ((*p1)->pid < (*p2)->pid)
		return(-1);
	if ((*p1)->pid > (*p2)->pid)
		return(1);
	return(0);
}