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 #include <cpu/inttypes.h> 43 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> 57 58 #include <libprop/proplib.h> 59 #include <sys/udev.h> 60 #include "udevd.h" 61 62 static int64_t udev_generation; 63 TAILQ_HEAD(pdev_array_list_head, pdev_array_entry) pdev_array_list; 64 65 void 66 pdev_array_entry_ref(struct pdev_array_entry *pae) 67 { 68 ++pae->refs; 69 } 70 71 void 72 pdev_array_entry_unref(struct pdev_array_entry *pae) 73 { 74 if (--pae->refs == 0) { 75 TAILQ_REMOVE(&pdev_array_list, pae, link); 76 prop_object_release(pae->pdev_array); /* XXX */ 77 free(pae); 78 } 79 } 80 81 void 82 pdev_array_entry_insert(prop_array_t pa) 83 { 84 struct pdev_array_entry *pae, *opae = NULL; 85 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 */ 95 96 /* 97 * If the TAILQ is not empty, unref the last entry, 98 * as it isn't needed anymore. 99 */ 100 if (!TAILQ_EMPTY(&pdev_array_list)) 101 opae = TAILQ_LAST(&pdev_array_list, pdev_array_list_head); 102 103 TAILQ_INSERT_TAIL(&pdev_array_list, pae, link); 104 105 if (opae != NULL) 106 pdev_array_entry_unref(opae); 107 } 108 109 void 110 pdev_array_clean(void) 111 { 112 struct pdev_array_entry *pae; 113 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); 118 } 119 } 120 121 struct pdev_array_entry * 122 pdev_array_entry_get(int64_t generation) 123 { 124 struct pdev_array_entry *pae; 125 int found = 0; 126 127 TAILQ_FOREACH(pae, &pdev_array_list, link) { 128 if (pae->generation == generation) { 129 found = 1; 130 break; 131 } 132 } 133 134 if (!found) 135 return NULL; 136 137 pdev_array_entry_ref(pae); 138 return pae; 139 } 140 141 struct pdev_array_entry * 142 pdev_array_entry_get_last(void) 143 { 144 struct pdev_array_entry *pae; 145 146 pae = TAILQ_LAST(&pdev_array_list, pdev_array_list_head); 147 148 pdev_array_entry_ref(pae); 149 return pae; 150 } 151