+int ustcomm_send_request(struct ustcomm_connection *conn, char *req, char **reply)
+{
+ int result;
+
+ result = send(conn->fd, req, strlen(req), 0);
+ if(result == -1) {
+ PERROR("send");
+ return -1;
+ }
+ else if(result == 0) {
+ return 0;
+ }
+
+ if(!reply)
+ return 1;
+
+ *reply = (char *) malloc(MSG_MAX+1);
+ result = recv(conn->fd, *reply, MSG_MAX, 0);
+ if(result == -1) {
+ PERROR("recv");
+ return -1;
+ }
+ else if(result == 0) {
+ return 0;
+ }
+
+ (*reply)[result] = '\0';
+
+ return 1;
+}
+
+int ustcomm_connect_path(char *path, struct ustcomm_connection *conn, pid_t signalpid)
+{
+ int fd;
+ int result;
+ struct sockaddr_un addr;
+
+ result = fd = socket(PF_UNIX, SOCK_STREAM, 0);
+ if(result == -1) {
+ PERROR("socket");
+ return -1;
+ }
+
+ addr.sun_family = AF_UNIX;
+
+ result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path);
+ if(result >= UNIX_PATH_MAX) {
+ ERR("string overflow allocating socket name");
+ return -1;
+ }
+
+ if(signalpid >= 0)
+ signal_process(signalpid);
+
+ result = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
+ if(result == -1) {
+ PERROR("connect");
+ return -1;
+ }
+
+ conn->fd = fd;
+
+ return 0;
+}
+
+int ustcomm_disconnect(struct ustcomm_connection *conn)
+{
+ return close(conn->fd);
+}
+
+int ustcomm_connect_app(pid_t pid, struct ustcomm_connection *conn)
+{
+ int result;
+ char path[UNIX_PATH_MAX];
+
+
+ result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid);
+ if(result >= UNIX_PATH_MAX) {
+ fprintf(stderr, "string overflow allocating socket name");
+ return -1;
+ }
+
+ return ustcomm_connect_path(path, conn, pid);
+}
+
+int ustcomm_disconnect_app(struct ustcomm_connection *conn)
+{
+ close(conn->fd);
+ return 0;
+}
+