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 #include "multipoll.h"
38 #define UNIX_PATH_MAX 108
40 static int mkdir_p(const char *path
, mode_t mode
)
48 tmp
= malloc(strlen(path
) + 1);
56 while (*path_p
!= '/') {
62 strncpy(tmp
, path
, path_p
- path
);
63 tmp
[path_p
-path
] = '\0';
64 if (tmp
[path_p
- path
- 1] != '/') {
65 result
= mkdir(tmp
, mode
);
67 if (!(errno
== EEXIST
|| errno
== EACCES
|| errno
== EROFS
)) {
68 /* Then this is a real error */
78 result
= mkdir(path
, mode
);
89 char *strdup_malloc(const char *s
)
96 retval
= (char *) malloc(strlen(s
)+1);
103 static int signal_process(pid_t pid
)
108 void ustcomm_init_connection(struct ustcomm_connection
*conn
)
110 conn
->recv_buf
= NULL
;
111 conn
->recv_buf_size
= 0;
112 conn
->recv_buf_alloc
= 0;
115 int pid_is_online(pid_t pid
) {
121 * @fd: file descriptor to send to
122 * @msg: a null-terminated string containing the message to send
126 * 0: connection closed
130 static int send_message_fd(int fd
, const char *msg
)
134 /* Send including the final \0 */
135 result
= patient_send(fd
, msg
, strlen(msg
)+1, MSG_NOSIGNAL
);
141 else if(result
== 0) {
145 DBG("sent message \"%s\"", msg
);
149 /* Called by an app to ask the consumer daemon to connect to it. */
151 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
153 char path
[UNIX_PATH_MAX
];
157 struct ustcomm_connection conn
;
158 char *explicit_daemon_socket_path
;
160 explicit_daemon_socket_path
= getenv("UST_DAEMON_SOCKET");
161 if(explicit_daemon_socket_path
) {
162 /* user specified explicitly a socket path */
163 result
= snprintf(path
, UNIX_PATH_MAX
, "%s", explicit_daemon_socket_path
);
166 /* just use the default path */
167 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
170 if(result
>= UNIX_PATH_MAX
) {
171 ERR("string overflow allocating socket name");
175 asprintf(&msg
, "collect %d %s", pid
, channel
);
177 /* don't signal it because it's the daemon */
178 result
= ustcomm_connect_path(path
, &conn
, -1);
180 WARN("ustcomm_connect_path failed");
185 result
= ustcomm_send_request(&conn
, msg
, NULL
);
187 WARN("ustcomm_send_request failed");
193 ustcomm_disconnect(&conn
);
200 /* returns 1 to indicate a message was received
201 * returns 0 to indicate no message was received (end of stream)
202 * returns -1 to indicate an error
205 #define RECV_INCREMENT 1000
206 #define RECV_INITIAL_BUF_SIZE 10
208 static int recv_message_fd(int fd
, char **recv_buf
, int *recv_buf_size
, int *recv_buf_alloc
, char **msg
)
212 /* 1. Check if there is a message in the buf */
214 2.1 receive chunk and put it in buffer
215 2.2 process full message if there is one
216 -- while no message arrived
223 /* Search for full message in buffer */
224 for(i
=0; i
<*recv_buf_size
; i
++) {
225 if((*recv_buf
)[i
] == '\0') {
231 /* Process found message */
237 WARN("received empty message");
239 *msg
= strndup(*recv_buf
, i
);
241 /* Remove processed message from buffer */
242 newbuf
= (char *) malloc(*recv_buf_size
- (i
+1));
243 memcpy(newbuf
, *recv_buf
+ (i
+1), *recv_buf_size
- (i
+1));
246 *recv_buf_size
-= (i
+1);
247 *recv_buf_alloc
-= (i
+1);
252 /* Receive a chunk from the fd */
253 if(*recv_buf_alloc
- *recv_buf_size
< RECV_INCREMENT
) {
254 *recv_buf_alloc
+= RECV_INCREMENT
- (*recv_buf_alloc
- *recv_buf_size
);
255 *recv_buf
= (char *) realloc(*recv_buf
, *recv_buf_alloc
);
258 result
= recv(fd
, *recv_buf
+*recv_buf_size
, RECV_INCREMENT
, 0);
260 if(errno
== ECONNRESET
) {
271 *recv_buf_size
+= result
;
273 /* Go back to the beginning to check if there is a full message in the buffer */
276 DBG("received message \"%s\"", *recv_buf
);
282 static int recv_message_conn(struct ustcomm_connection
*conn
, char **msg
)
284 return recv_message_fd(conn
->fd
, &conn
->recv_buf
, &conn
->recv_buf_size
, &conn
->recv_buf_alloc
, msg
);
287 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
291 result
= send_message_fd(src
->fd
, msg
);
293 ERR("error in send_message_fd");
300 /* Called after a fork. */
302 int ustcomm_close_all_connections(struct ustcomm_server
*server
)
304 struct ustcomm_connection
*conn
;
305 struct ustcomm_connection
*deletable_conn
= NULL
;
307 list_for_each_entry(conn
, &server
->connections
, list
) {
308 free(deletable_conn
);
309 deletable_conn
= conn
;
310 ustcomm_close_app(conn
);
311 list_del(&conn
->list
);
317 /* @timeout: max blocking time in milliseconds, -1 means infinity
319 * returns 1 to indicate a message was received
320 * returns 0 to indicate no message was received
321 * returns -1 to indicate an error
324 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
327 struct ustcomm_connection
**conn_table
;
328 struct ustcomm_connection
*conn
;
336 list_for_each_entry(conn
, &server
->connections
, list
) {
340 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
342 ERR("malloc returned NULL");
346 conn_table
= (struct ustcomm_connection
**) malloc(n_fds
* sizeof(struct ustcomm_connection
*));
347 if(conn_table
== NULL
) {
348 ERR("malloc returned NULL");
350 goto free_fds_return
;
353 /* special idx 0 is for listening socket */
354 fds
[idx
].fd
= server
->listen_fd
;
355 fds
[idx
].events
= POLLIN
;
358 list_for_each_entry(conn
, &server
->connections
, list
) {
359 fds
[idx
].fd
= conn
->fd
;
360 fds
[idx
].events
= POLLIN
;
361 conn_table
[idx
] = conn
;
365 while((result
= poll(fds
, n_fds
, timeout
)) == -1 && errno
== EINTR
)
370 goto free_conn_table_return
;
375 goto free_conn_table_return
;
379 struct ustcomm_connection
*newconn
;
382 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
386 goto free_conn_table_return
;
389 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
390 if(newconn
== NULL
) {
391 ERR("malloc returned NULL");
395 ustcomm_init_connection(newconn
);
398 list_add(&newconn
->list
, &server
->connections
);
401 for(idx
=1; idx
<n_fds
; idx
++) {
402 if(fds
[idx
].revents
) {
403 retval
= recv_message_conn(conn_table
[idx
], msg
);
405 src
->fd
= fds
[idx
].fd
;
408 /* connection finished */
409 list_for_each_entry(conn
, &server
->connections
, list
) {
410 if(conn
->fd
== fds
[idx
].fd
) {
411 ustcomm_close_app(conn
);
412 list_del(&conn
->list
);
419 goto free_conn_table_return
;
428 free_conn_table_return
:
435 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
437 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
440 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
442 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
445 /* This removes src from the list of active connections of app.
448 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
450 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
451 struct ustcomm_connection
*conn
;
453 list_for_each_entry(conn
, &server
->connections
, list
) {
454 if(conn
->fd
== src
->fd
) {
455 list_del(&conn
->list
);
465 static int init_named_socket(const char *name
, char **path_out
)
470 struct sockaddr_un addr
;
472 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
478 addr
.sun_family
= AF_UNIX
;
480 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
481 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
483 result
= access(name
, F_OK
);
486 result
= unlink(name
);
488 PERROR("unlink of socket file");
491 DBG("socket already exists; overwriting");
494 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
500 result
= listen(fd
, 1);
507 *path_out
= strdup(addr
.sun_path
);
520 * 0: Success, but no reply because recv() returned 0
524 * On error, the error message is printed, except on
525 * ECONNRESET, which is normal when the application dies.
528 int ustcomm_send_request(struct ustcomm_connection
*conn
, const char *req
, char **reply
)
532 /* Send including the final \0 */
533 result
= send_message_fd(conn
->fd
, req
);
540 result
= recv_message_conn(conn
, reply
);
544 else if(result
== 0) {
556 int ustcomm_connect_path(const char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
560 struct sockaddr_un addr
;
562 ustcomm_init_connection(conn
);
564 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
570 addr
.sun_family
= AF_UNIX
;
572 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
573 if(result
>= UNIX_PATH_MAX
) {
574 ERR("string overflow allocating socket name");
579 result
= signal_process(signalpid
);
581 ERR("could not signal process");
586 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
588 PERROR("connect (path=%s)", path
);
597 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
599 return close(conn
->fd
);
602 /* Open a connection to a traceable app.
609 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
612 char path
[UNIX_PATH_MAX
];
615 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
616 if(result
>= UNIX_PATH_MAX
) {
617 ERR("string overflow allocating socket name");
621 return ustcomm_connect_path(path
, conn
, pid
);
624 /* Close a connection to a traceable app. It frees the
625 * resources. It however does not free the
626 * ustcomm_connection itself.
629 int ustcomm_close_app(struct ustcomm_connection
*conn
)
632 free(conn
->recv_buf
);
637 static int ensure_dir_exists(const char *dir
)
645 result
= stat(dir
, &st
);
646 if(result
== -1 && errno
!= ENOENT
) {
649 else if(result
== -1) {
653 result
= mkdir_p(dir
, 0777);
655 ERR("executing in recursive creation of directory %s", dir
);
663 /* Called by an application to initialize its server so daemons can
667 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
672 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
673 if(result
>= UNIX_PATH_MAX
) {
674 ERR("string overflow allocating socket name");
678 result
= ensure_dir_exists(SOCK_DIR
);
680 ERR("Unable to create socket directory %s", SOCK_DIR
);
684 handle
->server
.listen_fd
= init_named_socket(name
, &(handle
->server
.socketpath
));
685 if(handle
->server
.listen_fd
< 0) {
686 ERR("Error initializing named socket (%s). Check that directory exists and that it is writable.", name
);
691 INIT_LIST_HEAD(&handle
->server
.connections
);
700 /* Used by the daemon to initialize its server so applications
704 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
, const char *sock_path
)
710 asprintf(&name
, "%s", sock_path
);
715 /* Only check if socket dir exists if we are using the default directory */
716 result
= ensure_dir_exists(SOCK_DIR
);
718 ERR("Unable to create socket directory %s", SOCK_DIR
);
722 asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
725 handle
->server
.listen_fd
= init_named_socket(name
, &handle
->server
.socketpath
);
726 if(handle
->server
.listen_fd
< 0) {
727 ERR("error initializing named socket at %s", name
);
732 INIT_LIST_HEAD(&handle
->server
.connections
);
740 static void ustcomm_fini_server(struct ustcomm_server
*server
, int keep_socket_file
)
745 if(!keep_socket_file
) {
747 result
= stat(server
->socketpath
, &st
);
749 PERROR("stat (%s)", server
->socketpath
);
753 /* Paranoid check before deleting. */
754 result
= S_ISSOCK(st
.st_mode
);
756 ERR("The socket we are about to delete is not a socket.");
760 result
= unlink(server
->socketpath
);
766 free(server
->socketpath
);
768 result
= close(server
->listen_fd
);
775 /* Free a traceable application server */
777 void ustcomm_fini_app(struct ustcomm_app
*handle
, int keep_socket_file
)
779 ustcomm_fini_server(&handle
->server
, keep_socket_file
);
782 /* Free a ustd server */
784 void ustcomm_fini_ustd(struct ustcomm_ustd
*handle
)
786 ustcomm_fini_server(&handle
->server
, 0);
789 static const char *find_tok(const char *str
)
801 static const char *find_sep(const char *str
)
813 int nth_token_is(const char *str
, const char *token
, int tok_no
)
819 for(i
=0; i
<=tok_no
; i
++) {
833 if(end
-start
!= strlen(token
))
836 if(strncmp(start
, token
, end
-start
))
842 char *nth_token(const char *str
, int tok_no
)
844 static char *retval
= NULL
;
849 for(i
=0; i
<=tok_no
; i
++) {
868 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);
873 /* Callback from multipoll.
874 * Receive a new connection on the listening socket.
877 static int process_mp_incoming_conn(void *priv
, int fd
, short events
)
879 struct ustcomm_connection
*newconn
;
880 struct ustcomm_server
*server
= (struct ustcomm_server
*) priv
;
884 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
890 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
891 if(newconn
== NULL
) {
892 ERR("malloc returned NULL");
896 ustcomm_init_connection(newconn
);
899 list_add(&newconn
->list
, &server
->connections
);
904 /* Callback from multipoll.
905 * Receive a message on an existing connection.
908 static int process_mp_conn_msg(void *priv
, int fd
, short revents
)
910 struct ustcomm_multipoll_conn_info
*mpinfo
= (struct ustcomm_multipoll_conn_info
*) priv
;
913 struct ustcomm_source src
;
918 result
= recv_message_conn(mpinfo
->conn
, &msg
);
920 ERR("error in recv_message_conn");
923 else if(result
== 0) {
924 /* connection finished */
925 ustcomm_close_app(mpinfo
->conn
);
926 list_del(&mpinfo
->conn
->list
);
930 mpinfo
->cb(msg
, &src
);
938 int free_ustcomm_client_poll(void *data
)
944 void ustcomm_mp_add_app_clients(struct mpentries
*ent
, struct ustcomm_app
*app
, int (*cb
)(struct ustcomm_connection
*conn
, char *msg
))
946 struct ustcomm_connection
*conn
;
948 /* add listener socket */
949 multipoll_add(ent
, app
->server
.listen_fd
, POLLIN
, process_mp_incoming_conn
, &app
->server
, NULL
);
951 list_for_each_entry(conn
, &app
->server
.connections
, list
) {
952 struct ustcomm_multipoll_conn_info
*mpinfo
= (struct ustcomm_multipoll_conn_info
*) malloc(sizeof(struct ustcomm_multipoll_conn_info
));
955 multipoll_add(ent
, conn
->fd
, POLLIN
, process_mp_conn_msg
, mpinfo
, free_ustcomm_client_poll
);