c944214ae2ea93aec91febf836e6b8018bc873d5
5 #include <sys/socket.h>
17 #define UNIX_PATH_MAX 108
18 #define SOCK_DIR "/tmp/socks"
19 #define UST_SIGNAL SIGIO
23 //static void bt(void)
28 // result = backtrace(&buffer, 100);
29 // backtrace_symbols_fd(buffer, result, STDERR_FILENO);
32 static void signal_process(pid_t pid
)
36 result
= kill(pid
, UST_SIGNAL
);
45 int send_message_path(const char *path
, const char *msg
, char **reply
, int signalpid
)
49 struct sockaddr_un addr
;
51 result
= fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
57 addr
.sun_family
= AF_UNIX
;
59 result
= snprintf(addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
60 if(result
>= UNIX_PATH_MAX
) {
61 ERR("string overflow allocating socket name");
66 signal_process(signalpid
);
68 result
= sendto(fd
, msg
, strlen(msg
), 0, (struct sockaddr
*)&addr
, sizeof(addr
));
77 *reply
= (char *) malloc(MSG_MAX
+1);
78 result
= recvfrom(fd
, *reply
, MSG_MAX
, 0, NULL
, NULL
);
84 (*reply
)[result
] = '\0';
89 /* pid: the pid of the trace process that must receive the msg
90 msg: pointer to a null-terminated message to send
91 reply: location where to put the null-terminated string of the reply;
92 it must be free'd after usage
95 int send_message(pid_t pid
, const char *msg
, char **reply
)
98 char path
[UNIX_PATH_MAX
];
100 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/%d", SOCK_DIR
, pid
);
101 if(result
>= UNIX_PATH_MAX
) {
102 fprintf(stderr
, "string overflow allocating socket name");
106 send_message_path(path
, msg
, reply
, pid
);
111 /* Called by an app to ask the consumer daemon to connect to it. */
113 int ustcomm_request_consumer(pid_t pid
, const char *channel
)
115 char path
[UNIX_PATH_MAX
];
119 result
= snprintf(path
, UNIX_PATH_MAX
, "%s/ustd", SOCK_DIR
);
120 if(result
>= UNIX_PATH_MAX
) {
121 fprintf(stderr
, "string overflow allocating socket name");
125 asprintf(&msg
, "collect %d %s", pid
, channel
);
127 send_message_path(path
, msg
, NULL
, -1);
133 static int recv_message_fd(int fd
, char **msg
, struct ustcomm_source
*src
)
136 size_t initial_addrlen
,addrlen
;
138 *msg
= (char *) malloc(MSG_MAX
+1);
141 initial_addrlen
= addrlen
= sizeof(src
->addr
);
143 result
= recvfrom(fd
, *msg
, MSG_MAX
, 0, &src
->addr
, &addrlen
);
144 if(initial_addrlen
!= addrlen
) {
145 ERR("recvfrom: unexpected address length");
150 result
= recvfrom(fd
, *msg
, MSG_MAX
, 0, NULL
, NULL
);
158 (*msg
)[result
] = '\0';
160 DBG("ustcomm_app_recv_message: result is %d, message is %s", result
, (*msg
));
165 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
, struct ustcomm_source
*src
)
167 return recv_message_fd(ustd
->fd
, msg
, src
);
170 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
, struct ustcomm_source
*src
)
172 return recv_message_fd(app
->fd
, msg
, src
);
175 static int init_named_socket(char *name
, char **path_out
)
180 struct sockaddr_un addr
;
182 result
= fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
188 addr
.sun_family
= AF_UNIX
;
190 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
191 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
193 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
201 *path_out
= strdupa(addr
.sun_path
);
212 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
217 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
218 if(result
>= UNIX_PATH_MAX
) {
219 ERR("string overflow allocating socket name");
223 handle
->fd
= init_named_socket(name
, &(handle
->socketpath
));
236 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
)
241 result
= asprintf(&name
, "%s/%s", SOCK_DIR
, "ustd");
242 if(result
>= UNIX_PATH_MAX
) {
243 ERR("string overflow allocating socket name");
247 handle
->fd
= init_named_socket(name
, &handle
->socketpath
);
255 char *find_tok(const char *str
)
267 static char *find_sep(char *str
)
279 int nth_token_is(char *str
, char *token
, int tok_no
)
285 for(i
=0; i
<=tok_no
; i
++) {
299 if(end
-start
!= strlen(token
))
302 if(strncmp(start
, token
, end
-start
))
308 char *nth_token(char *str
, int tok_no
)
310 static char *retval
= NULL
;
315 for(i
=0; i
<=tok_no
; i
++) {
334 retval
= strndupa(start
, end
-start
);
This page took 0.04974 seconds and 3 git commands to generate.