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>
35 #define UNIX_PATH_MAX 108
36 #define SOCK_DIR "/tmp/socks"
37 #define UST_SIGNAL SIGIO
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
)
73 result
= kill(pid
, UST_SIGNAL
);
79 /* FIXME: should wait in a better way */
85 static int send_message_fd(int fd
, const char *msg
)
89 result
= send(fd
, msg
, strlen(msg
), 0);
94 else if(result
== 0) {
100 // *reply = (char *) malloc(MSG_MAX+1);
101 // result = recv(fd, *reply, MSG_MAX, 0);
102 // if(result == -1) {
106 // else if(result == 0) {
110 // (*reply)[result] = '\0';
115 static int send_message_path(const char *path
, const char *msg
, int signalpid
)
119 struct sockaddr_un addr
;
121 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
127 addr
.sun_family
= AF_UNIX
;
129 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
130 if(result
>= UNIX_PATH_MAX
) {
131 ERR("string overflow allocating socket name");
136 result
= signal_process(signalpid
);
138 ERR("could not signal process");
143 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
149 return send_message_fd(fd
, msg
);
152 ///* pid: the pid of the trace process that must receive the msg
153 // msg: pointer to a null-terminated message to send
154 // reply: location where to put the null-terminated string of the reply;
155 // it must be free'd after usage
158 //int send_message_pid(pid_t pid, const char *msg, char **reply)
161 // char path[UNIX_PATH_MAX];
163 // result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
164 // if(result >= UNIX_PATH_MAX) {
165 // fprintf(stderr, "string overflow allocating socket name");
169 // send_message_path(path, msg, reply, pid);
174 /* Called by an app to ask the consumer daemon to connect to it. */
176 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
178 char path
[UNIX_PATH_MAX
];
182 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
183 if(result
>= UNIX_PATH_MAX
) {
184 fprintf(stderr
, "string overflow allocating socket name");
188 asprintf(&msg
, "collect %d %s", pid
, channel
);
190 send_message_path(path
, msg
, -1);
196 /* returns 1 to indicate a message was received
197 * returns 0 to indicate no message was received (cannot happen)
198 * returns -1 to indicate an error
201 static int recv_message_fd(int fd
, char **msg
, struct ustcomm_source
*src
)
205 *msg
= (char *) malloc(MSG_MAX
+1);
207 result
= recv(fd
, *msg
, MSG_MAX
, 0);
213 (*msg
)[result
] = '\0';
215 DBG("ustcomm_app_recv_message: result is %d, message is %s", result
, (*msg
));
223 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
227 result
= send_message_fd(src
->fd
, msg
);
229 ERR("error in send_message_fd");
236 /* @timeout: max blocking time in milliseconds, -1 means infinity
238 * returns 1 to indicate a message was received
239 * returns 0 to indicate no message was received
240 * returns -1 to indicate an error
243 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
246 struct ustcomm_connection
*conn
;
254 list_for_each_entry(conn
, &server
->connections
, list
) {
258 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
260 ERR("malloc returned NULL");
264 /* special idx 0 is for listening socket */
265 fds
[idx
].fd
= server
->listen_fd
;
266 fds
[idx
].events
= POLLIN
;
269 list_for_each_entry(conn
, &server
->connections
, list
) {
270 fds
[idx
].fd
= conn
->fd
;
271 fds
[idx
].events
= POLLIN
;
275 result
= poll(fds
, n_fds
, timeout
);
285 struct ustcomm_connection
*newconn
;
288 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
294 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
295 if(newconn
== NULL
) {
296 ERR("malloc returned NULL");
302 list_add(&newconn
->list
, &server
->connections
);
305 for(idx
=1; idx
<n_fds
; idx
++) {
306 if(fds
[idx
].revents
) {
307 retval
= recv_message_fd(fds
[idx
].fd
, msg
, src
);
309 /* connection finished */
312 list_for_each_entry(conn
, &server
->connections
, list
) {
313 if(conn
->fd
== fds
[idx
].fd
) {
314 list_del(&conn
->list
);
320 goto free_fds_return
;
333 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
335 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
338 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
340 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
343 /* This removes src from the list of active connections of app.
346 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
348 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
349 struct ustcomm_connection
*conn
;
351 list_for_each_entry(conn
, &server
->connections
, list
) {
352 if(conn
->fd
== src
->fd
) {
353 list_del(&conn
->list
);
363 static int init_named_socket(char *name
, char **path_out
)
368 struct sockaddr_un addr
;
370 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
376 addr
.sun_family
= AF_UNIX
;
378 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
379 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
381 result
= access(name
, F_OK
);
384 result
= unlink(name
);
386 PERROR("unlink of socket file");
389 WARN("socket already exists; overwriting");
392 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
398 result
= listen(fd
, 1);
406 *path_out
= strdupa(addr
.sun_path
);
417 int ustcomm_send_request(struct ustcomm_connection
*conn
, char *req
, char **reply
)
421 result
= send(conn
->fd
, req
, strlen(req
), 0);
430 *reply
= (char *) malloc(MSG_MAX
+1);
431 result
= recv(conn
->fd
, *reply
, MSG_MAX
, 0);
436 else if(result
== 0) {
440 (*reply
)[result
] = '\0';
445 int ustcomm_connect_path(char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
449 struct sockaddr_un addr
;
451 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
457 addr
.sun_family
= AF_UNIX
;
459 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
460 if(result
>= UNIX_PATH_MAX
) {
461 ERR("string overflow allocating socket name");
466 result
= signal_process(signalpid
);
468 ERR("could not signal process");
473 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
484 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
486 return close(conn
->fd
);
489 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
492 char path
[UNIX_PATH_MAX
];
495 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
496 if(result
>= UNIX_PATH_MAX
) {
497 fprintf(stderr
, "string overflow allocating socket name");
501 return ustcomm_connect_path(path
, conn
, pid
);
504 int ustcomm_disconnect_app(struct ustcomm_connection
*conn
)
510 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
515 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
516 if(result
>= UNIX_PATH_MAX
) {
517 ERR("string overflow allocating socket name");
521 handle
->server
.listen_fd
= init_named_socket(name
, &(handle
->server
.socketpath
));
522 if(handle
->server
.listen_fd
< 0) {
523 ERR("error initializing named socket");
528 INIT_LIST_HEAD(&handle
->server
.connections
);
537 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
)
542 result
= asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
543 if(result
>= UNIX_PATH_MAX
) {
544 ERR("string overflow allocating socket name");
548 handle
->server
.listen_fd
= init_named_socket(name
, &handle
->server
.socketpath
);
549 if(handle
->server
.listen_fd
< 0) {
550 ERR("error initializing named socket at %s", name
);
555 INIT_LIST_HEAD(&handle
->server
.connections
);
564 static char *find_tok(char *str
)
576 static char *find_sep(char *str
)
588 int nth_token_is(char *str
, char *token
, int tok_no
)
594 for(i
=0; i
<=tok_no
; i
++) {
608 if(end
-start
!= strlen(token
))
611 if(strncmp(start
, token
, end
-start
))
617 char *nth_token(char *str
, int tok_no
)
619 static char *retval
= NULL
;
624 for(i
=0; i
<=tok_no
; i
++) {
643 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);
This page took 0.045604 seconds and 5 git commands to generate.