pccardq.c revision 81977
190075Sobrien/* $FreeBSD: head/usr.sbin/apmd/contrib/pccardq.c 81977 2001-08-20 13:24:39Z brian $ */ 290075Sobrien 390075Sobrien#include <err.h> 490075Sobrien#include <errno.h> 590075Sobrien#include <limits.h> 690075Sobrien#include <stdarg.h> 790075Sobrien#include <stddef.h> 890075Sobrien#include <stdio.h> 990075Sobrien#include <stdlib.h> 1090075Sobrien#include <string.h> 1190075Sobrien#include <time.h> 1290075Sobrien#include <unistd.h> 1390075Sobrien#include <sys/types.h> 1490075Sobrien#include <sys/socket.h> 1590075Sobrien#include <sys/un.h> 1690075Sobrien 1790075Sobrienconst char *const pccardd_file = "/var/tmp/.pccardd"; 1890075Sobrienconst char *prog = "pccardq"; 1990075Sobrienconst char *tmp_dir = "/tmp"; 2090075Sobrienunsigned slot_map = ~0; 2190075Sobrien 2290075Sobrienvoid 2390075Sobrienusage() 2490075Sobrien{ 2590075Sobrien fprintf(stderr, "usage: %s [-a] [-n] [-s slot]\n", prog); 2690075Sobrien} 2790075Sobrien 2890075Sobrienint 2990075Sobrienproc_arg(int ac, char **av) 3090075Sobrien{ 3190075Sobrien int rc = -1; 3290075Sobrien int ch; 3390075Sobrien 3490075Sobrien char *p = strrchr(av[0], '/'); 3590075Sobrien prog = p ? p + 1 : av[0]; 3690075Sobrien 3790075Sobrien tmp_dir = getenv("TMPDIR") ? getenv("TMPDIR") : tmp_dir; 3890075Sobrien 3990075Sobrien while ((ch = getopt(ac, av, "ans:")) != EOF) { 4090075Sobrien switch (ch) { 4196263Sobrien case 'a': 4290075Sobrien slot_map = ~0; 4396263Sobrien break; 4496263Sobrien case 'n': 4590075Sobrien slot_map = 0; 4690075Sobrien break; 4790075Sobrien case 's': 4890075Sobrien { 4990075Sobrien int n = atoi(optarg); 5090075Sobrien if (n < 0 || n >= CHAR_BIT * sizeof slot_map) { 5190075Sobrien warnc(0, "Invalid slot number."); 5290075Sobrien usage(); 5390075Sobrien goto out; 5490075Sobrien } 5590075Sobrien if (slot_map == ~0) 5690075Sobrien slot_map = 0; 5790075Sobrien slot_map |= 1 << n; 5890075Sobrien } 5990075Sobrien break; 6090075Sobrien default: 6190075Sobrien usage(); 6296263Sobrien goto out; 6396263Sobrien } 6496263Sobrien } 6590075Sobrien 6690075Sobrien rc = 0; 6790075Sobrien out: 6890075Sobrien return rc; 6990075Sobrien} 7090075Sobrien 7190075Sobrienint 7290075Sobrienconnect_to_pccardd(char **path) 7390075Sobrien{ 7490075Sobrien int so = -1; 7590075Sobrien int pccardd_len; 7690075Sobrien struct sockaddr_un pccardq; 7790075Sobrien struct sockaddr_un pccardd; 7896263Sobrien 7996263Sobrien if ((so = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) { 8096263Sobrien warn("socket"); 8190075Sobrien goto err; 8290075Sobrien } 8390075Sobrien 8490075Sobrien snprintf(pccardq.sun_path, sizeof pccardq.sun_path, 8590075Sobrien "%s/%s%ld%ld", tmp_dir, prog, (long) getpid(), (long) time(0)); 86 pccardq.sun_family = AF_UNIX; 87 pccardq.sun_len = offsetof(struct sockaddr_un, sun_path) + strlen(pccardq.sun_path); 88 if (bind(so, (struct sockaddr *) &pccardq, pccardq.sun_len) < 0) { 89 warn("bind: %s", pccardq.sun_path); 90 goto err; 91 } 92 if ((*path = strdup(pccardq.sun_path)) == NULL) { 93 warn("strdup"); 94 goto err; 95 } 96 97 pccardd_len = strlen(pccardd_file) + 1; 98 if (pccardd_len > sizeof pccardd.sun_path) { 99 warnc(0, "%s: too long", pccardd_file); 100 goto err; 101 } 102 pccardd.sun_len = offsetof(struct sockaddr_un, sun_path) + pccardd_len; 103 pccardd.sun_family = AF_UNIX; 104 strcpy(pccardd.sun_path, pccardd_file); 105 if (connect(so, (struct sockaddr *) &pccardd, pccardd.sun_len) < 0) { 106 warn("connect: %s", pccardd_file); 107 goto err; 108 } 109 return so; 110 err: 111 if (so >= 0) 112 close(so); 113 return -1; 114} 115 116int 117get_slot_number(int so) 118{ 119 char buf[8]; 120 int rv; 121 int nslot; 122 123 if ((rv = write(so, "S", 1)) < 1) { 124 warn("write"); 125 goto err; 126 } else if (rv != 1) { 127 warnc(0, "write: fail."); 128 goto err; 129 } 130 131 if ((rv = read(so, buf, sizeof buf)) < 0) { 132 warn("read"); 133 goto err; 134 } 135 buf[sizeof buf - 1] = 0; 136 if (sscanf(buf, "%d", &nslot) != 1) { 137 warnc(0, "Invalid response."); 138 goto err; 139 } 140 return nslot; 141 err: 142 return -1; 143} 144 145enum { 146 SLOT_EMPTY = 0, 147 SLOT_FILLED = 1, 148 SLOT_INACTIVE = 2, 149 SLOT_UNDEFINED = 9 150}; 151 152int 153get_slot_info(int so, int slot, char **manuf, char **version, char 154 **device, int *state) 155{ 156 int rc = -1; 157 int rv; 158 static char buf[1024]; 159 int slen; 160 char *s; 161 char *sl; 162 163 char *_manuf; 164 char *_version; 165 char *_device; 166 167 if ((slen = snprintf(buf, sizeof buf, "N%d", slot)) == -1) { 168 warnc(0, "write"); 169 goto err; 170 } 171 172 if ((rv = write(so, buf, slen)) < 0) { 173 warn("write"); 174 goto err; 175 } else if (rv != slen) { 176 warnc(0, "write"); 177 goto err; 178 } 179 180 if ((rv = read(so, buf, sizeof buf)) < 0) { 181 warn("read"); 182 goto err; 183 } 184 185 s = buf; 186 if ((sl = strsep(&s, "~")) == NULL) 187 goto parse_err; 188 if (atoi(sl) != slot) 189 goto parse_err; 190 if ((_manuf = strsep(&s, "~")) == NULL) 191 goto parse_err; 192 if ((_version = strsep(&s, "~")) == NULL) 193 goto parse_err; 194 if ((_device = strsep(&s, "~")) == NULL) 195 goto parse_err; 196 if (sscanf(s, "%1d", state) != 1) 197 goto parse_err; 198 if (s != NULL && strchr(s, '~') != NULL) 199 goto parse_err; 200 201 if ((*manuf = strdup(_manuf)) == NULL) { 202 warn("strdup"); 203 goto err; 204 } 205 if ((*version = strdup(_version)) == NULL) { 206 warn("strdup"); 207 goto err; 208 } 209 if ((*device = strdup(_device)) == NULL) { 210 warn("strdup"); 211 goto err; 212 } 213 if (*manuf == NULL || *version == NULL || *device == NULL) { 214 warn("strdup"); 215 goto err; 216 } 217 218 rc = 0; 219 err: 220 return rc; 221 parse_err: 222 warnc(0, "Invalid response: %*s", rv, buf); 223 return rc; 224} 225 226const char * 227strstate(int state) 228{ 229 switch (state) { 230 case 0: 231 return "empty"; 232 case 1: 233 return "filled"; 234 case 2: 235 return "inactive"; 236 default: 237 return "unknown"; 238 } 239} 240 241int 242main(int ac, char **av) 243{ 244 char *path = NULL; 245 int so = -1; 246 int nslot; 247 int i; 248 249 if (proc_arg(ac, av) < 0) 250 goto out; 251 if ((so = connect_to_pccardd(&path)) < 0) 252 goto out; 253 if ((nslot = get_slot_number(so)) < 0) 254 goto out; 255 if (slot_map == 0) { 256 printf("%d\n", nslot); 257 } else { 258 for (i = 0; i < nslot; i++) { 259 if ((slot_map & (1 << i))) { 260 char *manuf; 261 char *version; 262 char *device; 263 int state; 264 265 if (get_slot_info(so, i, &manuf, &version, &device, 266 &state) < 0) 267 goto out; 268 if (manuf == NULL || version == NULL || device == NULL) 269 goto out; 270 printf("%d~%s~%s~%s~%s\n", 271 i, manuf, version, device, strstate(state)); 272 free(manuf); 273 free(version); 274 free(device); 275 } 276 } 277 } 278 out: 279 if (path) { 280 unlink(path); 281 free(path); 282 } 283 if (so >= 0) 284 close(so); 285 exit(0); 286} 287