0275a736b99a22b7f4b82304f5c289ca2544446d
5 #include <sys/socket.h>
17 #define UNIX_PATH_MAX 108
18 #define SOCK_DIR "/tmp/socks"
19 #define UST_SIGNAL SIGIO
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
, pid
);
133 static int recv_message_fd(int fd
, char **msg
)
136 struct sockaddr_un addr
;
138 *msg
= (char *) malloc(MSG_MAX
+1);
139 result
= recvfrom(fd
, *msg
, MSG_MAX
, 0, NULL
, NULL
);
145 (*msg
)[result
] = '\0';
147 DBG("ustcomm_app_recv_message: result is %d, message is %s", result
, (*msg
));
154 int ustcomm_ustd_recv_message(struct ustcomm_ustd
*ustd
, char **msg
)
156 return recv_message_fd(ustd
->fd
, msg
);
159 int ustcomm_app_recv_message(struct ustcomm_app
*app
, char **msg
)
161 return recv_message_fd(app
->fd
, msg
);
164 static int init_named_socket(char *name
, char **path_out
)
169 struct sockaddr_un addr
;
171 result
= fd
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
177 addr
.sun_family
= AF_UNIX
;
179 strncpy(addr
.sun_path
, name
, UNIX_PATH_MAX
);
180 addr
.sun_path
[UNIX_PATH_MAX
-1] = '\0';
182 result
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
190 *path_out
= strdupa(addr
.sun_path
);
201 int ustcomm_init_app(pid_t pid
, struct ustcomm_app
*handle
)
206 result
= asprintf(&name
, "%s/%d", SOCK_DIR
, (int)pid
);
207 if(result
>= UNIX_PATH_MAX
) {
208 ERR("string overflow allocating socket name");
212 handle
->fd
= init_named_socket(name
, &(handle
->socketpath
));
225 int ustcomm_init_ustd(struct ustcomm_ustd
*handle
)
227 handle
->fd
= init_named_socket("ustd", &handle
->socketpath
);
This page took 0.043279 seconds and 3 git commands to generate.