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}
40 fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
41 if (fd < 0) return NULL;
43 if(!(f = fdopen(fd, "r"))) {
48 if (strlen(key) > INT32_MAX - 1)
51 if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
52 /* If there isn't a running nscd we return -1 to indicate that
53 * that is precisely what happened
55 if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT)
60 if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0)
63 if (!fread(buf, len, 1, f)) {
64 /* If the VERSION entry mismatches nscd will disconnect. The
65 * most likely cause is that the endianness mismatched. So, we
66 * byteswap and try once more. (if we already swapped, just
69 if (ferror(f)) goto error;
72 for (i = 0; i < sizeof(req_buf)/sizeof(req_buf[0]); i++) {
73 req_buf[i] = bswap_32(req_buf[i]);
84 for (i = 0; i < len/sizeof(buf[0]); i++) {
85 buf[i] = bswap_32(buf[i]);
89 /* The first entry in every nscd response is the version number. This
90 * really shouldn't happen, and is evidence of some form of malformed
93 if(buf[0] != NSCDVERSION) {