summaryrefslogtreecommitdiff
path: root/usr/src/cmd/devfsadm/devalloc.c
blob: 8ad66cb688ab615b527c9e586ff8ec6695fccc7e (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (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 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Device allocation related work.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dkio.h>
#include <sys/wait.h>
#include <bsm/devalloc.h>

#define	DEALLOCATE	 "/usr/sbin/deallocate"
#define	MKDEVALLOC	"/usr/sbin/mkdevalloc"

static char *_update_dev(deventry_t *, int, const char *, char *, char *);
static int _make_db();
extern int event_driven;


/*
 * _da_check_for_usb
 *	returns 1 if device pointed by 'link' is a removable hotplugged disk,
 *	else returns 0.
 */
int
_da_check_for_usb(char *link, char *root_dir)
{
	int		fd = -1;
	int		len, dstsize;
	int		removable = 0;
	int		hotpluggable = 0;
	char		*p = NULL;
	char		path[MAXPATHLEN + 4];
	char		rpath[MAXPATHLEN + 4];		/* for ",raw" */

	dstsize = sizeof (path);
	if (strcmp(root_dir, "") != 0) {
		if (strlcat(path, root_dir, dstsize) >= dstsize)
			return (0);
		len = strlen(path);
	} else {
		len = 0;
	}
	(void) snprintf(path, dstsize - len, "%s", link);
	if ((p = realpath(path, rpath)) == NULL) {
		p = path;
	} else {
		if (strstr(link, "rdsk")) {
			p = rpath;
		} else {
			(void) snprintf(path, dstsize, "%s%s", rpath, ",raw");
			p = path;
		}
	}
	if ((fd = open(p, O_RDONLY | O_NONBLOCK)) < 0)
		return (0);
	(void) ioctl(fd, DKIOCREMOVABLE, &removable);
	(void) ioctl(fd, DKIOCHOTPLUGGABLE, &hotpluggable);
	(void) close(fd);

	if (removable && hotpluggable)
		return (1);

	return (0);
}

/*
 * _reset_devalloc
 *	If device allocation is being turned on, creates device_allocate
 *	device_maps if they do not exist.
 *	Puts DEVICE_ALLOCATION=ON/OFF in device_allocate to indicate if
 *	device allocation is on/off.
 */
void
_reset_devalloc(int action)
{
	da_args	dargs;

	if (action == DA_ON)
		(void) _make_db();
	else if ((action == DA_OFF) && (open(DEVALLOC, O_RDONLY) == -1))
		return;

	if (action == DA_ON)
		dargs.optflag = DA_ON;
	else if (action == DA_OFF)
		dargs.optflag = DA_OFF | DA_ALLOC_ONLY;

	dargs.rootdir = NULL;
	dargs.devnames = NULL;
	dargs.devinfo = NULL;

	(void) da_update_device(&dargs);
}

/*
 * _make_db
 *	execs /usr/sbin/mkdevalloc to create device_allocate and
 *	device_maps.
 */
static int
_make_db()
{
	int	status;
	pid_t	pid, wpid;

	pid = vfork();
	switch (pid) {
	case -1:
		return (1);
	case 0:
		if (execl(MKDEVALLOC, MKDEVALLOC, DA_IS_LABELED, NULL) == -1)
			exit((errno == ENOENT) ? 0 : 1);
		return (1);
	default:
		for (;;) {
			wpid = waitpid(pid, &status, 0);
			if (wpid == (pid_t)-1) {
				if (errno == EINTR)
					continue;
				else
					return (1);
			} else {
				break;
			}
		}
		break;
	}

	return ((WIFEXITED(status) == 0) ? 1 : WEXITSTATUS(status));
}


/*
 * _update_devalloc_db
 * 	Forms allocatable device entries to be written to device_allocate and
 *	device_maps.
 *
 *      Or finds the correct entry to remove, and removes it.
 *
 *    Note: devname is a /devices link in the REMOVE case.
 */
/* ARGSUSED */
void
_update_devalloc_db(devlist_t *devlist, int devflag, int action, char *devname,
    char *root_dir)
{
	int		i;
	deventry_t	*entry = NULL, *dentry = NULL;
	char 		*typestring;
	char 		*nickname;  /* typestring + instance */

	if (action == DA_ADD) {
		for (i = 0; i < DA_COUNT; i++) {
			switch (i) {
			case 0:
				dentry = devlist->audio;
				break;
			case 1:
				dentry = devlist->cd;
				break;
			case 2:
				dentry = devlist->floppy;
				break;
			case 3:
				dentry = devlist->tape;
				break;
			case 4:
				dentry = devlist->rmdisk;
				break;
			default:
				return;
			}
			if (dentry)
				(void) _update_dev(dentry, action, NULL, NULL,
				    NULL);
		}
	} else if (action == DA_REMOVE) {
		if (devflag & DA_AUDIO) {
			dentry = devlist->audio;
			typestring = DA_AUDIO_TYPE;
		} else if (devflag & DA_CD) {
			dentry = devlist->cd;
			typestring = DA_CD_TYPE;
		} else if (devflag & DA_FLOPPY) {
			dentry = devlist->floppy;
			typestring = DA_FLOPPY_TYPE;
		} else if (devflag & DA_TAPE) {
			dentry = devlist->tape;
			typestring = DA_TAPE_TYPE;
		} else if (devflag & DA_RMDISK) {
			dentry = devlist->rmdisk;
			typestring = DA_RMDISK_TYPE;
		} else
			return;

		if (event_driven) {
			nickname = _update_dev(NULL, action, typestring, NULL,
			    devname);

			if (nickname != NULL) {
				(void) da_rm_list_entry(devlist, devname,
				    devflag, nickname);
				free(nickname);
			}
			return;
		}
		/*
		 * Not reached as of now, could be reached if devfsadm is
		 * enhanced to clean up devalloc database more thoroughly.
		 * Will not reliably match for event-driven removes
		 */
		for (entry = dentry; entry != NULL; entry = entry->next) {
			if (strcmp(entry->devinfo.devname, devname) == 0)
				break;
		}
		(void) _update_dev(entry, action, NULL, devname, NULL);
	}
}

/*
 *	_update_dev: Update device_allocate and/or device_maps files
 *
 *      If adding a device:
 *	    dentry:	A linked list of allocatable devices
 *	    action:	DA_ADD or DA_REMOVE
 *	    devtype:	type of device linked list to update on removal
 *	    devname:	short name (i.e. rmdisk5, cdrom0)  of device if known
 *	    rm_link:	name of real /device from hot_cleanup
 *
 *	If the action is ADD or if the action is triggered by an event
 *      from syseventd,  read the files FIRST and treat their data as
 *      more-accurate than the dentry list, adjusting dentry contents if needed.
 *
 *	For DA_ADD, try to add each device in the list to the files.
 *
 *      If the action is DA_REMOVE and not a hotplug remove, adjust the files
 *	as indicated by the linked list.
 *
 *	RETURNS:
 *          If we successfully remove a device from the files,  returns
 *          a char * to strdup'd devname of the device removed.
 *
 *	    The caller is responsible for freeing the return value.
 *
 *	NULL for all other cases, both success and failure.
 *
 */
static char *
_update_dev(deventry_t *dentry, int action, const char *devtype, char *devname,
    char *rm_link)
{
	da_args		dargs;
	deventry_t	newentry, *entry;
	int status;

	dargs.rootdir = NULL;
	dargs.devnames = NULL;

	if (event_driven)
		dargs.optflag = DA_EVENT;
	else
		dargs.optflag = 0;

	if (action == DA_ADD) {
		dargs.optflag |= DA_ADD;
		/*
		 * Add Events do not have enough information to overrride the
		 * existing file contents.
		 */

		for (entry = dentry; entry != NULL; entry = entry->next) {
			dargs.devinfo = &(entry->devinfo);
			(void) da_update_device(&dargs);
		}
	} else if (action == DA_REMOVE) {
		dargs.optflag |= DA_REMOVE;
		if (dentry) {
			entry = dentry;
		} else if (dargs.optflag & DA_EVENT) {
			if (devname == NULL)
				newentry.devinfo.devname = NULL;
			else
				newentry.devinfo.devname = strdup(devname);
			newentry.devinfo.devtype = (char *)devtype;
			newentry.devinfo.devauths =
			    newentry.devinfo.devopts =
			    newentry.devinfo.devexec = NULL;
			newentry.devinfo.devlist = strdup(rm_link);
			newentry.devinfo.instance = 0;
			newentry.next = NULL;
			entry = &newentry;
		} else {
			newentry.devinfo.devname = strdup(devname);
			newentry.devinfo.devtype = (char *)devtype;
			newentry.devinfo.devauths =
			    newentry.devinfo.devexec =
			    newentry.devinfo.devopts =
			    newentry.devinfo.devlist = NULL;
			newentry.devinfo.instance = 0;
			newentry.next = NULL;
			entry = &newentry;
		}
		dargs.devinfo = &(entry->devinfo);
		/*
		 * da_update_device will fill in entry devname if
		 * event_driven is true and device is in the file
		 */
		status = da_update_device(&dargs);
		if (event_driven)
			if (newentry.devinfo.devlist != NULL)
				free(newentry.devinfo.devlist);
		if (status == 0)
			return (dargs.devinfo->devname);
		else free(dargs.devinfo->devname);
	}
	return (NULL);
}