1 /* Copyright (C) 2009 Pierre-Marc Fournier
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <sys/types.h>
22 #include <sys/socket.h>
36 #define UNIX_PATH_MAX 108
40 /* FIXME: ustcomm blocks on message sending, which might be problematic in
41 * some cases. Fix the poll() usage so sends are buffered until they don't
45 //static void bt(void)
50 // result = backtrace(&buffer, 100);
51 // backtrace_symbols_fd(buffer, result, STDERR_FILENO);
54 char *strdup_malloc(const char *s
)
61 retval
= (char *) malloc(strlen(s
)+1);
68 static int signal_process(pid_t pid
)
73 int pid_is_online(pid_t pid
) {
77 static int send_message_fd(int fd
, const char *msg
)
81 result
= send(fd
, msg
, strlen(msg
), MSG_NOSIGNAL
);
86 else if(result
== 0) {
93 /* Called by an app to ask the consumer daemon to connect to it. */
95 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
97 char path
[UNIX_PATH_MAX
];
101 struct ustcomm_connection conn
;
102 char *explicit_daemon_socket_path
;
104 explicit_daemon_socket_path
= getenv("UST_DAEMON_SOCKET");
105 if(explicit_daemon_socket_path
) {
106 /* user specified explicitly a socket path */
107 result
= snprintf(path
, UNIX_PATH_MAX
, "%s", explicit_daemon_socket_path
);
110 /* just use the default path */
111 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
114 if(result
>= UNIX_PATH_MAX
) {
115 ERR("string overflow allocating socket name");
119 asprintf(&msg
, "collect %d %s", pid
, channel
);
121 /* don't signal it because it's the daemon */
122 result
= ustcomm_connect_path(path
, &conn
, -1);
124 WARN("ustcomm_connect_path failed");
129 result
= ustcomm_send_request(&conn
, msg
, NULL
);
131 WARN("ustcomm_send_request failed");
137 ustcomm_disconnect(&conn
);
144 /* returns 1 to indicate a message was received
145 * returns 0 to indicate no message was received (cannot happen)
146 * returns -1 to indicate an error
149 static int recv_message_fd(int fd
, char **msg
, struct ustcomm_source
*src
)
153 *msg
= (char *) malloc(MSG_MAX
+1);
155 result
= recv(fd
, *msg
, MSG_MAX
, 0);
161 (*msg
)[result
] = '\0';
163 DBG("ustcomm_app_recv_message: result is %d, message is %s", result
, (*msg
));
171 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
175 result
= send_message_fd(src
->fd
, msg
);
177 ERR("error in send_message_fd");
184 /* Called after a fork. */
186 int ustcomm_close_all_connections(struct ustcomm_server
*server
)
188 struct ustcomm_connection
*conn
;
189 struct ustcomm_connection
*deletable_conn
= NULL
;
191 list_for_each_entry(conn
, &server
->connections
, list
) {
192 free(deletable_conn
);
193 deletable_conn
= conn
;
195 list_del(&conn
->list
);
201 /* @timeout: max blocking time in milliseconds, -1 means infinity
203 * returns 1 to indicate a message was received
204 * returns 0 to indicate no message was received
205 * returns -1 to indicate an error
208 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
211 struct ustcomm_connection
*conn
;
219 list_for_each_entry(conn
, &server
->connections
, list
) {
223 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
225 ERR("malloc returned NULL");
229 /* special idx 0 is for listening socket */
230 fds
[idx
].fd
= server
->listen_fd
;
231 fds
[idx
].events
= POLLIN
;
234 list_for_each_entry(conn
, &server
->connections
, list
) {
235 fds
[idx
].fd
= conn
->fd
;
236 fds
[idx
].events
= POLLIN
;
240 while((result
= poll(fds
, n_fds
, timeout
)) == -1 && errno
== EINTR
)
251 struct ustcomm_connection
*newconn
;
254 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
260 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
261 if(newconn
== NULL
) {
262 ERR("malloc returned NULL");
268 list_add(&newconn
->list
, &server
->connections
);
271 for(idx
=1; idx
<n_fds
; idx
++) {
272 if(fds
[idx
].revents
) {
273 retval
= recv_message_fd(fds
[idx
].fd
, msg
, src
);
275 /* connection finished */
278 list_for_each_entry(conn
, &server
->connections
, list
) {
279 if(conn
->fd
== fds
[idx
].fd
) {
280 list_del(&conn
->list
);
286 goto free_fds_return
;
299 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
301 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
304 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
306 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
309 /* This removes src from the list of active connections of app.
312 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
314 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
315 struct ustcomm_connection
*conn
;
317 list_for_each_entry(conn
, &server
->connections
, list
) {
318 if(conn
->fd
== src
->fd
) {
319 list_del(&conn
->list
);
329 static int init_named_socket(const char *name
, char **path_out
)
334 struct sockaddr_un addr
;
336 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
342 addr
.sun_family
= AF_UNIX
;
344 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
345 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
347 result
= access(name
, F_OK
);
350 result
= unlink(name
);
352 PERROR("unlink of socket file");
355 WARN("socket already exists; overwriting");
358 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
364 result
= listen(fd
, 1);
371 *path_out
= strdup(addr
.sun_path
);
384 * 0: Success, but no reply because recv() returned 0
388 * On error, the error message is printed, except on
389 * ECONNRESET, which is normal when the application dies.
392 int ustcomm_send_request(struct ustcomm_connection
*conn
, const char *req
, char **reply
)
396 result
= send(conn
->fd
, req
, strlen(req
), MSG_NOSIGNAL
);
406 *reply
= (char *) malloc(MSG_MAX
+1);
407 result
= recv(conn
->fd
, *reply
, MSG_MAX
, 0);
409 if(errno
!= ECONNRESET
)
413 else if(result
== 0) {
417 (*reply
)[result
] = '\0';
422 int ustcomm_connect_path(const char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
426 struct sockaddr_un addr
;
428 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
434 addr
.sun_family
= AF_UNIX
;
436 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
437 if(result
>= UNIX_PATH_MAX
) {
438 ERR("string overflow allocating socket name");
443 result
= signal_process(signalpid
);
445 ERR("could not signal process");
450 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
461 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
463 return close(conn
->fd
);
466 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
469 char path
[UNIX_PATH_MAX
];
472 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
473 if(result
>= UNIX_PATH_MAX
) {
474 ERR("string overflow allocating socket name");
478 return ustcomm_connect_path(path
, conn
, pid
);
481 static int ensure_dir_exists(const char *dir
)
489 result
= stat(dir
, &st
);
490 if(result
== -1 && errno
!= ENOENT
) {
493 else if(result
== -1) {
498 result
= snprintf(buf
, sizeof(buf
), "mkdir -p \"%s\"", dir
);
499 if(result
>= sizeof(buf
)) {
500 ERR("snprintf buffer overflow");
503 result
= system(buf
);
505 ERR("executing command %s", buf
);
513 /* Called by an application to initialize its server so daemons can
517 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
522 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
523 if(result
>= UNIX_PATH_MAX
) {
524 ERR("string overflow allocating socket name");
528 result
= ensure_dir_exists(SOCK_DIR
);
530 ERR("Unable to create socket directory %s", SOCK_DIR
);
534 handle
->server
.listen_fd
= init_named_socket(name
, &(handle
->server
.socketpath
));
535 if(handle
->server
.listen_fd
< 0) {
536 ERR("Error initializing named socket (%s). Check that directory exists and that it is writable.", name
);
541 INIT_LIST_HEAD(&handle
->server
.connections
);
550 /* Used by the daemon to initialize its server so applications
554 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
, const char *sock_path
)
560 asprintf(&name
, "%s", sock_path
);
565 /* Only check if socket dir exists if we are using the default directory */
566 result
= ensure_dir_exists(SOCK_DIR
);
568 ERR("Unable to create socket directory %s", SOCK_DIR
);
572 asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
575 handle
->server
.listen_fd
= init_named_socket(name
, &handle
->server
.socketpath
);
576 if(handle
->server
.listen_fd
< 0) {
577 ERR("error initializing named socket at %s", name
);
582 INIT_LIST_HEAD(&handle
->server
.connections
);
590 void ustcomm_fini_app(struct ustcomm_app
*handle
)
596 result
= stat(handle
->server
.socketpath
, &st
);
598 PERROR("stat (%s)", handle
->server
.socketpath
);
602 /* Paranoid check before deleting. */
603 result
= S_ISSOCK(st
.st_mode
);
605 ERR("The socket we are about to delete is not a socket.");
609 result
= unlink(handle
->server
.socketpath
);
615 static char *find_tok(char *str
)
627 static char *find_sep(char *str
)
639 int nth_token_is(char *str
, char *token
, int tok_no
)
645 for(i
=0; i
<=tok_no
; i
++) {
659 if(end
-start
!= strlen(token
))
662 if(strncmp(start
, token
, end
-start
))
668 char *nth_token(char *str
, int tok_no
)
670 static char *retval
= NULL
;
675 for(i
=0; i
<=tok_no
; i
++) {
694 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);