]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/usockfd.c
Enhanced machine type
[netatalk.git] / etc / cnid_dbd / usockfd.c
1 /*
2  * $Id: usockfd.c,v 1.6 2009-11-05 14:38:07 franklahm Exp $
3  *
4  * Copyright (C) Joerg Lenneis 2003
5  * All Rights Reserved.  See COPYING.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif /* HAVE_UNISTD_H */
19 #include <sys/un.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <netdb.h>
24
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h>
27 #endif /* HAVE_SYS_TYPES_H */
28 #ifdef HAVE_SYS_TIME_H
29 #include <sys/time.h>
30 #endif /* HAVE_SYS_TIME_H */
31
32
33 #include <atalk/logger.h>
34 #include "usockfd.h"
35
36 #include <sys/select.h>
37
38 int usockfd_create(char *usock_fn, mode_t mode, int backlog)
39 {
40     int sockfd;
41     struct sockaddr_un addr;
42
43
44     if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
45         LOG(log_error, logtype_cnid, "error in socket call: %s",
46             strerror(errno));
47         return -1;
48     }
49      
50     if (unlink(usock_fn) < 0 && errno != ENOENT) {
51         LOG(log_error, logtype_cnid, "error unlinking unix socket file %s: %s",
52             usock_fn, strerror(errno));
53         return -1;
54     }
55     memset((char *) &addr, 0, sizeof(struct sockaddr_un));
56     addr.sun_family = AF_UNIX;
57     strncpy(addr.sun_path, usock_fn, sizeof(addr.sun_path) - 1);
58     if (bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0) {
59         LOG(log_error, logtype_cnid, "error binding to socket for %s: %s",
60             usock_fn, strerror(errno));
61         return -1;
62     }
63
64     if (listen(sockfd, backlog) < 0) {
65         LOG(log_error, logtype_cnid, "error in listen for %s: %s",
66             usock_fn, strerror(errno));
67         return -1;
68     }
69
70 #ifdef chmod
71 #undef chmod
72 #endif
73     if (chmod(usock_fn, mode) < 0) {
74         LOG(log_error, logtype_cnid, "error changing permissions for %s: %s",
75             usock_fn, strerror(errno));
76         close(sockfd);
77         return -1;
78     }
79
80     return sockfd;
81 }
82
83 /* ---------------
84  * create a tcp socket
85  */
86 int tsockfd_create(char *host, char *port, int backlog)
87 {
88     int sockfd, flag, ret;
89     struct addrinfo hints, *servinfo, *p;
90
91     /* Prepare hint for getaddrinfo */
92     memset(&hints, 0, sizeof hints);
93     hints.ai_family = AF_UNSPEC;
94     hints.ai_socktype = SOCK_STREAM;
95
96     if ((ret = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
97         LOG(log_error, logtype_default, "tsockfd_create: getaddrinfo: %s\n", gai_strerror(ret));
98         return 0;
99     }
100
101     /* create a socket */
102     /* loop through all the results and bind to the first we can */
103     for (p = servinfo; p != NULL; p = p->ai_next) {
104         if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
105             LOG(log_info, logtype_default, "tsockfd_create: socket: %s", strerror(errno));
106             continue;
107         }
108
109         /*
110          * Set some socket options:
111          * SO_REUSEADDR deals w/ quick close/opens
112          * TCP_NODELAY diables Nagle
113          */
114 #ifdef SO_REUSEADDR
115         flag = 1;
116         setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
117 #endif
118
119 #ifdef USE_TCP_NODELAY
120 #ifndef SOL_TCP
121 #define SOL_TCP IPPROTO_TCP
122 #endif
123         flag = 1;
124         setsockopt(sockfd, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
125 #endif /* USE_TCP_NODELAY */
126             
127         if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
128             close(sockfd);
129             LOG(log_info, logtype_default, "tsockfd_create: bind: %s\n", strerror(errno));
130             continue;
131         }
132
133         if (listen(sockfd, backlog) < 0) {
134             close(sockfd);
135             LOG(log_info, logtype_default, "tsockfd_create: listen: %s\n", strerror(errno));
136             continue;
137         }
138
139         /* We got a socket */
140         break;
141     }
142
143     if (p == NULL)  {
144         LOG(log_error, logtype_default, "tsockfd_create: no suitable network config %s:%s", host, port);
145         freeaddrinfo(servinfo);
146         return -1;
147     }
148
149     freeaddrinfo(servinfo);
150     return sockfd;
151 }
152
153 /* --------------------- */
154 int usockfd_check(int sockfd, const sigset_t *sigset)
155 {
156     int fd;
157     socklen_t size;
158     fd_set readfds;
159     int ret;
160      
161     FD_ZERO(&readfds);
162     FD_SET(sockfd, &readfds);
163
164     if ((ret = pselect(sockfd + 1, &readfds, NULL, NULL, NULL, sigset)) < 0) {
165         if (errno == EINTR)
166             return 0;
167         LOG(log_error, logtype_cnid, "error in select: %s",
168             strerror(errno));
169         return -1;
170     }
171
172     if (ret) {
173         size = 0;
174         if ((fd = accept(sockfd, NULL, &size)) < 0) {
175             if (errno == EINTR)
176                 return 0;
177             LOG(log_error, logtype_cnid, "error in accept: %s", 
178                 strerror(errno));
179             return -1;
180         }
181         return fd;
182     } else
183         return 0;
184 }