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>
37 #define UNIX_PATH_MAX 108
41 /* FIXME: ustcomm blocks on message sending, which might be problematic in
42 * some cases. Fix the poll() usage so sends are buffered until they don't
46 //static void bt(void)
51 // result = backtrace(&buffer, 100);
52 // backtrace_symbols_fd(buffer, result, STDERR_FILENO);
55 char *strdup_malloc(const char *s
)
62 retval
= (char *) malloc(strlen(s
)+1);
69 static int signal_process(pid_t pid
)
74 int pid_is_online(pid_t pid
) {
80 * @fd: file descriptor to send to
81 * @msg: a null-terminated string containing the message to send
85 * 0: connection closed
89 static int send_message_fd(int fd
, const char *msg
)
93 /* Send including the final \0 */
94 result
= patient_send(fd
, msg
, strlen(msg
)+1, MSG_NOSIGNAL
);
100 else if(result
== 0) {
104 DBG("sent message \"%s\"", msg
);
108 /* Called by an app to ask the consumer daemon to connect to it. */
110 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
112 char path
[UNIX_PATH_MAX
];
116 struct ustcomm_connection conn
;
117 char *explicit_daemon_socket_path
;
119 explicit_daemon_socket_path
= getenv("UST_DAEMON_SOCKET");
120 if(explicit_daemon_socket_path
) {
121 /* user specified explicitly a socket path */
122 result
= snprintf(path
, UNIX_PATH_MAX
, "%s", explicit_daemon_socket_path
);
125 /* just use the default path */
126 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
129 if(result
>= UNIX_PATH_MAX
) {
130 ERR("string overflow allocating socket name");
134 asprintf(&msg
, "collect %d %s", pid
, channel
);
136 /* don't signal it because it's the daemon */
137 result
= ustcomm_connect_path(path
, &conn
, -1);
139 WARN("ustcomm_connect_path failed");
144 result
= ustcomm_send_request(&conn
, msg
, NULL
);
146 WARN("ustcomm_send_request failed");
152 ustcomm_disconnect(&conn
);
159 /* returns 1 to indicate a message was received
160 * returns 0 to indicate no message was received (cannot happen)
161 * returns -1 to indicate an error
164 #define RECV_INCREMENT 1
166 static int recv_message_fd(int fd
, char **msg
)
169 int buf_alloc_size
= 0;
171 int buf_used_size
= 0;
177 if(buf_used_size
+ RECV_INCREMENT
> buf_alloc_size
) {
179 buf
= (char *) realloc(buf
, buf_alloc_size
);
182 /* FIXME: this is really inefficient; but with count>1 we would
183 * need a buffering mechanism */
184 result
= recv(fd
, buf
+buf_used_size
, RECV_INCREMENT
, 0);
187 if(errno
!= ECONNRESET
)
201 buf_used_size
+= result
;
203 if(buf
[buf_used_size
-1] == 0) {
210 DBG("received message \"%s\"", buf
);
216 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
220 result
= send_message_fd(src
->fd
, msg
);
222 ERR("error in send_message_fd");
229 /* Called after a fork. */
231 int ustcomm_close_all_connections(struct ustcomm_server
*server
)
233 struct ustcomm_connection
*conn
;
234 struct ustcomm_connection
*deletable_conn
= NULL
;
236 list_for_each_entry(conn
, &server
->connections
, list
) {
237 free(deletable_conn
);
238 deletable_conn
= conn
;
240 list_del(&conn
->list
);
246 /* @timeout: max blocking time in milliseconds, -1 means infinity
248 * returns 1 to indicate a message was received
249 * returns 0 to indicate no message was received
250 * returns -1 to indicate an error
253 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
256 struct ustcomm_connection
*conn
;
264 list_for_each_entry(conn
, &server
->connections
, list
) {
268 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
270 ERR("malloc returned NULL");
274 /* special idx 0 is for listening socket */
275 fds
[idx
].fd
= server
->listen_fd
;
276 fds
[idx
].events
= POLLIN
;
279 list_for_each_entry(conn
, &server
->connections
, list
) {
280 fds
[idx
].fd
= conn
->fd
;
281 fds
[idx
].events
= POLLIN
;
285 while((result
= poll(fds
, n_fds
, timeout
)) == -1 && errno
== EINTR
)
296 struct ustcomm_connection
*newconn
;
299 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
305 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
306 if(newconn
== NULL
) {
307 ERR("malloc returned NULL");
313 list_add(&newconn
->list
, &server
->connections
);
316 for(idx
=1; idx
<n_fds
; idx
++) {
317 if(fds
[idx
].revents
) {
318 retval
= recv_message_fd(fds
[idx
].fd
, msg
);
320 src
->fd
= fds
[idx
].fd
;
323 /* connection finished */
326 list_for_each_entry(conn
, &server
->connections
, list
) {
327 if(conn
->fd
== fds
[idx
].fd
) {
328 list_del(&conn
->list
);
334 goto free_fds_return
;
347 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
349 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
352 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
354 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
357 /* This removes src from the list of active connections of app.
360 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
362 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
363 struct ustcomm_connection
*conn
;
365 list_for_each_entry(conn
, &server
->connections
, list
) {
366 if(conn
->fd
== src
->fd
) {
367 list_del(&conn
->list
);
377 static int init_named_socket(const char *name
, char **path_out
)
382 struct sockaddr_un addr
;
384 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
390 addr
.sun_family
= AF_UNIX
;
392 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
393 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
395 result
= access(name
, F_OK
);
398 result
= unlink(name
);
400 PERROR("unlink of socket file");
403 WARN("socket already exists; overwriting");
406 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
412 result
= listen(fd
, 1);
419 *path_out
= strdup(addr
.sun_path
);
432 * 0: Success, but no reply because recv() returned 0
436 * On error, the error message is printed, except on
437 * ECONNRESET, which is normal when the application dies.
440 int ustcomm_send_request(struct ustcomm_connection
*conn
, const char *req
, char **reply
)
444 /* Send including the final \0 */
445 result
= send_message_fd(conn
->fd
, req
);
452 result
= recv_message_fd(conn
->fd
, reply
);
456 else if(result
== 0) {
463 int ustcomm_connect_path(const char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
467 struct sockaddr_un addr
;
469 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
475 addr
.sun_family
= AF_UNIX
;
477 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
478 if(result
>= UNIX_PATH_MAX
) {
479 ERR("string overflow allocating socket name");
484 result
= signal_process(signalpid
);
486 ERR("could not signal process");
491 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
493 PERROR("connect (path=%s)", path
);
502 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
504 return close(conn
->fd
);
507 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
510 char path
[UNIX_PATH_MAX
];
513 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
514 if(result
>= UNIX_PATH_MAX
) {
515 ERR("string overflow allocating socket name");
519 return ustcomm_connect_path(path
, conn
, pid
);
522 static int ensure_dir_exists(const char *dir
)
530 result
= stat(dir
, &st
);
531 if(result
== -1 && errno
!= ENOENT
) {
534 else if(result
== -1) {
539 result
= snprintf(buf
, sizeof(buf
), "mkdir -p \"%s\"", dir
);
540 if(result
>= sizeof(buf
)) {
541 ERR("snprintf buffer overflow");
544 result
= system(buf
);
546 ERR("executing command %s", buf
);
554 /* Called by an application to initialize its server so daemons can
558 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
563 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
564 if(result
>= UNIX_PATH_MAX
) {
565 ERR("string overflow allocating socket name");
569 result
= ensure_dir_exists(SOCK_DIR
);
571 ERR("Unable to create socket directory %s", SOCK_DIR
);
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 (%s). Check that directory exists and that it is writable.", name
);
582 INIT_LIST_HEAD(&handle
->server
.connections
);
591 /* Used by the daemon to initialize its server so applications
595 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
, const char *sock_path
)
601 asprintf(&name
, "%s", sock_path
);
606 /* Only check if socket dir exists if we are using the default directory */
607 result
= ensure_dir_exists(SOCK_DIR
);
609 ERR("Unable to create socket directory %s", SOCK_DIR
);
613 asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
616 handle
->server
.listen_fd
= init_named_socket(name
, &handle
->server
.socketpath
);
617 if(handle
->server
.listen_fd
< 0) {
618 ERR("error initializing named socket at %s", name
);
623 INIT_LIST_HEAD(&handle
->server
.connections
);
631 void ustcomm_fini_app(struct ustcomm_app
*handle
)
637 result
= stat(handle
->server
.socketpath
, &st
);
639 PERROR("stat (%s)", handle
->server
.socketpath
);
643 /* Paranoid check before deleting. */
644 result
= S_ISSOCK(st
.st_mode
);
646 ERR("The socket we are about to delete is not a socket.");
650 result
= unlink(handle
->server
.socketpath
);
656 static const char *find_tok(const char *str
)
668 static const char *find_sep(const char *str
)
680 int nth_token_is(const char *str
, const char *token
, int tok_no
)
686 for(i
=0; i
<=tok_no
; i
++) {
700 if(end
-start
!= strlen(token
))
703 if(strncmp(start
, token
, end
-start
))
709 char *nth_token(const char *str
, int tok_no
)
711 static char *retval
= NULL
;
716 for(i
=0; i
<=tok_no
; i
++) {
735 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);