5 #include <sys/socket.h>
18 #define UNIX_PATH_MAX 108
19 #define SOCK_DIR "/tmp/socks"
20 #define UST_SIGNAL SIGIO
24 /* FIXME: ustcomm blocks on message sending, which might be problematic in
25 * some cases. Fix the poll() usage so sends are buffered until they don't
29 //static void bt(void)
34 // result = backtrace(&buffer, 100);
35 // backtrace_symbols_fd(buffer, result, STDERR_FILENO);
38 char *strdup_malloc(const char *s
)
45 retval
= (char *) malloc(strlen(s
)+1);
52 static void signal_process(pid_t pid
)
56 result
= kill(pid
, UST_SIGNAL
);
65 static int send_message_fd(int fd
, const char *msg
)
69 result
= send(fd
, msg
, strlen(msg
), 0);
74 else if(result
== 0) {
80 // *reply = (char *) malloc(MSG_MAX+1);
81 // result = recv(fd, *reply, MSG_MAX, 0);
86 // else if(result == 0) {
90 // (*reply)[result] = '\0';
95 static int send_message_path(const char *path
, const char *msg
, int signalpid
)
99 struct sockaddr_un addr
;
101 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
107 addr
.sun_family
= AF_UNIX
;
109 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
110 if(result
>= UNIX_PATH_MAX
) {
111 ERR("string overflow allocating socket name");
116 signal_process(signalpid
);
118 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
124 return send_message_fd(fd
, msg
);
127 ///* pid: the pid of the trace process that must receive the msg
128 // msg: pointer to a null-terminated message to send
129 // reply: location where to put the null-terminated string of the reply;
130 // it must be free'd after usage
133 //int send_message_pid(pid_t pid, const char *msg, char **reply)
136 // char path[UNIX_PATH_MAX];
138 // result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
139 // if(result >= UNIX_PATH_MAX) {
140 // fprintf(stderr, "string overflow allocating socket name");
144 // send_message_path(path, msg, reply, pid);
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 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
158 if(result
>= UNIX_PATH_MAX
) {
159 fprintf(stderr
, "string overflow allocating socket name");
163 asprintf(&msg
, "collect %d %s", pid
, channel
);
165 send_message_path(path
, msg
, -1);
171 /* returns 1 to indicate a message was received
172 * returns 0 to indicate no message was received (cannot happen)
173 * returns -1 to indicate an error
176 static int recv_message_fd(int fd
, char **msg
, struct ustcomm_source
*src
)
180 *msg
= (char *) malloc(MSG_MAX
+1);
182 result
= recv(fd
, *msg
, MSG_MAX
, 0);
188 (*msg
)[result
] = '\0';
190 DBG("ustcomm_app_recv_message: result is %d, message is %s", result
, (*msg
));
198 int ustcomm_send_reply(struct ustcomm_server
*server
, char *msg
, struct ustcomm_source
*src
)
202 result
= send_message_fd(src
->fd
, msg
);
204 ERR("error in send_message_fd");
211 /* @timeout: max blocking time in milliseconds, -1 means infinity
213 * returns 1 to indicate a message was received
214 * returns 0 to indicate no message was received
215 * returns -1 to indicate an error
218 int ustcomm_recv_message(struct ustcomm_server
*server
, char **msg
, struct ustcomm_source
*src
, int timeout
)
221 struct ustcomm_connection
*conn
;
229 list_for_each_entry(conn
, &server
->connections
, list
) {
233 fds
= (struct pollfd
*) malloc(n_fds
* sizeof(struct pollfd
));
235 ERR("malloc returned NULL");
239 /* special idx 0 is for listening socket */
240 fds
[idx
].fd
= server
->listen_fd
;
241 fds
[idx
].events
= POLLIN
;
244 list_for_each_entry(conn
, &server
->connections
, list
) {
245 fds
[idx
].fd
= conn
->fd
;
246 fds
[idx
].events
= POLLIN
;
250 result
= poll(fds
, n_fds
, timeout
);
260 struct ustcomm_connection
*newconn
;
263 result
= newfd
= accept(server
->listen_fd
, NULL
, NULL
);
269 newconn
= (struct ustcomm_connection
*) malloc(sizeof(struct ustcomm_connection
));
270 if(newconn
== NULL
) {
271 ERR("malloc returned NULL");
277 list_add(&newconn
->list
, &server
->connections
);
280 for(idx
=1; idx
<n_fds
; idx
++) {
281 if(fds
[idx
].revents
) {
282 retval
= recv_message_fd(fds
[idx
].fd
, msg
, src
);
284 /* connection finished */
287 list_for_each_entry(conn
, &server
->connections
, list
) {
288 if(conn
->fd
== fds
[idx
].fd
) {
289 list_del(&conn
->list
);
295 goto free_fds_return
;
308 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
, int timeout
)
310 return ustcomm_recv_message(&ustd
->server
, msg
, src
, timeout
);
313 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
, int timeout
)
315 return ustcomm_recv_message(&app
->server
, msg
, src
, timeout
);
318 /* This removes src from the list of active connections of app.
321 int ustcomm_app_detach_client(struct ustcomm_app
*app
, struct ustcomm_source
*src
)
323 struct ustcomm_server
*server
= (struct ustcomm_server
*)app
;
324 struct ustcomm_connection
*conn
;
326 list_for_each_entry(conn
, &server
->connections
, list
) {
327 if(conn
->fd
== src
->fd
) {
328 list_del(&conn
->list
);
338 static int init_named_socket(char *name
, char **path_out
)
343 struct sockaddr_un addr
;
345 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
351 addr
.sun_family
= AF_UNIX
;
353 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
354 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
356 result
= access(name
, F_OK
);
359 result
= unlink(name
);
361 PERROR("unlink of socket file");
364 WARN("socket already exists; overwriting");
367 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
373 result
= listen(fd
, 1);
381 *path_out
= strdupa(addr
.sun_path
);
392 int ustcomm_send_request(struct ustcomm_connection
*conn
, char *req
, char **reply
)
396 result
= send(conn
->fd
, req
, strlen(req
), 0);
401 else if(result
== 0) {
408 *reply
= (char *) malloc(MSG_MAX
+1);
409 result
= recv(conn
->fd
, *reply
, MSG_MAX
, 0);
414 else if(result
== 0) {
418 (*reply
)[result
] = '\0';
423 int ustcomm_connect_path(char *path
, struct ustcomm_connection
*conn
, pid_t signalpid
)
427 struct sockaddr_un addr
;
429 result
= fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
435 addr
.sun_family
= AF_UNIX
;
437 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
438 if(result
>= UNIX_PATH_MAX
) {
439 ERR("string overflow allocating socket name");
444 signal_process(signalpid
);
446 result
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
457 int ustcomm_disconnect(struct ustcomm_connection
*conn
)
459 return close(conn
->fd
);
462 int ustcomm_connect_app(pid_t pid
, struct ustcomm_connection
*conn
)
465 char path
[UNIX_PATH_MAX
];
468 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
469 if(result
>= UNIX_PATH_MAX
) {
470 fprintf(stderr
, "string overflow allocating socket name");
474 return ustcomm_connect_path(path
, conn
, pid
);
477 int ustcomm_disconnect_app(struct ustcomm_connection
*conn
)
483 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
488 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
489 if(result
>= UNIX_PATH_MAX
) {
490 ERR("string overflow allocating socket name");
494 handle
->server
.listen_fd
= init_named_socket(name
, &(handle
->server
.socketpath
));
495 if(handle
->server
.listen_fd
< 0) {
496 ERR("error initializing named socket");
501 INIT_LIST_HEAD(&handle
->server
.connections
);
510 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
)
515 result
= asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
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 static char *find_tok(char *str
)
549 static char *find_sep(char *str
)
561 int nth_token_is(char *str
, char *token
, int tok_no
)
567 for(i
=0; i
<=tok_no
; i
++) {
581 if(end
-start
!= strlen(token
))
584 if(strncmp(start
, token
, end
-start
))
590 char *nth_token(char *str
, int tok_no
)
592 static char *retval
= NULL
;
597 for(i
=0; i
<=tok_no
; i
++) {
616 asprintf(&retval
, "%.*s", (int)(end
-start
), start
);
This page took 0.068769 seconds and 4 git commands to generate.