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 static int signal_process(pid_t pid
)
94 void ustcomm_init_connection(struct ustcomm_connection
*conn
)
96 conn
->recv_buf
= NULL
;
97 conn
->recv_buf_size
= 0;
98 conn
->recv_buf_alloc
= 0;
101 int pid_is_online(pid_t pid
) {
107 * @fd: file descriptor to send to
108 * @msg: a null-terminated string containing the message to send
112 * 0: connection closed
116 static int send_message_fd(int fd
, const char *msg
)
120 /* Send including the final \0 */
121 result
= patient_send(fd
, msg
, strlen(msg
)+1, MSG_NOSIGNAL
);
127 else if(result
== 0) {
131 DBG("sent message \"%s\"", msg
);
135 /* Called by an app to ask the consumer daemon to connect to it. */
137 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
139 char path
[UNIX_PATH_MAX
];
143 struct ustcomm_connection conn
;
144 char *explicit_daemon_socket_path
;
146 explicit_daemon_socket_path
= getenv("UST_DAEMON_SOCKET");
147 if(explicit_daemon_socket_path
) {
148 /* user specified explicitly a socket path */
149 result
= snprintf(path
, UNIX_PATH_MAX
, "%s", explicit_daemon_socket_path
);
152 /* just use the default path */
153 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
156 if(result
>= UNIX_PATH_MAX
) {
157 ERR("string overflow allocating socket name");
161 asprintf(&msg
, "collect %d %s", pid
, channel
);
163 /* don't signal it because it's the daemon */
164 result
= ustcomm_connect_path(path
, &conn
, -1);
166 WARN("ustcomm_connect_path failed");
171 result
= ustcomm_send_request(&conn
, msg
, NULL
);
173 WARN("ustcomm_send_request failed");
179 ustcomm_disconnect(&conn
);
186 /* returns 1 to indicate a message was received
187 * returns 0 to indicate no message was received (end of stream)
188 * returns -1 to indicate an error
191 #define RECV_INCREMENT 1000
192 #define RECV_INITIAL_BUF_SIZE 10
194 static int recv_message_fd(int fd
, char **recv_buf
, int *recv_buf_size
, int *recv_buf_alloc
, char **msg
)
198 /* 1. Check if there is a message in the buf */
200 2.1 receive chunk and put it in buffer
201 2.2 process full message if there is one
202 -- while no message arrived
209 /* Search for full message in buffer */
210 for(i
=0; i
<*recv_buf_size
; i
++) {
211 if((*recv_buf
)[i
] == '\0') {
217 /* Process found message */
223 WARN("received empty message");
225 *msg
= strndup(*recv_buf
, i
);
227 /* Remove processed message from buffer */
228 newbuf
= (char *) malloc(*recv_buf_size
- (i
+1));
229 memcpy(newbuf
, *recv_buf
+ (i
+1), *recv_buf_size
- (i
+1));
232 *recv_buf_size
-= (i
+1);
233 *recv_buf_alloc
-= (i
+1);
238 /* Receive a chunk from the fd */
239 if(*recv_buf_alloc
- *recv_buf_size
< RECV_INCREMENT
) {
240 *recv_buf_alloc
+= RECV_INCREMENT
- (*recv_buf_alloc
- *recv_buf_size
);
241 *recv_buf
= (char *) realloc(*recv_buf
, *recv_buf_alloc
);
244 result
= recv(fd
, *recv_buf
+*recv_buf_size
, RECV_INCREMENT
, 0);
246 if(errno
== ECONNRESET
) {
257 *recv_buf_size
+= result
;
259 /* Go back to the beginning to check if there is a full message in the buffer */
262 DBG("received message \"%s\"", *recv_buf
);
268 static int recv_message_conn(struct ustcomm_connection
*conn
, char **msg
)
270 return recv_message_fd(conn
->fd
, &conn
->recv_buf
, &conn
->recv_buf_size
, &conn
->recv_buf_alloc
, msg
);
273 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
277 result
= send_message_fd(src
->fd
, msg
);
279 ERR("error in send_message_fd");
286 /* Called after a fork. */
288 int ustcomm_close_all_connections(struct ustcomm_server
*server
)
290 struct ustcomm_connection
*conn
;
291 struct ustcomm_connection
*deletable_conn
= NULL
;
293 list_for_each_entry(conn
, &server
->connections
, list
) {
294 free(deletable_conn
);
295 deletable_conn
= conn
;
296 ustcomm_close_app(conn
);
297 list_del(&conn
->list
);
303 /* @timeout: max blocking time in milliseconds, -1 means infinity
305 * returns 1 to indicate a message was received
306 * returns 0 to indicate no message was received
307 * returns -1 to indicate an error
310 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
313 struct ustcomm_connection
**conn_table
;
314 struct ustcomm_connection
*conn
;
322 list_for_each_entry(conn
, &server
->connections
, list
) {
326 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
328 ERR("malloc returned NULL");
332 conn_table
= (struct ustcomm_connection
**) malloc(n_fds
* sizeof(struct ustcomm_connection
*));
333 if(conn_table
== NULL
) {
334 ERR("malloc returned NULL");
336 goto free_fds_return
;
339 /* special idx 0 is for listening socket */
340 fds
[idx
].fd
= server
->listen_fd
;
341 fds
[idx
].events
= POLLIN
;
344 list_for_each_entry(conn
, &server
->connections
, list
) {
345 fds
[idx
].fd
= conn
->fd
;
346 fds
[idx
].events
= POLLIN
;
347 conn_table
[idx
] = conn
;
351 while((result
= poll(fds
, n_fds
, timeout
)) == -1 && errno
== EINTR
)
356 goto free_conn_table_return
;
361 goto free_conn_table_return
;
365 struct ustcomm_connection
*newconn
;
368 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
372 goto free_conn_table_return
;
375 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
376 if(newconn
== NULL
) {
377 ERR("malloc returned NULL");
381 ustcomm_init_connection(newconn
);
384 list_add(&newconn
->list
, &server
->connections
);
387 for(idx
=1; idx
<n_fds
; idx
++) {
388 if(fds
[idx
].revents
) {
389 retval
= recv_message_conn(conn_table
[idx
], msg
);
391 src
->fd
= fds
[idx
].fd
;
394 /* connection finished */
395 list_for_each_entry(conn
, &server
->connections
, list
) {
396 if(conn
->fd
== fds
[idx
].fd
) {
397 ustcomm_close_app(conn
);
398 list_del(&conn
->list
);
405 goto free_conn_table_return
;
414 free_conn_table_return
:
421 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
423 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
426 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
428 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
431 /* This removes src from the list of active connections of app.
434 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
436 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
437 struct ustcomm_connection
*conn
;
439 list_for_each_entry(conn
, &server
->connections
, list
) {
440 if(conn
->fd
== src
->fd
) {
441 list_del(&conn
->list
);
451 static int init_named_socket(const char *name
, char **path_out
)
456 struct sockaddr_un addr
;
458 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
464 addr
.sun_family
= AF_UNIX
;
466 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
467 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
469 result
= access(name
, F_OK
);
472 result
= unlink(name
);
474 PERROR("unlink of socket file");
477 DBG("socket already exists; overwriting");
480 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
486 result
= listen(fd
, 1);
493 *path_out
= strdup(addr
.sun_path
);
506 * 0: Success, but no reply because recv() returned 0
510 * On error, the error message is printed, except on
511 * ECONNRESET, which is normal when the application dies.
514 int ustcomm_send_request(struct ustcomm_connection
*conn
, const char *req
, char **reply
)
518 /* Send including the final \0 */
519 result
= send_message_fd(conn
->fd
, req
);
526 result
= recv_message_conn(conn
, reply
);
530 else if(result
== 0) {
542 int ustcomm_connect_path(const char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
546 struct sockaddr_un addr
;
548 ustcomm_init_connection(conn
);
550 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
556 addr
.sun_family
= AF_UNIX
;
558 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
559 if(result
>= UNIX_PATH_MAX
) {
560 ERR("string overflow allocating socket name");
565 result
= signal_process(signalpid
);
567 ERR("could not signal process");
572 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
574 PERROR("connect (path=%s)", path
);
583 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
585 return close(conn
->fd
);
588 /* Open a connection to a traceable app.
595 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
598 char path
[UNIX_PATH_MAX
];
601 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
602 if(result
>= UNIX_PATH_MAX
) {
603 ERR("string overflow allocating socket name");
607 return ustcomm_connect_path(path
, conn
, pid
);
610 /* Close a connection to a traceable app. It frees the
611 * resources. It however does not free the
612 * ustcomm_connection itself.
615 int ustcomm_close_app(struct ustcomm_connection
*conn
)
618 free(conn
->recv_buf
);
623 static int ensure_dir_exists(const char *dir
)
631 result
= stat(dir
, &st
);
632 if(result
== -1 && errno
!= ENOENT
) {
635 else if(result
== -1) {
639 result
= mkdir_p(dir
, 0777);
641 ERR("executing in recursive creation of directory %s", dir
);
649 /* Called by an application to initialize its server so daemons can
653 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
658 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
659 if(result
>= UNIX_PATH_MAX
) {
660 ERR("string overflow allocating socket name");
664 result
= ensure_dir_exists(SOCK_DIR
);
666 ERR("Unable to create socket directory %s", SOCK_DIR
);
670 handle
->server
.listen_fd
= init_named_socket(name
, &(handle
->server
.socketpath
));
671 if(handle
->server
.listen_fd
< 0) {
672 ERR("Error initializing named socket (%s). Check that directory exists and that it is writable.", name
);
677 INIT_LIST_HEAD(&handle
->server
.connections
);
686 /* Used by the daemon to initialize its server so applications
690 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
, const char *sock_path
)
696 asprintf(&name
, "%s", sock_path
);
701 /* Only check if socket dir exists if we are using the default directory */
702 result
= ensure_dir_exists(SOCK_DIR
);
704 ERR("Unable to create socket directory %s", SOCK_DIR
);
708 asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
711 handle
->server
.listen_fd
= init_named_socket(name
, &handle
->server
.socketpath
);
712 if(handle
->server
.listen_fd
< 0) {
713 ERR("error initializing named socket at %s", name
);
718 INIT_LIST_HEAD(&handle
->server
.connections
);
726 static void ustcomm_fini_server(struct ustcomm_server
*server
, int keep_socket_file
)
731 if(!keep_socket_file
) {
733 result
= stat(server
->socketpath
, &st
);
735 PERROR("stat (%s)", server
->socketpath
);
739 /* Paranoid check before deleting. */
740 result
= S_ISSOCK(st
.st_mode
);
742 ERR("The socket we are about to delete is not a socket.");
746 result
= unlink(server
->socketpath
);
752 free(server
->socketpath
);
754 result
= close(server
->listen_fd
);
761 /* Free a traceable application server */
763 void ustcomm_fini_app(struct ustcomm_app
*handle
, int keep_socket_file
)
765 ustcomm_fini_server(&handle
->server
, keep_socket_file
);
768 /* Free a ustd server */
770 void ustcomm_fini_ustd(struct ustcomm_ustd
*handle
)
772 ustcomm_fini_server(&handle
->server
, 0);
775 static const char *find_tok(const char *str
)
787 static const char *find_sep(const char *str
)
799 int nth_token_is(const char *str
, const char *token
, int tok_no
)
805 for(i
=0; i
<=tok_no
; i
++) {
819 if(end
-start
!= strlen(token
))
822 if(strncmp(start
, token
, end
-start
))
828 char *nth_token(const char *str
, int tok_no
)
830 static char *retval
= NULL
;
835 for(i
=0; i
<=tok_no
; i
++) {
854 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);
859 /* Callback from multipoll.
860 * Receive a new connection on the listening socket.
863 static int process_mp_incoming_conn(void *priv
, int fd
, short events
)
865 struct ustcomm_connection
*newconn
;
866 struct ustcomm_server
*server
= (struct ustcomm_server
*) priv
;
870 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
876 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
877 if(newconn
== NULL
) {
878 ERR("malloc returned NULL");
882 ustcomm_init_connection(newconn
);
885 list_add(&newconn
->list
, &server
->connections
);
890 /* Callback from multipoll.
891 * Receive a message on an existing connection.
894 static int process_mp_conn_msg(void *priv
, int fd
, short revents
)
896 struct ustcomm_multipoll_conn_info
*mpinfo
= (struct ustcomm_multipoll_conn_info
*) priv
;
899 struct ustcomm_source src
;
904 result
= recv_message_conn(mpinfo
->conn
, &msg
);
906 ERR("error in recv_message_conn");
909 else if(result
== 0) {
910 /* connection finished */
911 ustcomm_close_app(mpinfo
->conn
);
912 list_del(&mpinfo
->conn
->list
);
916 mpinfo
->cb(msg
, &src
);
924 int free_ustcomm_client_poll(void *data
)
930 void ustcomm_mp_add_app_clients(struct mpentries
*ent
, struct ustcomm_app
*app
, int (*cb
)(char *recvbuf
, struct ustcomm_source
*src
))
932 struct ustcomm_connection
*conn
;
934 /* add listener socket */
935 multipoll_add(ent
, app
->server
.listen_fd
, POLLIN
, process_mp_incoming_conn
, &app
->server
, NULL
);
937 list_for_each_entry(conn
, &app
->server
.connections
, list
) {
938 struct ustcomm_multipoll_conn_info
*mpinfo
= (struct ustcomm_multipoll_conn_info
*) malloc(sizeof(struct ustcomm_multipoll_conn_info
));
941 multipoll_add(ent
, conn
->fd
, POLLIN
, process_mp_conn_msg
, mpinfo
, free_ustcomm_client_poll
);