Revert "Revert "Upstream merge""
[framework/uifw/ecore.git] / src / lib / ecore_con / ecore_con_local.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <time.h>
9 #include <fcntl.h>
10 #include <sys/stat.h>
11
12 #ifdef HAVE_SYS_SOCKET_H
13 # include <sys/socket.h>
14 #endif
15
16 #ifdef HAVE_SYS_UN_H
17 # include <sys/un.h>
18 #endif
19
20 #ifdef HAVE_WS2TCPIP_H
21 # include <ws2tcpip.h>
22 #endif
23
24 #include <Ecore.h>
25 #include <ecore_private.h>
26
27 #include "Ecore_Con.h"
28 #include "ecore_con_private.h"
29
30 #define LENGTH_OF_SOCKADDR_UN(s)                (strlen((s)->sun_path) +                 \
31                                                  (size_t)(((struct sockaddr_un *)NULL)-> \
32                                                           sun_path))
33 #define LENGTH_OF_ABSTRACT_SOCKADDR_UN(s, path) (strlen(path) + 1 +            \
34                                                  (size_t)(((struct sockaddr_un \
35                                                             *)NULL)->sun_path))
36
37 static int _ecore_con_local_init_count = 0;
38
39 int
40 ecore_con_local_init(void)
41 {
42    if (++_ecore_con_local_init_count != 1)
43      return _ecore_con_local_init_count;
44
45    return _ecore_con_local_init_count;
46 }
47
48 int
49 ecore_con_local_shutdown(void)
50 {
51    if (--_ecore_con_local_init_count != 0)
52      return _ecore_con_local_init_count;
53
54    return _ecore_con_local_init_count;
55 }
56
57 int
58 ecore_con_local_connect(Ecore_Con_Server *svr,
59                         Eina_Bool (*cb_done)(void *data, Ecore_Fd_Handler *fd_handler),
60                         void *data __UNUSED__)
61 {
62 #ifndef HAVE_LOCAL_SOCKETS
63    return 0;
64 #else
65    char buf[4096];
66    struct sockaddr_un socket_unix;
67    int curstate = 0;
68    const char *homedir;
69    int socket_unix_len;
70
71    if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_USER)
72      {
73         homedir = getenv("HOME");
74         if (!homedir)
75           homedir = getenv("TMP");
76
77         if (!homedir)
78           homedir = "/tmp";
79
80         snprintf(buf, sizeof(buf), "%s/.ecore/%s/%i", homedir, svr->name,
81                  svr->port);
82      }
83    else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_SYSTEM)
84      {
85         if (svr->port < 0)
86           {
87              if (svr->name[0] == '/')
88                strncpy(buf, svr->name, sizeof(buf));
89              else
90                snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s", svr->name);
91           }
92         else
93           {
94              if (svr->name[0] ==
95                  '/')
96                snprintf(buf, sizeof(buf), "%s|%i", svr->name,
97                         svr->port);
98              else
99                snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s|%i",
100                         svr->name,
101                         svr->port);
102           }
103      }
104    else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
105      strncpy(buf, svr->name,
106              sizeof(buf));
107
108    svr->fd = socket(AF_UNIX, SOCK_STREAM, 0);
109    if (svr->fd < 0)
110      return 0;
111
112    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
113      return 0;
114
115    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
116      return 0;
117
118    if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate,
119                   sizeof(curstate)) < 0)
120      return 0;
121
122    socket_unix.sun_family = AF_UNIX;
123
124    if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
125      {
126 #ifdef HAVE_ABSTRACT_SOCKETS
127         /* copy name insto sun_path, prefixed by null to indicate abstract namespace */
128         snprintf(socket_unix.sun_path, sizeof(socket_unix.sun_path), ".%s",
129                  svr->name);
130         socket_unix.sun_path[0] = '\0';
131         socket_unix_len = LENGTH_OF_ABSTRACT_SOCKADDR_UN(&socket_unix,
132                                                          svr->name);
133 #else
134         WRN("Your system does not support abstract sockets!");
135         return 0;
136 #endif
137      }
138    else
139      {
140         strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path));
141         socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix);
142      }
143
144    if (connect(svr->fd, (struct sockaddr *)&socket_unix,
145                socket_unix_len) < 0)
146      {
147         ERR("local connection failed: %s", strerror(errno));
148         return 0;
149      }
150
151    svr->path = strdup(buf);
152    if (!svr->path)
153      return 0;
154
155    if (svr->type & ECORE_CON_SSL)
156      ecore_con_ssl_server_init(svr);
157
158    svr->fd_handler =
159      ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
160                                cb_done, svr, NULL, NULL);
161    if (!svr->fd_handler)
162      return 0;
163
164    if (!svr->delete_me) ecore_con_event_server_add(svr);
165
166    return 1;
167 #endif
168 }
169
170 int
171 ecore_con_local_listen(
172   Ecore_Con_Server *svr,
173   Eina_Bool         (*
174                      cb_listen)(void *data,
175                                 Ecore_Fd_Handler *
176                                 fd_handler),
177   void *data
178   __UNUSED__)
179 {
180 #ifdef HAVE_LOCAL_SOCKETS
181    char buf[4096];
182    struct sockaddr_un socket_unix;
183    struct linger lin;
184    mode_t pmode;
185    const char *homedir;
186    struct stat st;
187    mode_t mask;
188    int socket_unix_len;
189
190    mask = S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH;
191
192    if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_USER)
193      {
194         homedir = getenv("HOME");
195         if (!homedir)
196           homedir = getenv("TMP");
197
198         if (!homedir)
199           homedir = "/tmp";
200
201         mask = S_IRUSR | S_IWUSR | S_IXUSR;
202         snprintf(buf, sizeof(buf), "%s/.ecore", homedir);
203         if (stat(buf, &st) < 0)
204           mkdir(buf, mask);
205
206         snprintf(buf, sizeof(buf), "%s/.ecore/%s", homedir, svr->name);
207         if (stat(buf, &st) < 0)
208           mkdir(buf, mask);
209
210         snprintf(buf,
211                  sizeof(buf),
212                  "%s/.ecore/%s/%i",
213                  homedir,
214                  svr->name,
215                  svr->port);
216         mask = S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH;
217      }
218    else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_SYSTEM)
219      {
220         mask = 0;
221         if (svr->name[0] == '/')
222           {
223              if (svr->port >= 0)
224                snprintf(buf,
225                         sizeof(buf),
226                         "%s|%i",
227                         svr->name,
228                         svr->port);
229              else
230                snprintf(buf,
231                         sizeof(buf),
232                         "%s",
233                         svr->name);
234           }
235         else
236           snprintf(buf,
237                    sizeof(buf),
238                    "/tmp/.ecore_service|%s|%i",
239                    svr->name,
240                    svr->port);
241      }
242    else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
243      strncpy(buf, svr->name,
244              sizeof(buf));
245
246    pmode = umask(mask);
247 start:
248    svr->fd = socket(AF_UNIX, SOCK_STREAM, 0);
249    if (svr->fd < 0)
250      goto error_umask;
251
252    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
253      goto error_umask;
254
255    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
256      goto error_umask;
257
258    lin.l_onoff = 1;
259    lin.l_linger = 0;
260    if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, (const void *)&lin,
261                   sizeof(struct linger)) < 0)
262      goto error_umask;
263
264    socket_unix.sun_family = AF_UNIX;
265    if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
266      {
267 #ifdef HAVE_ABSTRACT_SOCKETS
268         /* . is a placeholder */
269         snprintf(socket_unix.sun_path, sizeof(socket_unix.sun_path), ".%s",
270                  svr->name);
271         /* first char null indicates abstract namespace */
272         socket_unix.sun_path[0] = '\0';
273         socket_unix_len = LENGTH_OF_ABSTRACT_SOCKADDR_UN(&socket_unix,
274                                                          svr->name);
275 #else
276         ERR("Your system does not support abstract sockets!");
277         goto error_umask;
278 #endif
279      }
280    else
281      {
282         strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path));
283         socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix);
284      }
285
286    if (bind(svr->fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0)
287      {
288         if ((((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_USER) ||
289              ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_SYSTEM)) &&
290             (connect(svr->fd, (struct sockaddr *)&socket_unix,
291                      socket_unix_len) < 0) &&
292             (unlink(buf) >= 0))
293           goto start;
294         else
295           goto error_umask;
296      }
297
298    if (listen(svr->fd, 4096) < 0)
299      goto error_umask;
300
301    svr->path = strdup(buf);
302    if (!svr->path)
303      goto error_umask;
304
305    svr->fd_handler =
306      ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
307                                cb_listen, svr, NULL, NULL);
308    umask(pmode);
309    if (!svr->fd_handler)
310      goto error;
311
312    return 1;
313
314 error_umask:
315    umask(pmode);
316 error:
317 #endif /* HAVE_LOCAL_SOCKETS */
318    return 0;
319 }
320