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