Browse Source

Unix message failures are almost always fatal so add debugging about where the functions were called from

master
Con Kolivas 11 years ago
parent
commit
ed8f73a426
  1. 43
      src/libckpool.c
  2. 18
      src/libckpool.h

43
src/libckpool.c

@ -564,7 +564,7 @@ void close_unix_socket(const int sockd, const char *server_path)
LOGERR("Failed to unlink %s", server_path);
}
int open_unix_server(const char *server_path)
int _open_unix_server(const char *server_path, const char *file, const char *func, const int line)
{
struct sockaddr_un serveraddr;
int sockd = -1, len, ret;
@ -623,10 +623,12 @@ int open_unix_server(const char *server_path)
LOGDEBUG("Opened server path %s successfully on socket %d", server_path, sockd);
out:
if (unlikely(sockd == -1))
LOGERR("Failure in open_unix_server from %s %s:%d", file, func, line);
return sockd;
}
int open_unix_client(const char *server_path)
int _open_unix_client(const char *server_path, const char *file, const char *func, const int line)
{
struct sockaddr_un serveraddr;
int sockd = -1, len, ret;
@ -658,7 +660,11 @@ int open_unix_client(const char *server_path)
sockd = -1;
goto out;
}
LOGDEBUG("Opened client path %s successfully on socket %d", server_path, sockd);
out:
if (unlikely(sockd == -1))
LOGERR("Failure in open_unix_client from %s %s:%d", file, func, line);
return sockd;
}
@ -701,7 +707,7 @@ int read_length(int sockd, void *buf, int len)
/* Use a standard message across the unix sockets:
* 4 byte length of message as little endian encoded uint32_t followed by the
* string. Return NULL in case of failure. */
char *recv_unix_msg(int sockd)
char *_recv_unix_msg(int sockd, const char *file, const char *func, const int line)
{
char *buf = NULL;
uint32_t msglen;
@ -736,6 +742,8 @@ char *recv_unix_msg(int sockd)
dealloc(buf);
}
out:
if (unlikely(!buf))
LOGERR("Failure in recv_unix_msg from %s %s:%d", file, func, line);
return buf;
}
@ -775,47 +783,52 @@ int write_length(int sockd, const void *buf, int len)
return ofs;
}
bool send_unix_msg(int sockd, const char *buf)
bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *func, const int line)
{
uint32_t msglen, len;
bool retval = false;
int ret;
if (unlikely(!buf)) {
LOGWARNING("Null message sent to send_unix_msg");
return false;
goto out;
}
len = strlen(buf);
if (unlikely(!len)) {
LOGWARNING("Zero length message sent to send_unix_msg");
return false;
goto out;
}
msglen = htole32(len);
ret = wait_write_select(sockd, 60);
if (unlikely(ret < 1)) {
LOGERR("Select1 failed in send_unix_msg");
return false;
goto out;
}
ret = write_length(sockd, &msglen, 4);
if (unlikely(ret < 4)) {
LOGERR("Failed to write 4 byte length in send_unix_msg");
return false;
goto out;
}
ret = wait_write_select(sockd, 60);
if (unlikely(ret < 1)) {
LOGERR("Select2 failed in send_unix_msg");
return false;
goto out;
}
ret = write_length(sockd, buf, len);
if (unlikely(ret < 0)) {
LOGERR("Failed to write %d bytes in send_unix_msg", len);
return false;
goto out;
}
return true;
retval = true;
out:
if (unlikely(!retval))
LOGERR("Failure in send_unix_msg from %s %s:%d", file, func, line);
return retval;
}
/* Send a single message to a process instance when there will be no response,
* closing the socket immediately. */
bool send_proc(proc_instance_t *pi, const char *msg)
bool _send_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line)
{
char *path = pi->us.path;
bool ret = false;
@ -840,12 +853,14 @@ bool send_proc(proc_instance_t *pi, const char *msg)
ret = true;
close(sockd);
out:
if (unlikely(!ret))
LOGERR("Failure in send_proc from %s %s:%d", file, func, line);
return ret;
}
/* Send a single message to a process instance and retrieve the response, then
* close the socket. */
char *send_recv_proc(proc_instance_t *pi, const char *msg)
char *_send_recv_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line)
{
char *path = pi->us.path, *buf = NULL;
int sockd;
@ -869,6 +884,8 @@ char *send_recv_proc(proc_instance_t *pi, const char *msg)
buf = recv_unix_msg(sockd);
close(sockd);
out:
if (unlikely(!buf))
LOGERR("Failure in send_recv_proc from %s %s:%d", file, func, line);
return buf;
}

18
src/libckpool.h

@ -315,16 +315,22 @@ int write_socket(int fd, const void *buf, size_t nbyte);
int read_socket_line(connsock_t *cs, int timeout);
void empty_socket(int fd);
void close_unix_socket(const int sockd, const char *server_path);
int open_unix_server(const char *server_path);
int open_unix_client(const char *server_path);
int _open_unix_server(const char *server_path, const char *file, const char *func, const int line);
#define open_unix_server(server_path) _open_unix_server(server_path, __FILE__, __func__, __LINE__)
int _open_unix_client(const char *server_path, const char *file, const char *func, const int line);
#define open_unix_client(server_path) _open_unix_client(server_path, __FILE__, __func__, __LINE__)
int wait_read_select(int sockd, int timeout);
int read_length(int sockd, void *buf, int len);
char *recv_unix_msg(int sockd);
char *_recv_unix_msg(int sockd, const char *file, const char *func, const int line);
#define recv_unix_msg(sockd) _recv_unix_msg(sockd, __FILE__, __func__, __LINE__)
int wait_write_select(int sockd, int timeout);
int write_length(int sockd, const void *buf, int len);
bool send_unix_msg(int sockd, const char *buf);
bool send_proc(proc_instance_t *pi, const char *msg);
char *send_recv_proc(proc_instance_t *pi, const char *msg);
bool _send_unix_msg(int sockd, const char *buf, const char *file, const char *func, const int line);
#define send_unix_msg(sockd, buf) _send_unix_msg(sockd, buf, __FILE__, __func__, __LINE__)
bool _send_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line);
#define send_proc(pi, msg) _send_proc(pi, msg, __FILE__, __func__, __LINE__)
char *_send_recv_proc(proc_instance_t *pi, const char *msg, const char *file, const char *func, const int line);
#define send_recv_proc(pi, msg) _send_recv_proc(pi, msg, __FILE__, __func__, __LINE__)
const char *__json_array_string(json_t *val, unsigned int entry);
char *json_array_string(json_t *val, unsigned int entry);

Loading…
Cancel
Save