ifq: Factor out if_classq from altq_classq and use it for default ifq.
[dragonfly.git] / sbin / udevd / udevd_pdev.c
blobdcb50e69bac7bef029cf88f160bdf669217114bc
1 /*
2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 #include <sys/types.h>
35 #include <sys/device.h>
36 #include <sys/wait.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
39 #include <sys/poll.h>
40 #include <sys/queue.h>
41 #include <sys/un.h>
42 #include <cpu/inttypes.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <libgen.h>
48 #include <regex.h>
49 #include <signal.h>
50 #include <stdarg.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <syslog.h>
55 #include <unistd.h>
56 #include <pthread.h>
58 #include <libprop/proplib.h>
59 #include <sys/udev.h>
60 #include "udevd.h"
62 static int64_t udev_generation;
63 TAILQ_HEAD(pdev_array_list_head, pdev_array_entry) pdev_array_list;
65 void
66 pdev_array_entry_ref(struct pdev_array_entry *pae)
68 ++pae->refs;
71 void
72 pdev_array_entry_unref(struct pdev_array_entry *pae)
74 if (--pae->refs == 0) {
75 TAILQ_REMOVE(&pdev_array_list, pae, link);
76 prop_object_release(pae->pdev_array); /* XXX */
77 free(pae);
81 void
82 pdev_array_entry_insert(prop_array_t pa)
84 struct pdev_array_entry *pae, *opae = NULL;
86 if (pa == NULL)
87 errx(1, "null prop_array in insert_pdev_array");
88 pae = malloc(sizeof(struct pdev_array_entry));
89 if (pae == NULL)
90 errx(1, "insert_pdev_array could not allocate mem");
91 memset(pae, 0, sizeof(struct pdev_array_entry));
92 pae->pdev_array = pa;
93 pae->generation = udev_generation++;
94 pae->refs = 1; /* One ref because it's the most recent one */
97 * If the TAILQ is not empty, unref the last entry,
98 * as it isn't needed anymore.
100 if (!TAILQ_EMPTY(&pdev_array_list))
101 opae = TAILQ_LAST(&pdev_array_list, pdev_array_list_head);
103 TAILQ_INSERT_TAIL(&pdev_array_list, pae, link);
105 if (opae != NULL)
106 pdev_array_entry_unref(opae);
109 void
110 pdev_array_clean(void)
112 struct pdev_array_entry *pae;
114 while ((pae = TAILQ_LAST(&pdev_array_list, pdev_array_list_head)) != NULL) {
115 TAILQ_REMOVE(&pdev_array_list, pae, link);
116 prop_object_release(pae->pdev_array);
117 free(pae);
121 struct pdev_array_entry *
122 pdev_array_entry_get(int64_t generation)
124 struct pdev_array_entry *pae;
125 int found = 0;
127 TAILQ_FOREACH(pae, &pdev_array_list, link) {
128 if (pae->generation == generation) {
129 found = 1;
130 break;
134 if (!found)
135 return NULL;
137 pdev_array_entry_ref(pae);
138 return pae;
141 struct pdev_array_entry *
142 pdev_array_entry_get_last(void)
144 struct pdev_array_entry *pae;
146 pae = TAILQ_LAST(&pdev_array_list, pdev_array_list_head);
148 pdev_array_entry_ref(pae);
149 return pae;