summaryrefslogtreecommitdiff
path: root/usr/src/cmd/devfsadm/devfsadm_impl.h
blob: 5e68e9236d23b6b4ae85008b5dfd2723033046fb (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
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
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _DEVFSADM_IMPL_H
#define	_DEVFSADM_IMPL_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <dlfcn.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/file.h>
#include <locale.h>
#include <libintl.h>
#include <ctype.h>
#include <signal.h>
#include <deflt.h>
#include <ftw.h>
#include <sys/instance.h>
#include <sys/types.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mkdev.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/int_types.h>
#include <limits.h>
#include <strings.h>
#include <devfsadm.h>
#include <libdevinfo.h>
#include <sys/devinfo_impl.h>
#include <sys/modctl.h>
#include <libgen.h>
#include <sys/hwconf.h>
#include <sys/sunddi.h>
#include <door.h>
#include <syslog.h>
#include <libsysevent.h>
#include <thread.h>
#include <message.h>
#include <sys/cladm.h>
#include <sys/sysevent/dev.h>
#include <libzonecfg.h>
#include <device_info.h>
#include <sys/fs/sdev_impl.h>
#include <sys/syscall.h>
#include <rpcsvc/ypclnt.h>
#include <sys/sysevent/eventdefs.h>

#define	DEV_LOCK_FILE ".devfsadm_dev.lock"
#define	DAEMON_LOCK_FILE ".devfsadm_daemon.lock"

#define	DEV "/dev"
#define	ETC "/etc"
#define	ETCDEV "/etc/dev"
#define	DEV_LEN 4
#define	DEVICES "/devices"
#define	DEVICES_LEN 8
#define	MODULE_DIRS "/usr/lib/devfsadm/linkmod"
#define	ALIASFILE "/etc/driver_aliases"
#define	NAME_TO_MAJOR "/etc/name_to_major"
#define	RECONFIG_BOOT "_INIT_RECONFIG"
#define	PID_STR_LEN 10
#define	EXTRA_PRIVS	"/etc/security/extra_privs"
#define	DEV_POLICY	"/etc/security/device_policy"
#define	LDEV_FILE	"/etc/logindevperm"

#define	DEVFSADM_DEFAULT_FILE "/etc/default/devfsadm"

#define	MINOR_FINI_TIMEOUT_DEFAULT 2

#define	SYNCH_DOOR_PERMS	(S_IRUSR | S_IWUSR)

#define	DRVCONFIG "drvconfig"
#define	DEVFSADM "devfsadm"
#define	DEVFSADMD "devfsadmd"
#define	DEVLINKS "devlinks"
#define	TAPES "tapes"
#define	AUDLINKS "audlinks"
#define	PORTS "ports"
#define	DISKS "disks"

#define	MAX_IDLE_DELAY 5
#define	MAX_DELAY 30
#define	NAME 0x01
#define	ADDR 0x03
#define	MINOR 0x04
#define	COUNTER 0x05
#define	CONSTANT 0x06
#define	TYPE 0x07
#define	TYPE_S "type"
#define	ADDR_S "addr"
#define	ADDR_S_LEN 4
#define	MINOR_S "minor"
#define	MINOR_S_LEN 5
#define	NAME_S "name"
#define	TAB '\t'
#define	NEWLINE '\n'
#define	MAX_DEVLINK_LINE 4028
#define	INTEGER 0
#define	LETTER 1
#define	MAX_PERM_LINE 256
#define	MAX_LDEV_LINE 256
#define	LDEV_DELIMS " \t\n"
#define	LDEV_DRVLIST_DELIMS "="
#define	LDEV_DRV_DELIMS ", \t\n"
#define	LDEV_DEV_DELIM ":"
#define	LDEV_DRVLIST_NAME "driver"
#define	NFP_HASH_SZ 256

#define	TYPE_LINK 0x00
#define	TYPE_DEVICES 0x01

#define	CREATE_LINK 0x01
#define	READ_LINK 0x02
#define	CREATE_NODE 0x01
#define	READ_NODE 0x02

#define	CACHE_STATE 0x0
#define	SYNC_STATE 0x1

#define	MODULE_ACTIVE 0x01

/* Possible flag values for flag member of numeral_t */
#define	NUMERAL_RESERVED 0x01

#define	MAX_SLEEP 120

#define	DEVLINKTAB_FILE "/etc/devlink.tab"

#define	MODULE_SUFFIX ".so"
#define	MINOR_INIT "minor_init"
#define	MINOR_FINI "minor_fini"
#define	_DEVFSADM_CREATE_REG "_devfsadm_create_reg"
#define	_DEVFSADM_REMOVE_REG "_devfsadm_remove_reg"

#define	NUM_EV_STR		4
#define	EV_TYPE			0
#define	EV_CLASS		1
#define	EV_PATH_NAME		2
#define	EV_MINOR_NAME		3

/* add new debug level and meanings here */
#define	DEVLINK_MID		"devfsadm:devlink"
#define	MODLOAD_MID		"devfsadm:modload"
#define	INITFINI_MID		"devfsadm:initfini"
#define	EVENT_MID		"devfsadm:event"
#define	REMOVE_MID		"devfsadm:remove"
#define	LOCK_MID		"devfsadm:lock"
#define	PATH2INST_MID		"devfsadm:path2inst"
#define	CACHE_MID		"devfsadm:cache"
#define	BUILDCACHE_MID		"devfsadm:buildcache"
#define	RECURSEDEV_MID		"devfsadm:recursedev"
#define	INSTSYNC_MID		"devfsadm:instsync"
#define	FILES_MID		"devfsadm:files"
#define	ENUM_MID		"devfsadm:enum"
#define	RSRV_MID		"devfsadm:rsrv"	/* enum interface reserve  */
#define	RSBY_MID		"devfsadm:rsby"	/* enum reserve bypass */
#define	LINKCACHE_MID		"devfsadm:linkcache"
#define	ADDREMCACHE_MID		"devfsadm:addremcache"
#define	MALLOC_MID		"devfsadm:malloc"
#define	READDIR_MID		"devfsadm:readdir"
#define	READDIR_ALL_MID		"devfsadm:readdir_all"
#define	DEVNAME_MID		"devfsadm:devname"
#define	ALL_MID			"all"

#define	DEVFSADM_DEBUG_ON	(verbose == NULL) ? FALSE : TRUE

typedef struct recurse_dev {
	void (*fcn)(char *, void *);
	void *data;
} recurse_dev_t;

typedef struct link {
	char *devlink; /* without ".../dev/"   prefix */
	char *contents; /* without "../devices" prefix */
	struct link *next;
} link_t;

typedef struct linkhead {
	regex_t dir_re_compiled;
	char *dir_re;
	link_t *link;
	link_t *nextlink;
	struct linkhead *nexthead;
} linkhead_t;

typedef struct link_list  {
	int type;
	char *constant;
	int arg;
	struct link_list *next;
} link_list_t;

typedef struct selector_list {
	int key;
	char *val;
	int arg;
	struct selector_list *next;
} selector_list_t;

typedef struct devlinktab_list {
	int line_number;
	char *selector_pattern;
	char *p_link_pattern;
	char *s_link_pattern;
	selector_list_t *selector;
	link_list_t *p_link;
	link_list_t *s_link;
	struct devlinktab_list *next;
} devlinktab_list_t;

typedef struct module {
	char *name;
	void *dlhandle;
	int (*minor_init)();
	int (*minor_fini)();
	int flags;
	struct module *next;
} module_t;

typedef struct create_list {
	devfsadm_create_t *create;
	module_t *modptr;
	regex_t node_type_comp;
	regex_t drv_name_comp;
	struct create_list *next;
} create_list_t;

struct minor {
	di_node_t node;
	di_minor_t minor;
	struct minor *next;
};

struct mlist {
	struct minor *head;
	struct minor *tail;
};

typedef struct remove_list {
	devfsadm_remove_V1_t *remove;
	module_t *modptr;
	struct remove_list *next;
} remove_list_t;

typedef struct item {
	char *i_key;
	struct item *i_next;
} item_t;

typedef struct cleanup_data {
	int flags;
	char *phypath;
	remove_list_t *rm;
} cleanup_data_t;

typedef struct n2m {
	major_t major;
	char *driver;
	struct n2m *next;
} n2m_t;

/* structures for devfsadm_enumerate() */
typedef struct numeral {
	char *id;
	char *full_path;
	int rule_index;
	char *cmp_str;
	struct numeral *next;
	int flags;
} numeral_t;

typedef struct numeral_set {
	int re_count;
	char **re;
	numeral_t *headnumeral;
	struct numeral_set *next;
} numeral_set_t;

typedef struct temp {
	int integer;
	struct temp *next;
} temp_t;

typedef struct driver_alias {
	char *driver_name;
	char *alias_name;
	struct driver_alias *next;
} driver_alias_t;

struct driver_list {
	char driver_name[MAXNAMELEN];
	struct driver_list *next;
};

struct login_dev {
	char *ldev_console;
	int ldev_perms;
	char *ldev_device;
	regex_t ldev_device_regex;
	struct driver_list *ldev_driver_list;
	struct login_dev *ldev_next;
};

#define	MAX_DEV_NAME_COUNT	100
struct devlink_cb_arg {
	char *dev_names[MAX_DEV_NAME_COUNT];
	char *link_contents[MAX_DEV_NAME_COUNT];
	int count;
	int rv;
};

struct dca_impl {
	char *dci_root;
	char *dci_minor;
	char *dci_driver;
	void *dci_arg;
	int dci_error;
	int dci_flags;
};

/* sysevent queue related */
typedef struct syseventq_s {
	struct syseventq_s *next;
	char *class;
	char *subclass;
	nvlist_t *nvl;
} syseventq_t;

static int devfsadm_enumerate_int_start(char *devfs_path,
	int index, char **buf, devfsadm_enumerate_t rules[],
	int nrules, char *start);
static void set_root_devices_dev_dir(char *dir);
static void pre_and_post_cleanup(int flags);
static void hot_cleanup(char *, char *, char *, char *, int);
static void devfsadm_exit(int status);
static void rm_link_from_cache(char *devlink);
static void rm_all_links_from_cache();
static void add_link_to_cache(char *devlink, char *physpath);
static linkhead_t *get_cached_links(char *dir_re);
static void build_devlink_list(char *check_link, void *data);
static void instance_flush_thread(void);
static int s_rmdir(char *path);
static void rm_parent_dir_if_empty(char *path);
static void free_link_list(link_list_t *head);
static void free_selector_list(selector_list_t *head);
void devfsadm_err_print(char *message, ...);
void defvsadm_print(int level, char *message, ...);
static int call_minor_init(module_t *module);
static void load_module(char *module, char *cdir);
static void invalidate_enumerate_cache(void);
static pid_t enter_dev_lock(void);
static void exit_dev_lock(int exiting);
static pid_t enter_daemon_lock(void);
static void exit_daemon_lock(int exiting);
static int process_devlink_compat(di_minor_t minor, di_node_t node);
static int alias(char *, char *);
static int devfsadm_copy(void);
static void flush_path_to_inst(void);
static void detachfromtty(void);
static void minor_process(di_node_t node, di_minor_t minor,
    struct mlist *dep);
static void read_minor_perm_file(void);
static void read_driver_aliases_file(void);
static void load_modules(void);
static void unload_modules(void);
static void *s_malloc(const size_t size);
static void *s_zalloc(const size_t size);
static void devfs_instance_mod(void);
static void add_minor_pathname(char *, char *, char *);
static int check_minor_type(di_node_t node, di_minor_t minor, void *arg);
static void cache_deferred_minor(struct mlist *dep, di_node_t node,
    di_minor_t minor);
static int compare_field(char *full_name, char *field_item, int field);
static int component_cat(char *link, char *name, int field);
static void recurse_dev_re(char *current_dir, char *path_re, recurse_dev_t *rd);
static void matching_dev(char *devpath, void *data);
static int resolve_link(char *devpath, char **content_p, int *type_p,
    char **devfs_path, int dangle);
static int clean_ok(devfsadm_remove_V1_t *remove);
static int translate_major(dev_t old_dev, dev_t *new_dev);
static int get_major_no(char *driver, major_t *major);
static int load_n2m_table(char *filename);
static int get_stat_info(char *, struct stat *);
static char *new_id(numeral_t *, int, char *);
static int find_enum_id(devfsadm_enumerate_t rules[], int nrules,
    char *devfs_path, int index, char *min, int type, char **buf, int multiple);
static void daemon_update(void);
static void usage(void);
static int getnexttoken(char *next, char **nextp, char **tokenpp, char *tchar);
static int class_ok(char *class);
static int create_link_common(char *devlink, char *contents, int *exists);
static char *dequote(char *src);
static void parse_args(int argc, char *argv[]);
static void process_devinfo_tree(void);
static void *minor_fini_thread(void *arg);
static void *s_realloc(void *ptr, const size_t size);
static void read_devlinktab_file(void);
static selector_list_t *create_selector_list(char *selector);
static int parse_selector(char **selector, char **key, char **val);
int devfsadm_noupdate(void);
const char *devfsadm_root_path(void);
static link_list_t *create_link_list(char *link);
static void s_unlink(const char *file);
static void s_closedir(DIR *dirp);
static void s_mkdirp(const char *path, const mode_t mode);
static int is_minor_node(char *contents, char **mn_root);
static int construct_devlink(char *link, link_list_t *link_build,
				char *contents, di_minor_t minor,
				di_node_t node, char *pattern);
static int split_devlinktab_entry(char *entry, char **selector, char **p_link,
	    char **s_link);
static int devlink_matches(devlinktab_list_t *entry, di_minor_t minor,
			    di_node_t node);
static int build_links(devlinktab_list_t *entry, di_minor_t minor,
			di_node_t node);
static numeral_set_t *get_enum_cache(devfsadm_enumerate_t rules[],
				    int nrules);
static void enumerate_recurse(char *current_dir, char *path_left,
    numeral_set_t *setp, devfsadm_enumerate_t rules[], int index);

static int match_path_component(char *file_re, char *file, char **id,
				int subexp);
static void create_cached_numeral(char *path, numeral_set_t *setp,
    char *numeral_id, devfsadm_enumerate_t rules[], int index);
static int devfsadm_copy_file(const char *file, const struct stat *stat,
			    int flags, struct FTW *ftw);
static void getattr(char *devname, char *aminor, int spectype, dev_t dev,
    mode_t *mode, uid_t *uid, gid_t *gid);
static int minor_matches_rule(di_node_t node, di_minor_t minor,
				create_list_t *create);
static void add_verbose_id(char *mid);
static char *get_component(char *str, const char *comp_num);
static char *alloc_cmp_str(const char *devfs_path, devfsadm_enumerate_t *dep);
static int lookup_enum_cache(numeral_set_t *set, char *cmp_str,
    devfsadm_enumerate_t rules[], int index, numeral_t **matchnpp);
static void sync_handler(void *cookie, char *ap, size_t asize,
    door_desc_t *dp, uint_t ndesc);
static int zone_pathcheck(char *checkpath);
static void process_deferred_links(struct dca_impl *dcip, int flag);
static void event_handler(sysevent_t *ev);
static void dca_impl_init(char *root, char *minor, struct dca_impl *dcip);
static void lock_dev(void);
static void unlock_dev(int flag);
static int devlink_cb(di_devlink_t dl, void *arg);
static void free_dev_names(struct devlink_cb_arg *x);

int load_devpolicy(void);
static void load_dev_acl(void);
static void load_minor_perm_file(void);

static nvlist_t *build_event_attributes(char *, char *, char *,
    di_node_t, char *, int, char *);
static void log_event(char *, char *, nvlist_t *);
static void build_and_enq_event(char *, char *, char *, di_node_t, char *);

static void read_logindevperm_file(void);
static void set_logindev_perms(char *devlink);

static void reset_node_permissions(di_node_t, di_minor_t);

/*
 * devname related
 */
static void devname_lookup_handler(void *, char *, size_t,
    door_desc_t *, uint_t);		/* /dev name lookup server */
static int devname_kcall(int, void *);	/* syscall into the devname fs */

static void nfphash_create(void);
static int nfphash_fcn(char *key);
static item_t *nfphash_lookup(char *key);
static void nfphash_insert(char *key);
static void nfphash_destroy(void);

/* Enumerate reserve related */
static void read_enumerate_file(void);
static int enumerate_parse(char *rsvstr, char *path_left, numeral_set_t *setp,
    devfsadm_enumerate_t rules[], int index);
static void create_reserved_numeral(numeral_set_t *setp, char *numeral_id);

/* convenient short hands */
#define	vprint		devfsadm_print
#define	err_print	devfsadm_errprint
#ifndef TRUE
#define	TRUE	1
#endif
#ifndef FALSE
#define	FALSE	0
#endif

#ifdef	__cplusplus
}
#endif

#endif /* _DEVFSADM_IMPL_H */