1 #include <sys/socket.h>
14 "/var/run/nscd/socket"
17 FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *swap)
22 int32_t req_buf[REQ_LEN] = {
28 .msg_iov = (struct iovec[]){
29 {&req_buf, sizeof(req_buf)},
30 {(char*)key, strlen(key)+1}
35 if (strlen(key) > INT32_MAX - 1) {
42 fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
43 if (fd < 0) return NULL;
45 if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
46 /* If there isn't a running nscd we return -1 to indicate that
47 * that is precisely what happened
49 if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) {
56 if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0)
59 if(!(f = fdopen(fd, "r"))) goto error;
61 if (!fread(buf, len, 1, f)) {
62 /* If the VERSION entry mismatches nscd will disconnect. The
63 * most likely cause is that the endianness mismatched. So, we
64 * byteswap and try once more. (if we already swapped, just
67 if (ferror(f)) goto error;
70 for (i = 0; i < sizeof(req_buf)/sizeof(req_buf[0]); i++) {
71 req_buf[i] = bswap_32(req_buf[i]);
82 for (i = 0; i < len/sizeof(buf[0]); i++) {
83 buf[i] = bswap_32(buf[i]);
87 /* The first entry in every nscd response is the version number. This
88 * really shouldn't happen, and is evidence of some form of malformed
91 if(buf[0] != NSCDVERSION) {
98 if (f) fclose(f); else close(fd);