1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
22  * Use is subject to license terms.
23  */
24 
25 #ifndef _DEVFSADM_IMPL_H
26 #define	_DEVFSADM_IMPL_H
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 #include <dlfcn.h>
35 #include <stdarg.h>
36 #include <fcntl.h>
37 #include <sys/file.h>
38 #include <locale.h>
39 #include <libintl.h>
40 #include <ctype.h>
41 #include <signal.h>
42 #include <deflt.h>
43 #include <ftw.h>
44 #include <sys/instance.h>
45 #include <sys/types.h>
46 #include <dirent.h>
47 #include <pwd.h>
48 #include <grp.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <sys/mkdev.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #include <errno.h>
55 #include <unistd.h>
56 #include <sys/stat.h>
57 #include <sys/int_types.h>
58 #include <limits.h>
59 #include <strings.h>
60 #include <devfsadm.h>
61 #include <libdevinfo.h>
62 #include <sys/devinfo_impl.h>
63 #include <sys/modctl.h>
64 #include <libgen.h>
65 #include <sys/hwconf.h>
66 #include <sys/sunddi.h>
67 #include <door.h>
68 #include <syslog.h>
69 #include <libsysevent.h>
70 #include <thread.h>
71 #include <message.h>
72 #include <sys/cladm.h>
73 #include <librcm.h>
74 #include <sys/sysevent/dev.h>
75 #include <libzonecfg.h>
76 #include <device_info.h>
77 #include <sys/fs/sdev_node.h>
78 #include <sys/syscall.h>
79 #include <rpcsvc/ypclnt.h>
80 #include <sys/sysevent/eventdefs.h>
81 
82 #undef	DEBUG
83 #ifndef DEBUG
84 #define	NDEBUG 1
85 #else
86 #undef	NDEBUG
87 #endif
88 
89 #include <assert.h>
90 
91 
92 #define	DEV_LOCK_FILE ".devfsadm_dev.lock"
93 #define	DAEMON_LOCK_FILE ".devfsadm_daemon.lock"
94 
95 #define	DEV "/dev"
96 #define	ETC "/etc"
97 #define	ETCDEV "/etc/dev"
98 #define	DEV_LEN 4
99 #define	DEVICES "/devices"
100 #define	DEVICES_LEN 8
101 #define	MODULE_DIRS "/usr/lib/devfsadm/linkmod"
102 #define	ALIASFILE "/etc/driver_aliases"
103 #define	NAME_TO_MAJOR "/etc/name_to_major"
104 #define	RECONFIG_BOOT "_INIT_RECONFIG"
105 #define	PID_STR_LEN 10
106 #define	EXTRA_PRIVS	"/etc/security/extra_privs"
107 #define	DEV_POLICY	"/etc/security/device_policy"
108 #define	LDEV_FILE	"/etc/logindevperm"
109 
110 #define	DEVFSADM_DEFAULT_FILE "/etc/default/devfsadm"
111 
112 #define	MINOR_FINI_TIMEOUT_DEFAULT 2
113 
114 #define	SYNCH_DOOR_PERMS	(S_IRUSR | S_IWUSR)
115 
116 #define	DRVCONFIG "drvconfig"
117 #define	DEVFSADM "devfsadm"
118 #define	DEVFSADMD "devfsadmd"
119 #define	DEVLINKS "devlinks"
120 #define	TAPES "tapes"
121 #define	AUDLINKS "audlinks"
122 #define	PORTS "ports"
123 #define	DISKS "disks"
124 
125 #define	MAX_IDLE_DELAY 5
126 #define	MAX_DELAY 30
127 #define	NAME 0x01
128 #define	ADDR 0x03
129 #define	MINOR 0x04
130 #define	COUNTER 0x05
131 #define	CONSTANT 0x06
132 #define	TYPE 0x07
133 #define	TYPE_S "type"
134 #define	ADDR_S "addr"
135 #define	ADDR_S_LEN 4
136 #define	MINOR_S "minor"
137 #define	MINOR_S_LEN 5
138 #define	NAME_S "name"
139 #define	TAB '\t'
140 #define	NEWLINE '\n'
141 #define	MAX_DEVLINK_LINE 4028
142 #define	INTEGER 0
143 #define	LETTER 1
144 #define	MAX_PERM_LINE 256
145 #define	MAX_LDEV_LINE 256
146 #define	LDEV_DELIMS " \t\n"
147 #define	LDEV_DRVLIST_DELIMS "="
148 #define	LDEV_DRV_DELIMS ", \t\n"
149 #define	LDEV_DEV_DELIM ":"
150 #define	LDEV_DRVLIST_NAME "driver"
151 #define	NFP_HASH_SZ 256
152 
153 #define	TYPE_LINK 0x00
154 #define	TYPE_DEVICES 0x01
155 
156 #define	CREATE_LINK 0x01
157 #define	READ_LINK 0x02
158 #define	CREATE_NODE 0x01
159 #define	READ_NODE 0x02
160 
161 #define	CACHE_STATE 0x0
162 #define	SYNC_STATE 0x1
163 
164 #define	MODULE_ACTIVE 0x01
165 
166 #define	MAX_SLEEP 120
167 
168 #define	DEVLINKTAB_FILE "/etc/devlink.tab"
169 
170 #define	MODULE_SUFFIX ".so"
171 #define	MINOR_INIT "minor_init"
172 #define	MINOR_FINI "minor_fini"
173 #define	_DEVFSADM_CREATE_REG "_devfsadm_create_reg"
174 #define	_DEVFSADM_REMOVE_REG "_devfsadm_remove_reg"
175 
176 #define	NUM_EV_STR		4
177 #define	EV_TYPE			0
178 #define	EV_CLASS		1
179 #define	EV_PATH_NAME		2
180 #define	EV_MINOR_NAME		3
181 
182 /* add new debug level and meanings here */
183 #define	DEVLINK_MID		"devfsadm:devlink"
184 #define	MODLOAD_MID		"devfsadm:modload"
185 #define	INITFINI_MID		"devfsadm:initfini"
186 #define	EVENT_MID		"devfsadm:event"
187 #define	REMOVE_MID		"devfsadm:remove"
188 #define	LOCK_MID		"devfsadm:lock"
189 #define	PATH2INST_MID		"devfsadm:path2inst"
190 #define	CACHE_MID		"devfsadm:cache"
191 #define	BUILDCACHE_MID		"devfsadm:buildcache"
192 #define	RECURSEDEV_MID		"devfsadm:recursedev"
193 #define	INSTSYNC_MID		"devfsadm:instsync"
194 #define	FILES_MID		"devfsadm:files"
195 #define	ENUM_MID		"devfsadm:enum"
196 #define	LINKCACHE_MID		"devfsadm:linkcache"
197 #define	ADDREMCACHE_MID		"devfsadm:addremcache"
198 #define	MALLOC_MID		"devfsadm:malloc"
199 #define	READDIR_MID		"devfsadm:readdir"
200 #define	READDIR_ALL_MID		"devfsadm:readdir_all"
201 #define	DEVNAME_MID		"devfsadm:devname"
202 #define	ALL_MID			"all"
203 
204 #define	DEVFSADM_DEBUG_ON	(verbose == NULL) ? FALSE : TRUE
205 
206 typedef struct recurse_dev {
207 	void (*fcn)(char *, void *);
208 	void *data;
209 } recurse_dev_t;
210 
211 typedef struct link {
212 	char *devlink; /* without ".../dev/"   prefix */
213 	char *contents; /* without "../devices" prefix */
214 	struct link *next;
215 } link_t;
216 
217 typedef struct linkhead {
218 	regex_t dir_re_compiled;
219 	char *dir_re;
220 	link_t *link;
221 	link_t *nextlink;
222 	struct linkhead *nexthead;
223 } linkhead_t;
224 
225 typedef struct link_list  {
226 	int type;
227 	char *constant;
228 	int arg;
229 	struct link_list *next;
230 } link_list_t;
231 
232 typedef struct selector_list {
233 	int key;
234 	char *val;
235 	int arg;
236 	struct selector_list *next;
237 } selector_list_t;
238 
239 typedef struct devlinktab_list {
240 	int line_number;
241 	char *selector_pattern;
242 	char *p_link_pattern;
243 	char *s_link_pattern;
244 	selector_list_t *selector;
245 	link_list_t *p_link;
246 	link_list_t *s_link;
247 	struct devlinktab_list *next;
248 } devlinktab_list_t;
249 
250 typedef struct module {
251 	char *name;
252 	void *dlhandle;
253 	int (*minor_init)();
254 	int (*minor_fini)();
255 	int flags;
256 	struct module *next;
257 } module_t;
258 
259 typedef struct create_list {
260 	devfsadm_create_t *create;
261 	module_t *modptr;
262 	regex_t node_type_comp;
263 	regex_t drv_name_comp;
264 	struct create_list *next;
265 } create_list_t;
266 
267 struct minor {
268 	di_node_t node;
269 	di_minor_t minor;
270 	struct minor *next;
271 };
272 
273 struct mlist {
274 	struct minor *head;
275 	struct minor *tail;
276 };
277 
278 typedef struct remove_list {
279 	devfsadm_remove_V1_t *remove;
280 	module_t *modptr;
281 	struct remove_list *next;
282 } remove_list_t;
283 
284 typedef struct item {
285 	char *i_key;
286 	struct item *i_next;
287 } item_t;
288 
289 typedef struct cleanup_data {
290 	int flags;
291 	char *phypath;
292 	remove_list_t *rm;
293 } cleanup_data_t;
294 
295 typedef struct n2m {
296 	major_t major;
297 	char *driver;
298 	struct n2m *next;
299 } n2m_t;
300 
301 /* structures for devfsadm_enumerate() */
302 typedef struct numeral {
303 	char *id;
304 	char *full_path;
305 	int rule_index;
306 	char *cmp_str;
307 	struct numeral *next;
308 } numeral_t;
309 
310 typedef struct numeral_set {
311 	int re_count;
312 	char **re;
313 	numeral_t *headnumeral;
314 	struct numeral_set *next;
315 } numeral_set_t;
316 
317 typedef struct temp {
318 	int integer;
319 	struct temp *next;
320 } temp_t;
321 
322 typedef struct driver_alias {
323 	char *driver_name;
324 	char *alias_name;
325 	struct driver_alias *next;
326 } driver_alias_t;
327 
328 struct driver_list {
329 	char driver_name[MAXNAMELEN];
330 	struct driver_list *next;
331 };
332 
333 struct login_dev {
334 	char *ldev_console;
335 	int ldev_perms;
336 	char *ldev_device;
337 	regex_t ldev_device_regex;
338 	struct driver_list *ldev_driver_list;
339 	struct login_dev *ldev_next;
340 };
341 
342 #define	MAX_DEV_NAME_COUNT	100
343 struct devlink_cb_arg {
344 	char *dev_names[MAX_DEV_NAME_COUNT];
345 	char *link_contents[MAX_DEV_NAME_COUNT];
346 	int count;
347 	int rv;
348 };
349 
350 struct dca_impl {
351 	char *dci_root;
352 	char *dci_minor;
353 	char *dci_driver;
354 	void *dci_arg;
355 	int dci_error;
356 	int dci_flags;
357 };
358 
359 /* RCM related */
360 struct rcm_eventq {
361 	nvlist_t *nvl;
362 	struct rcm_eventq *next;
363 };
364 
365 /* sysevent queue related */
366 typedef struct syseventq_s {
367 	struct syseventq_s *next;
368 	char *class;
369 	char *subclass;
370 	nvlist_t *nvl;
371 } syseventq_t;
372 
373 static int devfsadm_enumerate_int_start(char *devfs_path,
374 	int index, char **buf, devfsadm_enumerate_t rules[],
375 	int nrules, char *start);
376 static void set_root_devices_dev_dir(char *dir);
377 static void pre_and_post_cleanup(int flags);
378 static void hot_cleanup(char *, char *, char *, char *, int);
379 static void devfsadm_exit(int status);
380 static void rm_link_from_cache(char *devlink);
381 static void rm_all_links_from_cache();
382 static void add_link_to_cache(char *devlink, char *physpath);
383 static linkhead_t *get_cached_links(char *dir_re);
384 static void build_devlink_list(char *check_link, void *data);
385 static void instance_flush_thread(void);
386 static int s_rmdir(char *path);
387 static void rm_parent_dir_if_empty(char *path);
388 static void free_link_list(link_list_t *head);
389 static void free_selector_list(selector_list_t *head);
390 void devfsadm_err_print(char *message, ...);
391 void defvsadm_print(int level, char *message, ...);
392 static int call_minor_init(module_t *module);
393 static void load_module(char *module, char *cdir);
394 static void invalidate_enumerate_cache(void);
395 static pid_t enter_dev_lock(void);
396 static void exit_dev_lock(void);
397 static pid_t enter_daemon_lock(void);
398 static void exit_daemon_lock(void);
399 static int process_devlink_compat(di_minor_t minor, di_node_t node);
400 static int alias(char *, char *);
401 static int devfsadm_copy(void);
402 static void flush_path_to_inst(void);
403 static void detachfromtty(void);
404 static void minor_process(di_node_t node, di_minor_t minor,
405     struct mlist *dep);
406 static void read_minor_perm_file(void);
407 static void read_driver_aliases_file(void);
408 static void load_modules(void);
409 static void unload_modules(void);
410 static void *s_malloc(const size_t size);
411 static void *s_zalloc(const size_t size);
412 static void devfs_instance_mod(void);
413 static void add_minor_pathname(char *, char *, char *);
414 static int check_minor_type(di_node_t node, di_minor_t minor, void *arg);
415 static void cache_deferred_minor(struct mlist *dep, di_node_t node,
416     di_minor_t minor);
417 static int compare_field(char *full_name, char *field_item, int field);
418 static int component_cat(char *link, char *name, int field);
419 static void recurse_dev_re(char *current_dir, char *path_re, recurse_dev_t *rd);
420 static void matching_dev(char *devpath, void *data);
421 static int resolve_link(char *devpath, char **content_p, int *type_p,
422     char **devfs_path, int dangle);
423 static int clean_ok(devfsadm_remove_V1_t *remove);
424 static int translate_major(dev_t old_dev, dev_t *new_dev);
425 static int get_major_no(char *driver, major_t *major);
426 static int load_n2m_table(char *filename);
427 static int get_stat_info(char *, struct stat *);
428 static char *new_id(numeral_t *, int, char *);
429 static int find_enum_id(devfsadm_enumerate_t rules[], int nrules,
430     char *devfs_path, int index, char *min, int type, char **buf, int multiple);
431 static void daemon_update(void);
432 static void usage(void);
433 static int getnexttoken(char *next, char **nextp, char **tokenpp, char *tchar);
434 static int class_ok(char *class);
435 static int create_link_common(char *devlink, char *contents, int *exists);
436 static char *dequote(char *src);
437 static void parse_args(int argc, char *argv[]);
438 static void process_devinfo_tree(void);
439 static void minor_fini_thread(void *arg);
440 static void *s_realloc(void *ptr, const size_t size);
441 static void read_devlinktab_file(void);
442 static selector_list_t *create_selector_list(char *selector);
443 static int parse_selector(char **selector, char **key, char **val);
444 int devfsadm_noupdate(void);
445 const char *devfsadm_root_path(void);
446 static link_list_t *create_link_list(char *link);
447 static void s_unlink(const char *file);
448 static void s_closedir(DIR *dirp);
449 static void s_mkdirp(const char *path, const mode_t mode);
450 static int is_minor_node(char *contents, char **mn_root);
451 static int construct_devlink(char *link, link_list_t *link_build,
452 				char *contents, di_minor_t minor,
453 				di_node_t node, char *pattern);
454 static int split_devlinktab_entry(char *entry, char **selector, char **p_link,
455 	    char **s_link);
456 static int devlink_matches(devlinktab_list_t *entry, di_minor_t minor,
457 			    di_node_t node);
458 static int build_links(devlinktab_list_t *entry, di_minor_t minor,
459 			di_node_t node);
460 static numeral_set_t *get_enum_cache(devfsadm_enumerate_t rules[],
461 				    int nrules);
462 static void enumerate_recurse(char *current_dir, char *path_left,
463     numeral_set_t *setp, devfsadm_enumerate_t rules[], int index);
464 
465 static int match_path_component(char *file_re, char *file, char **id,
466 				int subexp);
467 static void create_cached_numeral(char *path, numeral_set_t *setp,
468     char *numeral_id, devfsadm_enumerate_t rules[], int index);
469 static int devfsadm_copy_file(const char *file, const struct stat *stat,
470 			    int flags, struct FTW *ftw);
471 static void getattr(char *devname, char *aminor, int spectype, dev_t dev,
472     mode_t *mode, uid_t *uid, gid_t *gid);
473 static int minor_matches_rule(di_node_t node, di_minor_t minor,
474 				create_list_t *create);
475 static void add_verbose_id(char *mid);
476 static char *get_component(char *str, const char *comp_num);
477 static char *alloc_cmp_str(const char *devfs_path, devfsadm_enumerate_t *dep);
478 static int lookup_enum_cache(numeral_set_t *set, char *cmp_str,
479     devfsadm_enumerate_t rules[], int index, numeral_t **matchnpp);
480 static void sync_handler(void *cookie, char *ap, size_t asize,
481     door_desc_t *dp, uint_t ndesc);
482 static int zone_pathcheck(char *checkpath);
483 static void process_deferred_links(struct dca_impl *dcip, int flag);
484 static void event_handler(sysevent_t *ev);
485 static int rcm_init(void);
486 static int notify_rcm(di_node_t node, char *minor_name);
487 static void dca_impl_init(char *root, char *minor, struct dca_impl *dcip);
488 static void lock_dev(void);
489 static void unlock_dev(int flag);
490 static int devlink_cb(di_devlink_t dl, void *arg);
491 static void free_dev_names(struct devlink_cb_arg *x);
492 
493 int load_devpolicy(void);
494 static void load_dev_acl(void);
495 static void load_minor_perm_file(void);
496 
497 static int (*librcm_alloc_handle)(char *, uint_t, void *, rcm_handle_t **);
498 static void (*librcm_free_handle)(rcm_handle_t *);
499 static int (*librcm_notify_event)(rcm_handle_t *, char *, uint_t, nvlist_t *,
500     rcm_info_t *);
501 static nvlist_t *build_event_attributes(char *, char *, char *,
502     di_node_t, char *, int);
503 static void log_event(char *, char *, nvlist_t *);
504 static void build_and_enq_event(char *, char *, char *, di_node_t);
505 
506 static void read_logindevperm_file(void);
507 static void set_logindev_perms(char *devlink);
508 
509 static void reset_node_permissions(di_node_t, di_minor_t);
510 
511 /*
512  * devname related
513  */
514 static void devname_lookup_handler(void *, char *, size_t,
515     door_desc_t *, uint_t);		/* /dev name lookup server */
516 static int devname_kcall(int, void *);	/* syscall into the devname fs */
517 static void nfphash_create(void);
518 static int nfphash_fcn(char *key);
519 static item_t *nfphash_lookup(char *key);
520 static void nfphash_insert(char *key);
521 static void nfphash_destroy(void);
522 
523 /* convenient short hands */
524 #define	vprint		devfsadm_print
525 #define	err_print	devfsadm_errprint
526 #ifndef TRUE
527 #define	TRUE	1
528 #endif
529 #ifndef FALSE
530 #define	FALSE	0
531 #endif
532 
533 #ifdef	__cplusplus
534 }
535 #endif
536 
537 #endif /* _DEVFSADM_IMPL_H */
538