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 static int mkdir_p(const char *path
, mode_t mode
)
63 tmp
= malloc(strlen(path
) + 1);
71 while (*path_p
!= '/') {
77 strncpy(tmp
, path
, path_p
- path
);
78 tmp
[path_p
-path
] = '\0';
79 if (tmp
[path_p
- path
- 1] != '/') {
80 result
= mkdir(tmp
, mode
);
82 if (!(errno
== EEXIST
|| errno
== EACCES
|| errno
== EROFS
)) {
83 /* Then this is a real error */
93 result
= mkdir(path
, mode
);
104 char *strdup_malloc(const char *s
)
111 retval
= (char *) malloc(strlen(s
)+1);
118 static int signal_process(pid_t pid
)
123 void ustcomm_init_connection(struct ustcomm_connection
*conn
)
125 conn
->recv_buf
= NULL
;
126 conn
->recv_buf_size
= 0;
127 conn
->recv_buf_alloc
= 0;
130 int pid_is_online(pid_t pid
) {
136 * @fd: file descriptor to send to
137 * @msg: a null-terminated string containing the message to send
141 * 0: connection closed
145 static int send_message_fd(int fd
, const char *msg
)
149 /* Send including the final \0 */
150 result
= patient_send(fd
, msg
, strlen(msg
)+1, MSG_NOSIGNAL
);
156 else if(result
== 0) {
160 DBG("sent message \"%s\"", msg
);
164 /* Called by an app to ask the consumer daemon to connect to it. */
166 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
168 char path
[UNIX_PATH_MAX
];
172 struct ustcomm_connection conn
;
173 char *explicit_daemon_socket_path
;
175 explicit_daemon_socket_path
= getenv("UST_DAEMON_SOCKET");
176 if(explicit_daemon_socket_path
) {
177 /* user specified explicitly a socket path */
178 result
= snprintf(path
, UNIX_PATH_MAX
, "%s", explicit_daemon_socket_path
);
181 /* just use the default path */
182 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
185 if(result
>= UNIX_PATH_MAX
) {
186 ERR("string overflow allocating socket name");
190 asprintf(&msg
, "collect %d %s", pid
, channel
);
192 /* don't signal it because it's the daemon */
193 result
= ustcomm_connect_path(path
, &conn
, -1);
195 WARN("ustcomm_connect_path failed");
200 result
= ustcomm_send_request(&conn
, msg
, NULL
);
202 WARN("ustcomm_send_request failed");
208 ustcomm_disconnect(&conn
);
215 /* returns 1 to indicate a message was received
216 * returns 0 to indicate no message was received (end of stream)
217 * returns -1 to indicate an error
220 #define RECV_INCREMENT 1000
221 #define RECV_INITIAL_BUF_SIZE 10
223 static int recv_message_fd(int fd
, char **recv_buf
, int *recv_buf_size
, int *recv_buf_alloc
, char **msg
)
227 /* 1. Check if there is a message in the buf */
229 2.1 receive chunk and put it in buffer
230 2.2 process full message if there is one
231 -- while no message arrived
238 /* Search for full message in buffer */
239 for(i
=0; i
<*recv_buf_size
; i
++) {
240 if((*recv_buf
)[i
] == '\0') {
246 /* Process found message */
252 WARN("received empty message");
254 *msg
= strndup(*recv_buf
, i
);
256 /* Remove processed message from buffer */
257 newbuf
= (char *) malloc(*recv_buf_size
- (i
+1));
258 memcpy(newbuf
, *recv_buf
+ (i
+1), *recv_buf_size
- (i
+1));
261 *recv_buf_size
-= (i
+1);
262 *recv_buf_alloc
-= (i
+1);
267 /* Receive a chunk from the fd */
268 if(*recv_buf_alloc
- *recv_buf_size
< RECV_INCREMENT
) {
269 *recv_buf_alloc
+= RECV_INCREMENT
- (*recv_buf_alloc
- *recv_buf_size
);
270 *recv_buf
= (char *) realloc(*recv_buf
, *recv_buf_alloc
);
273 result
= recv(fd
, *recv_buf
+*recv_buf_size
, RECV_INCREMENT
, 0);
275 if(errno
== ECONNRESET
) {
286 *recv_buf_size
+= result
;
288 /* Go back to the beginning to check if there is a full message in the buffer */
291 DBG("received message \"%s\"", *recv_buf
);
297 static int recv_message_conn(struct ustcomm_connection
*conn
, char **msg
)
299 return recv_message_fd(conn
->fd
, &conn
->recv_buf
, &conn
->recv_buf_size
, &conn
->recv_buf_alloc
, msg
);
302 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
306 result
= send_message_fd(src
->fd
, msg
);
308 ERR("error in send_message_fd");
315 /* Called after a fork. */
317 int ustcomm_close_all_connections(struct ustcomm_server
*server
)
319 struct ustcomm_connection
*conn
;
320 struct ustcomm_connection
*deletable_conn
= NULL
;
322 list_for_each_entry(conn
, &server
->connections
, list
) {
323 free(deletable_conn
);
324 deletable_conn
= conn
;
325 ustcomm_close_app(conn
);
326 list_del(&conn
->list
);
332 /* @timeout: max blocking time in milliseconds, -1 means infinity
334 * returns 1 to indicate a message was received
335 * returns 0 to indicate no message was received
336 * returns -1 to indicate an error
339 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
342 struct ustcomm_connection
**conn_table
;
343 struct ustcomm_connection
*conn
;
351 list_for_each_entry(conn
, &server
->connections
, list
) {
355 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
357 ERR("malloc returned NULL");
361 conn_table
= (struct ustcomm_connection
**) malloc(n_fds
* sizeof(struct ustcomm_connection
*));
362 if(conn_table
== NULL
) {
363 ERR("malloc returned NULL");
365 goto free_fds_return
;
368 /* special idx 0 is for listening socket */
369 fds
[idx
].fd
= server
->listen_fd
;
370 fds
[idx
].events
= POLLIN
;
373 list_for_each_entry(conn
, &server
->connections
, list
) {
374 fds
[idx
].fd
= conn
->fd
;
375 fds
[idx
].events
= POLLIN
;
376 conn_table
[idx
] = conn
;
380 while((result
= poll(fds
, n_fds
, timeout
)) == -1 && errno
== EINTR
)
385 goto free_conn_table_return
;
390 goto free_conn_table_return
;
394 struct ustcomm_connection
*newconn
;
397 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
401 goto free_conn_table_return
;
404 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
405 if(newconn
== NULL
) {
406 ERR("malloc returned NULL");
410 ustcomm_init_connection(newconn
);
413 list_add(&newconn
->list
, &server
->connections
);
416 for(idx
=1; idx
<n_fds
; idx
++) {
417 if(fds
[idx
].revents
) {
418 retval
= recv_message_conn(conn_table
[idx
], msg
);
420 src
->fd
= fds
[idx
].fd
;
423 /* connection finished */
426 list_for_each_entry(conn
, &server
->connections
, list
) {
427 if(conn
->fd
== fds
[idx
].fd
) {
428 list_del(&conn
->list
);
434 goto free_conn_table_return
;
443 free_conn_table_return
:
450 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
452 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
455 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
457 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
460 /* This removes src from the list of active connections of app.
463 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
465 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
466 struct ustcomm_connection
*conn
;
468 list_for_each_entry(conn
, &server
->connections
, list
) {
469 if(conn
->fd
== src
->fd
) {
470 list_del(&conn
->list
);
480 static int init_named_socket(const char *name
, char **path_out
)
485 struct sockaddr_un addr
;
487 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
493 addr
.sun_family
= AF_UNIX
;
495 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
496 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
498 result
= access(name
, F_OK
);
501 result
= unlink(name
);
503 PERROR("unlink of socket file");
506 WARN("socket already exists; overwriting");
509 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
515 result
= listen(fd
, 1);
522 *path_out
= strdup(addr
.sun_path
);
535 * 0: Success, but no reply because recv() returned 0
539 * On error, the error message is printed, except on
540 * ECONNRESET, which is normal when the application dies.
543 int ustcomm_send_request(struct ustcomm_connection
*conn
, const char *req
, char **reply
)
547 /* Send including the final \0 */
548 result
= send_message_fd(conn
->fd
, req
);
555 result
= recv_message_conn(conn
, reply
);
559 else if(result
== 0) {
571 int ustcomm_connect_path(const char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
575 struct sockaddr_un addr
;
577 ustcomm_init_connection(conn
);
579 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
585 addr
.sun_family
= AF_UNIX
;
587 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
588 if(result
>= UNIX_PATH_MAX
) {
589 ERR("string overflow allocating socket name");
594 result
= signal_process(signalpid
);
596 ERR("could not signal process");
601 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
603 PERROR("connect (path=%s)", path
);
612 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
614 return close(conn
->fd
);
617 /* Open a connection to a traceable app.
624 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
627 char path
[UNIX_PATH_MAX
];
630 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
631 if(result
>= UNIX_PATH_MAX
) {
632 ERR("string overflow allocating socket name");
636 return ustcomm_connect_path(path
, conn
, pid
);
639 /* Close a connection to a traceable app. */
641 int ustcomm_close_app(struct ustcomm_connection
*conn
)
644 free(conn
->recv_buf
);
649 static int ensure_dir_exists(const char *dir
)
657 result
= stat(dir
, &st
);
658 if(result
== -1 && errno
!= ENOENT
) {
661 else if(result
== -1) {
665 result
= mkdir_p(dir
, 0777);
667 ERR("executing in recursive creation of directory %s", dir
);
675 /* Called by an application to initialize its server so daemons can
679 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
684 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
685 if(result
>= UNIX_PATH_MAX
) {
686 ERR("string overflow allocating socket name");
690 result
= ensure_dir_exists(SOCK_DIR
);
692 ERR("Unable to create socket directory %s", SOCK_DIR
);
696 handle
->server
.listen_fd
= init_named_socket(name
, &(handle
->server
.socketpath
));
697 if(handle
->server
.listen_fd
< 0) {
698 ERR("Error initializing named socket (%s). Check that directory exists and that it is writable.", name
);
703 INIT_LIST_HEAD(&handle
->server
.connections
);
712 /* Used by the daemon to initialize its server so applications
716 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
, const char *sock_path
)
722 asprintf(&name
, "%s", sock_path
);
727 /* Only check if socket dir exists if we are using the default directory */
728 result
= ensure_dir_exists(SOCK_DIR
);
730 ERR("Unable to create socket directory %s", SOCK_DIR
);
734 asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
737 handle
->server
.listen_fd
= init_named_socket(name
, &handle
->server
.socketpath
);
738 if(handle
->server
.listen_fd
< 0) {
739 ERR("error initializing named socket at %s", name
);
744 INIT_LIST_HEAD(&handle
->server
.connections
);
752 static void ustcomm_fini_server(struct ustcomm_server
*server
, int keep_socket_file
)
757 if(!keep_socket_file
) {
759 result
= stat(server
->socketpath
, &st
);
761 PERROR("stat (%s)", server
->socketpath
);
765 /* Paranoid check before deleting. */
766 result
= S_ISSOCK(st
.st_mode
);
768 ERR("The socket we are about to delete is not a socket.");
772 result
= unlink(server
->socketpath
);
778 free(server
->socketpath
);
780 result
= close(server
->listen_fd
);
787 void ustcomm_fini_app(struct ustcomm_app
*handle
, int keep_socket_file
)
789 ustcomm_fini_server(&handle
->server
, keep_socket_file
);
792 void ustcomm_fini_ustd(struct ustcomm_ustd
*handle
)
794 ustcomm_fini_server(&handle
->server
, 0);
797 static const char *find_tok(const char *str
)
809 static const char *find_sep(const char *str
)
821 int nth_token_is(const char *str
, const char *token
, int tok_no
)
827 for(i
=0; i
<=tok_no
; i
++) {
841 if(end
-start
!= strlen(token
))
844 if(strncmp(start
, token
, end
-start
))
850 char *nth_token(const char *str
, int tok_no
)
852 static char *retval
= NULL
;
857 for(i
=0; i
<=tok_no
; i
++) {
876 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);