summaryrefslogtreecommitdiff
path: root/usr/src/lib/libadm/common/getdgrp.c
blob: 3b5eb89da0374acb5a6bdd503d3776ee73328a62 (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/


/*
 * Copyright (c) 1997, by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*LINTLIBRARY*/

/*
 *  getdgrp.c
 *
 * Contains the following global functions:
 *	getdgrp()	Get the device groups that meet certain criteria.
 */

/*
 *  Header Files Referenced
 *	<sys/types.h>		Data Types
 *	<stdio.h>		Standard I/O definitions
 *	<string.h>		Character-string definitions
 *	<devmgmt.h>		Definitions for accessing device table files
 *	"devtab.h"		Local definitions for device tables
 */

#include	<sys/types.h>
#include	<stdio.h>
#include	<string.h>
#include	<stdlib.h>
#include	<devmgmt.h>
#include	"devtab.h"

/*
 *  Local definitions
 *	struct dgrplist		Structure that makes up the internal device
 *				group list
 *				Members:
 *				    name	Name of the device group
 *				    next	Pointer to the next in the list
 */

struct dgrplist {
	char			*name;
	struct dgrplist		*next;
};


/*
 *  Local functions
 *	initdgrplist		Initialize the internal device group list
 *	addtodgrplist		Add a device group to the device group list
 *	isindevlist		Does the device group contain a device?
 *	isincallerslist		Is a device group in the caller's list?
 *	buildreturnlist		Build list of device groups to return
 *	freedgrplist		Free the internal device group list
 */

static	void	initdgrplist(void);
static	int	addtodgrplist(struct dgrptabent *);
static	int	isindevlist(struct dgrptabent *, char **);
static	int	isincallerslist(struct dgrptabent *, char **);
static	char	**buildreturnlist(void);
static	void	freedgrplist(void);


/*
 *  Local data
 *	dgrplistfirst	First (dummy) node in the device group list
 *	dgrplistcount	Number of items in the device group list
 */

static	struct dgrplist	dgrplistfirst;
static	int		dgrplistcount;

/*
 * char **getdgrp(dgroups, criteria, options)
 *	char  **dgroups
 *	char  **criteria
 *	int	options
 *
 *	This function compiles a list of device groups containing devices
 *	that meet certain criteria and returns a pointer to the first
 *	item in that list.
 *
 *  Arguments:
 *	dgroups		The list of device groups to choose from or the list
 *			of device groups to exclude from the list (depends on
 *			"options"
 *	criteria	The criteria that a device must meet
 *	options		Indicates 1) whether to "and" the criteria or to "or"
 *			the criteria, 2) indicates whether to limit the
 *			generated list to "dgroups" or to exclude those
 *			device-groups from the list, 3) to list all device
 *			groups even if they don't contain valid devices.
 *
 *  Returns:  char **
 *	A pointer to the first address in the list of addresses of generated
 *	device groups
 */

char **
getdgrp(
	char	**dgroups,	/* List of device groups */
	char	**criteria,	/* List of criteria to meet */
	int	options)	/* Options governing the search */
{
	/*  Automatic data  */
	char			**devlist;	/* Devices that meet criteria */
	char			**plist;	/* Device groups to return */
	struct dgrptabent	*dgrp;		/* Dgrp information struct */
	int			errorflag;	/* TRUE if error occurred */
	int listallflag; /* TRUE if DTAB_LISTALL && (!criteria || !*criteria) */


	/*
	 *  Open the device-group table if needed
	 */

	if (!oam_dgroup && !_opendgrptab("r"))
		return (NULL);


	/*
	 *  Get the list of devices that meet the criteria specified
	 *  This step can be skipped if DTAB_LISTALL is requested and
	 *  there is no criteria list.
	 */

	if (((options & DTAB_LISTALL) == 0) || (criteria && *criteria)) {
	    devlist = getdev(NULL, criteria, (options & DTAB_ANDCRITERIA));
	    listallflag = FALSE;
	} else {
	    devlist = NULL;
	    listallflag = TRUE;
	}


	/*
	 *  Initialize the device group list (contains the device groups
	 *  we're accumulating)
	 */

	errorflag = FALSE;
	initdgrplist();


	/*
	 *  If no device groups were specified by the caller, accumulate all
	 *  device groups
	 */

	_setdgrptab();
	if (!dgroups || !(*dgroups)) {
	    while (!errorflag && (dgrp = _getdgrptabent())) {
		if (!dgrp->comment && (listallflag ||
		    isindevlist(dgrp, devlist)))
		    errorflag = !addtodgrplist(dgrp);
		_freedgrptabent(dgrp);
	    }
	}

	else {

	/*
	 *  If the exclusion flag is not set, build a list of device
	 *  groups that is a subset of those specified by the caller
	 */

	    if ((options & DTAB_EXCLUDEFLAG) == 0) {
		while (!errorflag && (dgrp = _getdgrptabent())) {
		    if (!dgrp->comment && isincallerslist(dgrp, dgroups) &&
			(listallflag || isindevlist(dgrp, devlist))) {
			errorflag = !addtodgrplist(dgrp);
		    }
		    _freedgrptabent(dgrp);
		}
	    }

		/*
		 *  If the exclusion flag is set, build a list of device groups
		 *  that meet the criteria and are not in the list of device
		 *  groups specified by the caller.
		 */
	    else {
		while (!errorflag && (dgrp = _getdgrptabent())) {
		    if (!dgrp->comment && !isincallerslist(dgrp, dgroups) &&
			(listallflag || isindevlist(dgrp, devlist))) {
			errorflag = !addtodgrplist(dgrp);
		    }
		    _freedgrptabent(dgrp);
		}
	    }
	}
	plist = buildreturnlist();
	freedgrplist();
	_enddgrptab();
	return (plist);
}

/*
 *  int initdgrplist()
 *
 *	Initializes the internal device group linked list
 *
 *  Arguments:  None
 *
 *  Returns:  void
 */

static void
initdgrplist(void)
{
	/*  Automatic data  */

	/*
	 *  Initialize the structure.  Dummy node points to nothing, count to
	 * zero.
	 */
	dgrplistcount = 0;
	dgrplistfirst.name = "";
	dgrplistfirst.next = NULL;
}

/*
 *  int addtodgrplist(dgrp)
 *	struct dgrptabent *dgrp
 *
 *	Adds the device group described by the "dgrp" structure to the
 *	internal list of device-groups we're accumulating.
 *
 *  Arguments:
 *	dgrp	Describes the device-group we're adding
 *
 *  Returns: int
 *	TRUE if successful, FALSE otherwise
 */

static int
addtodgrplist(struct dgrptabent *dgrp)
{
	/*  Automatic data  */
	struct dgrplist *newnode;	/* Allocated node */
	struct dgrplist	*p;		/* Running dgrp list ptr */
	struct dgrplist	*q;		/* Another Running dgrp list ptr */
	char		*newstr;	/* Space for the dgroup name */
	int		errorflag;	/* TRUE if error */
	int		cmpval;		/* Value from strcmp() */

	/*  No errors seen yet  */
	errorflag = FALSE;

	/*  Find where we're supposed to insert this item in the list  */
	q = &dgrplistfirst;
	p = q->next;
	while (p && ((cmpval = strcmp(p->name, dgrp->name)) < 0)) {
	    q = p;
	    p = p->next;
	}

	/*  If the item isn't already in the list, insert it  */
	if ((p == NULL) || (cmpval != 0)) {

	    /* Allocate space for the structure */
	    newnode = malloc(sizeof (struct dgrplist));
	    if (newnode) {

		/* Allocate space for the device group name */
		if (newstr = malloc(strlen(dgrp->name)+1)) {

		    /* Link the new structure into the list */
		    newnode->name = strcpy(newstr, dgrp->name);
		    newnode->next = p;
		    q->next = newnode;
		    dgrplistcount++;
		} else {
		    /* No space for the string.  Clean up */
		    errorflag = TRUE;
		    free(newnode);
		}
	    } else errorflag = TRUE;
	}

	/* Return a value that indicates whether we've had an error */
	return (!errorflag);
}

/*
 *  int isindevlist(dgrp, devlist)
 *	struct dgrptabent *dgrp
 *	char		 **devlist
 *
 *	This function searches the device membership list of the device
 *	group <dgrp> for any of the devices listed in the list of devices
 *	<devlist>.  It returns TRUE if at least one device in <devlist> is
 *	found in <dgrp>, otherwise it returns false.
 *
 *  Arguments:
 *	dgrp		The device group to examine
 *	devlist		The list of devices to search for
 *
 *  Returns:  int
 *	TRUE if one of the devices in <devlist> is a member of the device
 *	group <dgrp>, FALSE otherwise
 */

static int
isindevlist(
	struct dgrptabent *dgrp,	/* Dgrp to search for */
	char		**devlist)	/* List of devices to search against */
{
	/*  Automatic data  */
	struct member *pmbr;	/* Next member of the dgrp list */
	char **pdev;		/* Next device in the dev list */
	char *mbralias;		/* The alias of a group member */
	int cmpval;		/* strcmp() result */
	int notfound;		/* TRUE if no mbr of dgrp is in dev list */
	int allocflag;		/* TRUE if the mbralias string is malloc()ed */


	/*
	 *  For each device in the device group, search the alphabetically
	 *  sorted list of devices for that device.
	 */

	notfound = TRUE;
	for (pmbr = dgrp->membership; notfound && pmbr; pmbr = pmbr->next) {

	/*
	 * Get the member's alias (we've got it if the member is not a
	 * pathname)
	 */
	    allocflag = (*pmbr->name == '/');
	    if (allocflag)
		mbralias = devattr(pmbr->name, DTAB_ALIAS);
	    else mbralias = pmbr->name;

	    /* If we've got a member alias, search the device list for it */
	    if (mbralias)
		for (pdev = devlist; notfound && *pdev; pdev++)

		if ((cmpval = strcmp(mbralias, *pdev)) == 0) notfound = FALSE;
		else if (cmpval < 0)
			break;	/* Optimization:  alpha sorted list */

		/*
		 * Free the space allocated to the member alias
		 * (if it was allocated above by devattr())
		 */
	    if (allocflag) free(mbralias);

	}


	/*
	 *  Return a value indicating that we the device group contains
	 *  a member that is in the list of devices
	 */

	return (!notfound);
}

/*
 * int isincallerslist(dgrp, dgroups)
 *	struct dgrptabent *dgrp
 *	char		 **dgroups
 *
 *	This function looks through the "dgroups" list for the device
 *	group described by "dgrp"
 *
 *  Arguments:
 *	dgrp		Device group to search for
 *	dgroups		The address of the first item in the list of device
 *			groups to search
 *
 *  Returns:  int
 *	TRUE if found, FALSE otherwise
 */

static int
isincallerslist(
	struct dgrptabent *dgrp,	/* Dgrp to search for */
	char		**dgroups)	/* Caller's list of dgroups */
{
	/*  Automatic data  */
	char		**pdgrp;
	int		notfound;

	/*
	 *  Search the list of device groups for the name of the device group
	 *  in the structure described by <dgrp>.
	 */

	/*  Initializations  */
	notfound = TRUE;

	/*  Search the device group list for name of this device group  */
	for (pdgrp = dgroups; notfound && *pdgrp; pdgrp++) {
	    if (strcmp(dgrp->name, *pdgrp) == 0) notfound = FALSE;
	}

	/*  Return TRUE if the device group is in the list, FALSE otherwise  */
	return (!notfound);
}

/*
 *  char **buildreturnlist()
 *
 *	This function builds the list of pointers to device groups
 *	to return to the caller from the linked list of device-groups
 *	we've been accumulating.
 *
 *  Arguments:  none
 *
 *  Returns: char **
 *	A pointer to the first element in the malloc()ed list of pointers
 *	to malloc()ed character strings containing device groups which have
 *	member devices which match the criteria
 */

static char **
buildreturnlist(void)
{
	char		**list;		/* List being built */
	char		**pp;		/* Temp ptr within list */
	struct dgrplist	*pdgrpent;	/* Ptr into list of dgrps to return */

	/*  Allocate space for the list of pointers to device groups */
	list = malloc((dgrplistcount+1)*sizeof (char *));

	/*
	 *  For each item in the device group list, put an entry in the
	 *  list of names we're building
	 */
	if ((pp = list) != NULL) {
	    for (pdgrpent = dgrplistfirst.next; pdgrpent;
		pdgrpent = pdgrpent->next) {

		*pp++ = pdgrpent->name;
	    }
	    /*  The list ends with a null pointer  */
	    *pp = NULL;
	}

	/*  Return a pointer to the allocated list  */
	return (list);
}

/*
 *  void freedgrplist()
 *
 *	This function frees the resources allocated to the internal
 *	linked list of device groups
 *
 *  Arguments:  none
 *
 *  Returns:  void
 */

static void
freedgrplist(void)
{
	struct dgrplist		*pdgrpent;	/* Dgrp to free */
	struct dgrplist		*nextnode;	/* Next one to free */

	for (pdgrpent = dgrplistfirst.next; pdgrpent; pdgrpent = nextnode) {
	    nextnode = pdgrpent->next;
	    free(pdgrpent);
	}
}