1 /*
2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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.
20 *
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.
33 */
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
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <libgen.h>
47 #include <regex.h>
48 #include <signal.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <syslog.h>
54 #include <unistd.h>
55 #include <pthread.h>
56
57 #include <libprop/proplib.h>
58 #include <sys/udev.h>
59 #include "udevd.h"
60
61 static int64_t udev_generation;
62 TAILQ_HEAD(pdev_array_list_head, pdev_array_entry) pdev_array_list;
63
64 void
pdev_array_entry_ref(struct pdev_array_entry * pae)65 pdev_array_entry_ref(struct pdev_array_entry *pae)
66 {
67 ++pae->refs;
68 }
69
70 void
pdev_array_entry_unref(struct pdev_array_entry * pae)71 pdev_array_entry_unref(struct pdev_array_entry *pae)
72 {
73 if (--pae->refs == 0) {
74 TAILQ_REMOVE(&pdev_array_list, pae, link);
75 prop_object_release(pae->pdev_array); /* XXX */
76 free(pae);
77 }
78 }
79
80 void
pdev_array_entry_insert(prop_array_t pa)81 pdev_array_entry_insert(prop_array_t pa)
82 {
83 struct pdev_array_entry *pae, *opae = NULL;
84
85 if (pa == NULL)
86 errx(1, "null prop_array in insert_pdev_array");
87 pae = malloc(sizeof(struct pdev_array_entry));
88 if (pae == NULL)
89 errx(1, "insert_pdev_array could not allocate mem");
90 memset(pae, 0, sizeof(struct pdev_array_entry));
91 pae->pdev_array = pa;
92 pae->generation = udev_generation++;
93 pae->refs = 1; /* One ref because it's the most recent one */
94
95 /*
96 * If the TAILQ is not empty, unref the last entry,
97 * as it isn't needed anymore.
98 */
99 if (!TAILQ_EMPTY(&pdev_array_list))
100 opae = TAILQ_LAST(&pdev_array_list, pdev_array_list_head);
101
102 TAILQ_INSERT_TAIL(&pdev_array_list, pae, link);
103
104 if (opae != NULL)
105 pdev_array_entry_unref(opae);
106 }
107
108 void
pdev_array_clean(void)109 pdev_array_clean(void)
110 {
111 struct pdev_array_entry *pae;
112
113 while ((pae = TAILQ_LAST(&pdev_array_list, pdev_array_list_head)) != NULL) {
114 TAILQ_REMOVE(&pdev_array_list, pae, link);
115 prop_object_release(pae->pdev_array);
116 free(pae);
117 }
118 }
119
120 struct pdev_array_entry *
pdev_array_entry_get(int64_t generation)121 pdev_array_entry_get(int64_t generation)
122 {
123 struct pdev_array_entry *pae;
124 int found = 0;
125
126 TAILQ_FOREACH(pae, &pdev_array_list, link) {
127 if (pae->generation == generation) {
128 found = 1;
129 break;
130 }
131 }
132
133 if (!found)
134 return NULL;
135
136 pdev_array_entry_ref(pae);
137 return pae;
138 }
139
140 struct pdev_array_entry *
pdev_array_entry_get_last(void)141 pdev_array_entry_get_last(void)
142 {
143 struct pdev_array_entry *pae;
144
145 pae = TAILQ_LAST(&pdev_array_list, pdev_array_list_head);
146
147 pdev_array_entry_ref(pae);
148 return pae;
149 }
150