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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
|
/*
* 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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <stdarg.h>
#include <fcntl.h>
#include <stdlib.h>
#include <libnvpair.h>
#include <libdevinfo.h>
#include <syslog.h>
#include <sys/param.h>
#include <errno.h>
#include <assert.h>
#include <sys/systeminfo.h>
#include <sys/modctl.h>
#include <sys/fs/sdev_node.h>
#define LINEMAX 1024
#define SPC " \t\n"
#define QUOTES "\'\""
/*
* This is for local file supports of DBNR configurations.
*/
static int di_devname_getmapent_files(char *, char *, nvlist_t **);
static int di_devname_get_mapinfo_files(char *, nvlist_t **);
static int parse_mapinfo_file(FILE *, nvlist_t **);
static FILE *open_local_map(char *);
static void unquote(char *, char *);
static int msglog = 1;
typedef enum {
DBG_ERR = 1,
DBG_INFO,
DBG_STEP,
DBG_ALL
} debug_level_t;
static int devname_debug = 1;
static void dprintf(debug_level_t, const char *, ...);
extern int isspace(int);
/* exported interfaces */
void di_devname_print_mapinfo(nvlist_t *);
int di_devname_get_mapinfo(char *, nvlist_t **);
int di_devname_get_mapent(char *, char *, nvlist_t **);
int di_devname_action_on_key(nvlist_t *, uint8_t, char *, void *);
/*
* Returns 0 and the valid maplist, otherwise errno.
*/
int
di_devname_get_mapinfo_files(char *mapname, nvlist_t **maplist)
{
FILE *fp;
int rval = 0;
nvlist_t *nvl = NULL;
fp = open_local_map(mapname);
if (fp == NULL) {
dprintf(DBG_INFO, "di_devname_get_mapinfo_files: file %s does"
"not exist\n", mapname);
return (ENOENT);
}
rval = parse_mapinfo_file(fp, &nvl);
if (rval == 0) {
*maplist = nvl;
}
(void) fclose(fp);
return (rval);
}
static FILE *
open_local_map(char *mapname)
{
char filename[LINEMAX];
if (*mapname != '/') {
(void) snprintf(filename, sizeof (filename), "/etc/dev/%s",
mapname);
} else {
(void) snprintf(filename, sizeof (filename), "%s", mapname);
}
return (fopen(filename, "r"));
}
static void
unquote(char *str, char *qbuf)
{
register int escaped, inquote, quoted;
register char *ip, *bp, *qp;
char buf[LINEMAX];
escaped = inquote = quoted = 0;
for (ip = str, bp = buf, qp = qbuf; *ip; ip++) {
if (!escaped) {
if (*ip == '\\') {
escaped = 1;
quoted ++;
continue;
} else if (*ip == '"') {
inquote = !inquote;
quoted ++;
continue;
}
}
*bp++ = *ip;
*qp++ = (inquote || escaped) ? '^' : ' ';
escaped = 0;
}
*bp = '\0';
*qp = '\0';
if (quoted)
(void) strcpy(str, buf);
}
/*
* gets the qualified characters in *p into w, which has space allocated
* already
*/
static int
getword(char *w, char *wq, char **p, char **pq, char delim, int wordsz)
{
char *tmp = w;
char *tmpq = wq;
int count = wordsz;
if (wordsz <= 0) {
return (-1);
}
while ((delim == ' ' ? isspace(**p) : **p == delim) && **pq == ' ') {
(*p)++;
(*pq)++;
}
while (**p &&
!((delim == ' ' ? isspace(**p) : **p == delim) &&
**pq == ' ')) {
if (--count <= 0) {
*tmp = '\0';
*tmpq = '\0';
dprintf(DBG_INFO, "maximum word length %d exceeded\n",
wordsz);
return (-1);
}
*w++ = *(*p)++;
*wq++ = *(*pq)++;
}
*w = '\0';
*wq = '\0';
return (0);
}
static int
parse_mapinfo_file(FILE *fp, nvlist_t **ret_nvlp)
{
int error = 0;
nvlist_t *nvl = NULL, *attrs = NULL;
char line[LINEMAX], lineq[LINEMAX];
char word[MAXPATHLEN+1], wordq[MAXPATHLEN+1];
char *lp, *lq;
if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
return (EFAULT);
}
while (fgets(line, sizeof (line), fp)) {
char *name, *key, *val;
lp = (char *)line;
lq = (char *)lineq;
unquote(lp, lq);
if ((getword(word, wordq, &lp, &lq, ' ',
sizeof (word)) == -1) || (word[0] == '\0'))
continue;
if (word[0] == '#')
continue;
name = strtok(line, SPC);
if (name == NULL)
continue;
(void) dprintf(DBG_INFO, "get a line for %s\n", name);
key = strtok(NULL, "=");
if (key == NULL) {
(void) dprintf(DBG_INFO, "no attributes specified for "
"%s\n", name);
continue;
}
attrs = NULL;
if (nvlist_alloc(&attrs, NV_UNIQUE_NAME, 0) != 0) {
error = EFAULT;
goto fail1;
}
while (key && *key) {
char *rest;
rest = strtok(NULL, "\n");
if (rest == NULL) {
(void) dprintf(DBG_INFO, "no value for key "
"%s\n", key);
break;
}
if (rest[0] == ';') {
val = strdup("devname_null");
rest++;
} else {
val = strtok(rest, ";");
rest = strtok(NULL, "");
}
(void) dprintf(DBG_INFO, "parse_map_info: one entry "
"key=%s val=%s\n", key, val);
if (nvlist_add_string(attrs, key, val) != 0) {
error = EFAULT;
goto fail;
}
key = strtok(rest, "=");
}
(void) dprintf(DBG_INFO, "parse_map_info: add entry name=%s\n",
name);
if (nvlist_add_nvlist(nvl, name, attrs) != 0) {
error = EFAULT;
goto fail;
}
}
done:
*ret_nvlp = nvl;
return (0);
fail:
nvlist_free(attrs);
fail1:
nvlist_free(nvl);
return (error);
}
void
di_devname_print_mapinfo(nvlist_t *nvl)
{
char *name, *key, *val;
nvlist_t *attrs;
nvpair_t *nvp, *kvp;
nvp = nvlist_next_nvpair(nvl, NULL);
while (nvp) {
name = nvpair_name(nvp);
(void) nvpair_value_nvlist(nvp, &attrs);
(void) printf("name = %s, binding attributes:\n", name);
kvp = nvlist_next_nvpair(attrs, NULL);
while (kvp) {
key = nvpair_name(kvp);
(void) nvpair_value_string(kvp, &val);
(void) printf("\t%s = %s\n", key, val);
kvp = nvlist_next_nvpair(attrs, kvp);
}
nvp = nvlist_next_nvpair(nvl, nvp);
}
}
static int
action_mklink(char *target, char *source)
{
(void) dprintf(DBG_INFO, "mklink for source %s target %s\n",
source, target);
return (symlink(source, target));
}
static struct actions {
char *key;
devname_spec_t spec;
int (*action)(char *, char *);
} actions[] = {
{"devices-path", DEVNAME_NS_PATH, action_mklink},
{"dev-path", DEVNAME_NS_DEV, action_mklink},
{NULL, DEVNAME_NS_NONE, NULL}
};
static int
action_on_key(uint_t cmd, char *dir_name, char *devname, nvpair_t *attr,
uint32_t *nsmapcount, char **devfsadm_link, devname_spec_t *devfsadm_spec)
{
int i = 0;
int error = 0;
char *attrname, *attrval;
int len = 0;
char *path = NULL;
attrname = nvpair_name(attr);
(void) nvpair_value_string(attr, &attrval);
(void) dprintf(DBG_INFO, "key = %s; value = %s\n", attrname, attrval);
while (actions[i].key) {
if (strcmp(actions[i].key, attrname) == 0) {
switch (cmd) {
case DEVFSADMD_NS_READDIR:
len = strlen(dir_name) + strlen(devname) + 2;
path = malloc(len);
(void) snprintf(path, len, "%s/%s", dir_name,
devname);
error = actions[i].action(path, attrval);
free(path);
if (error) {
(void) dprintf(DBG_INFO, "action "
"failed %d\n", error);
return (error);
} else {
(*nsmapcount)++;
(void) dprintf(DBG_INFO,
"mapcount %d\n", *nsmapcount);
}
break;
case DEVFSADMD_NS_LOOKUP:
*devfsadm_link = strdup(attrval);
*devfsadm_spec = actions[i].spec;
break;
default:
break;
}
}
i++;
}
return (0);
}
int
di_devname_action_on_key(nvlist_t *map, uint8_t cmd, char *dir_name, void *hdl)
{
char *name = NULL;
nvpair_t *entry;
nvlist_t *attrs;
int32_t error = 0;
uint32_t ns_mapcount = 0;
char *devfsadm_link = NULL;
devname_spec_t devfsadm_spec = DEVNAME_NS_NONE;
sdev_door_res_t *resp;
entry = nvlist_next_nvpair(map, NULL);
while (entry) {
nvpair_t *attr;
name = nvpair_name(entry);
(void) dprintf(DBG_INFO, "di_devname_action_on_key: name %s\n",
name);
(void) nvpair_value_nvlist(entry, &attrs);
attr = nvlist_next_nvpair(attrs, NULL);
while (attr) {
error = action_on_key(cmd, dir_name, name, attr,
&ns_mapcount, &devfsadm_link, &devfsadm_spec);
/* do not continue if encountered the first error */
if (error) {
(void) dprintf(DBG_INFO, "error %d\n", error);
return ((int32_t)error);
}
attr = nvlist_next_nvpair(attrs, attr);
}
entry = nvlist_next_nvpair(map, entry);
}
resp = (sdev_door_res_t *)hdl;
(void) dprintf(DBG_INFO, "cmd is %d\n", cmd);
switch (cmd) {
case DEVFSADMD_NS_READDIR:
resp->ns_rdr_hdl.ns_mapcount = (uint32_t)ns_mapcount;
(void) dprintf(DBG_INFO, "mapcount is %d\n", ns_mapcount);
break;
case DEVFSADMD_NS_LOOKUP:
if (devfsadm_link && devfsadm_spec != DEVNAME_NS_NONE) {
(void) dprintf(DBG_INFO, "devfsadm_link is %s\n",
devfsadm_link);
(void) snprintf(resp->ns_lkp_hdl.devfsadm_link,
strlen(devfsadm_link) + 1, "%s", devfsadm_link);
resp->ns_lkp_hdl.devfsadm_spec = devfsadm_spec;
} else {
(void) dprintf(DBG_INFO, "error out\n");
return (1);
}
break;
default:
(void) dprintf(DBG_INFO, "error NOTSUP out\n");
return (ENOTSUP);
}
return (0);
}
static nvlist_t *
getent_mapinfo_file(FILE *fp, char *match)
{
nvlist_t *nvl, *attrs;
char line[LINEMAX], lineq[LINEMAX];
char word[MAXPATHLEN+1], wordq[MAXPATHLEN+1];
int count = 0;
char *lp, *lq;
if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
return (NULL);
while (fgets(line, sizeof (line), fp)) {
char *name, *key, *val;
if (line[0] == '#')
continue;
dprintf(DBG_INFO, "getent_mapinfo_file: get a line %s\n", line);
lp = (char *)line;
lq = (char *)lineq;
unquote(lp, lq);
if ((getword(word, wordq, &lp, &lq, ' ', sizeof (word))
== -1) || (word[0] == '\0'))
continue;
name = strtok(line, SPC);
if (name == NULL)
continue;
dprintf(DBG_INFO, "macthing with the key %s match %s\n",
name, match);
/* bypass the non-related entries */
if (strcmp(name, match) != 0)
continue;
/* get a matched entry */
key = strtok(NULL, "=");
if (key == NULL) {
(void) dprintf(DBG_INFO, "no attributes specified "
"for %s\n", name);
goto fail1;
}
attrs = NULL;
if (nvlist_alloc(&attrs, NV_UNIQUE_NAME, 0) != 0)
goto fail1;
while (key && *key) {
char *rest;
rest = strtok(NULL, "\n");
if (rest == NULL) {
(void) dprintf(DBG_INFO, "no value for key "
"%s\n", key);
goto fail;
}
if (rest[0] == ';') {
val = strdup("devname_null");
rest++;
} else {
val = strtok(rest, ";");
rest = strtok(NULL, "");
}
(void) dprintf(DBG_INFO, "found entry %s %s for %s\n",
key, val, name);
if (nvlist_add_string(attrs, key, val) != 0)
goto fail;
key = strtok(rest, "=");
}
(void) dprintf(DBG_INFO, "adding nvlist for %s\n", name);
if (nvlist_add_nvlist(nvl, name, attrs) != 0)
goto fail;
count++;
break;
}
if (count == 0)
goto fail1;
return (nvl);
fail:
nvlist_free(attrs);
fail1:
nvlist_free(nvl);
errno = EFAULT;
return (NULL);
}
static int
di_devname_getmapent_files(char *key, char *mapname, nvlist_t **map)
{
FILE *fp;
int rval = 0;
nvlist_t *nvl = NULL;
fp = open_local_map(mapname);
if (fp == NULL)
return (1);
nvl = getent_mapinfo_file(fp, key);
if (nvl != NULL) {
*map = nvl;
} else {
rval = errno;
}
(void) fclose(fp);
return (rval);
}
int
di_devname_get_mapent(char *key, char *mapname, nvlist_t **map)
{
dprintf(DBG_INFO, "di_devname_get_mapent: called for %s in %s\n",
key, mapname);
return (di_devname_getmapent_files(key, mapname, map));
}
int
di_devname_get_mapinfo(char *mapname, nvlist_t **maps)
{
dprintf(DBG_INFO, "di_devname_get_mapinfo: called for %s\n", mapname);
return (di_devname_get_mapinfo_files(mapname, maps));
}
static void
debug_print(debug_level_t msglevel, const char *fmt, va_list ap)
{
if (devname_debug < msglevel)
return;
/* Print a distinctive label for error msgs */
if (msglevel == DBG_ERR) {
(void) fprintf(stderr, "[ERROR]: ");
}
if (msglog == TRUE) {
(void) vsyslog(LOG_NOTICE, fmt, ap);
} else {
(void) vfprintf(stderr, fmt, ap);
}
}
/* ARGSUSED */
/* PRINTFLIKE2 */
static void
dprintf(debug_level_t msglevel, const char *fmt, ...)
{
va_list ap;
assert(msglevel > 0);
if (!devname_debug)
return;
va_start(ap, fmt);
debug_print(msglevel, fmt, ap);
va_end(ap);
}
/*
* Private interfaces for non-global /dev profile
*/
/*
* Allocate opaque data structure for passing profile to the kernel for
* the given mount point.
*
* Note that this interface returns an empty, initialized, profile.
* It does not return what may have been previously committed.
*/
int
di_prof_init(const char *mountpt, di_prof_t *profp)
{
nvlist_t *nvl;
if (nvlist_alloc(&nvl, 0, 0))
return (-1);
if (nvlist_add_string(nvl, SDEV_NVNAME_MOUNTPT, mountpt)) {
nvlist_free(nvl);
return (-1);
}
*profp = (di_prof_t)nvl;
return (0);
}
/*
* Free space allocated by di_prof_init().
*/
void
di_prof_fini(di_prof_t prof)
{
nvlist_free((nvlist_t *)prof);
}
/*
* Sends profile to the kernel.
*/
int
di_prof_commit(di_prof_t prof)
{
char *buf = NULL;
size_t buflen = 0;
int rv;
if (nvlist_pack((nvlist_t *)prof, &buf, &buflen, NV_ENCODE_NATIVE, 0))
return (-1);
rv = modctl(MODDEVNAME, MODDEVNAME_PROFILE, buf, buflen);
free(buf);
return (rv);
}
/*
* Add a device or directory to profile's include list.
*
* Note that there is no arbitration between conflicting
* include and exclude profile entries, most recent
* is the winner.
*/
int
di_prof_add_dev(di_prof_t prof, const char *dev)
{
if (nvlist_add_string((nvlist_t *)prof, SDEV_NVNAME_INCLUDE, dev))
return (-1);
return (0);
}
/*
* Add a device or directory to profile's exclude list.
* This can effectively remove a previously committed device.
*/
int
di_prof_add_exclude(di_prof_t prof, const char *dev)
{
if (nvlist_add_string((nvlist_t *)prof, SDEV_NVNAME_EXCLUDE, dev))
return (-1);
return (0);
}
/*
* Add a symlink to profile.
*/
int
di_prof_add_symlink(di_prof_t prof, const char *linkname, const char *target)
{
nvlist_t *nvl = (nvlist_t *)prof;
char *syml[2];
syml[0] = (char *)linkname; /* 1st entry must be the symlink */
syml[1] = (char *)target; /* 2nd entry must be the target */
if (nvlist_add_string_array(nvl, SDEV_NVNAME_SYMLINK, syml, 2))
return (-1);
return (0);
}
/*
* Add a name mapping to profile.
*/
int
di_prof_add_map(di_prof_t prof, const char *source, const char *target)
{
nvlist_t *nvl = (nvlist_t *)prof;
char *map[2];
map[0] = (char *)source; /* 1st entry must be the source */
map[1] = (char *)target; /* 2nd entry must be the target */
if (nvlist_add_string_array(nvl, SDEV_NVNAME_MAP, map, 2))
return (-1);
return (0);
}
|