2 * Copyright (c) 1995 Andrew McRae. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/usr.sbin/pccard/pccardd/pccardd.c,v 1.6.2.6 2001/08/02 18:51:38 imp Exp $
27 * $DragonFly: src/usr.sbin/pccard/pccardd/Attic/pccardd.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
36 #include <sys/socket.h>
42 char *config_file = "/etc/defaults/pccard.conf";
43 static char *pid_file = "/var/run/pccardd.pid";
46 * pathname of UNIX-domain socket
48 static char *socket_name = "/var/tmp/.pccardd";
49 static char *sock = 0;
50 static int server_sock;
52 /* SIGHUP signal handler */
56 bitstr_t bit_decl(io_inuse, IOPORTS);
57 bitstr_t bit_decl(mem_inuse, MEMBLKS);
60 struct sockaddr_un sun;
62 bit_nclear(io_inuse, 0, IOPORTS-1);
63 bit_nclear(mem_inuse, 0, MEMBLKS-1);
64 bzero(irq_inuse, sizeof(irq_inuse));
66 /* compare the initial and current state of resource pool */
67 for (i = 0; i < IOPORTS; i++) {
68 if (bit_test(io_init, i) == 1 && bit_test(io_avail, i) == 0) {
69 if (debug_level >= 1) {
70 logmsg("io 0x%x seems to be in use\n", i);
75 for (i = 0; i < MEMBLKS; i++) {
76 if (bit_test(mem_init, i) == 1 && bit_test(mem_avail, i) == 0) {
77 if (debug_level >= 1) {
78 logmsg("mem 0x%x seems to be in use\n", i);
80 bit_set(mem_inuse, i);
83 for (i = 0; i < 16; i++) {
84 if (irq_init[i] == 1 && pool_irq[i] == 0) {
85 if (debug_level >= 1) {
86 logmsg("irq %d seems to be in use\n", i);
92 readfile(config_file);
94 /* reflect used resources to managed resource pool */
95 for (i = 0; i < IOPORTS; i++) {
96 if (bit_test(io_inuse, i) == 1) {
97 bit_clear(io_avail, i);
100 for (i = 0; i < MEMBLKS; i++) {
101 if (bit_test(mem_inuse, i) == 1) {
102 bit_clear(mem_avail, i);
105 for (i = 0; i < 16; i++) {
106 if (irq_inuse[i] == 1) {
111 if ((server_sock = socket(PF_LOCAL, SOCK_DGRAM, 0)) < 0)
112 die("socket failed");
113 bzero(&sun, sizeof(sun));
114 sun.sun_family = AF_UNIX;
118 strcpy(sun.sun_path, socket_name);
119 slen = SUN_LEN(&sun);
120 (void)unlink(socket_name);
121 if (bind(server_sock, (struct sockaddr *) & sun, slen) < 0)
123 chown(socket_name, 0, 5); /* XXX - root.operator */
124 chmod(socket_name, 0660);
125 set_socket(server_sock);
128 /* SIGTERM/SIGINT signal handler */
132 logmsg("pccardd terminated: signal %d received", sig);
133 (void)unlink(pid_file);
140 FILE *fp = fopen(pid_file, "w");
143 fprintf(fp, "%d\n", getpid());
150 * mainline code for cardd
153 main(int argc, char *argv[])
156 int count, dodebug = 0;
159 int irq_specified = 0;
161 struct sockaddr_un sun;
162 #define COM_OPTS ":Idvf:s:i:z"
164 bzero(irq_arg, sizeof(irq_arg));
167 pccard_init_sleep = 5000000;
168 cards = last_card = 0;
169 while ((count = getopt(argc, argv, COM_OPTS)) != -1) {
183 config_file = optarg;
186 /* configure available irq */
187 if (sscanf(optarg, "%d", &i) != 1) {
188 fprintf(stderr, "%s: -i number\n", argv[0]);
201 die("no config file argument");
204 die("illegal option");
211 io_avail = bit_alloc(IOPORTS); /* Only supports ISA ports */
212 io_init = bit_alloc(IOPORTS);
214 /* Mem allocation done in MEMUNIT units. */
215 mem_avail = bit_alloc(MEMBLKS);
216 mem_init = bit_alloc(MEMBLKS);
217 readfile(config_file);
219 bcopy(irq_arg, pool_irq, sizeof(irq_arg));
220 bcopy(irq_arg, irq_init, sizeof(irq_arg));
225 if (!dodebug && !delay)
230 die("no PC-CARD slots");
234 logmsg("pccardd started", NULL);
237 if ((server_sock = socket(PF_LOCAL, SOCK_DGRAM, 0)) < 0)
238 die("socket failed");
239 bzero(&sun, sizeof(sun));
240 sun.sun_family = AF_UNIX;
244 strcpy(sun.sun_path, socket_name);
245 slen = SUN_LEN(&sun);
246 (void)unlink(socket_name);
247 if (bind(server_sock, (struct sockaddr *) & sun, slen) < 0)
249 chown(socket_name, 0, 5); /* XXX - root.operator */
250 chmod(socket_name, 0660);
251 set_socket(server_sock);
253 (void)signal(SIGINT, dodebug ? term : SIG_IGN);
254 (void)signal(SIGTERM, term);
255 (void)signal(SIGHUP, (void (*)(int))restart);
261 for (sp = slots; sp; sp = sp->next)
262 FD_SET(sp->fd, &emask);
263 FD_SET(server_sock, &rmask);
264 count = select(32, &rmask, 0, &emask, 0);
270 for (sp = slots; sp; sp = sp->next)
271 if (FD_ISSET(sp->fd, &emask))
273 if (FD_ISSET(server_sock, &rmask))