improve error reporting
This commit is contained in:
@@ -12,6 +12,11 @@
|
||||
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <error.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define die(...) error(EXIT_FAILURE, errno, __VA_ARGS__)
|
||||
|
||||
// copied from OpenBSD
|
||||
// https://github.com/openbsd/src/blob/master/lib/libc/string/strlcpy.c
|
||||
|
||||
@@ -47,23 +52,20 @@ size_t strlcpy(char *dst, const char *src, size_t dsize)
|
||||
int open_socket(struct sockaddr_un *addr, struct sockaddr_un * peer_addr) {
|
||||
int fd = socket(AF_LOCAL, SOCK_DGRAM, PF_LOCAL);
|
||||
if(fd < 0) {
|
||||
perror("socket");
|
||||
exit(EXIT_FAILURE);
|
||||
die("can't create socket");
|
||||
}
|
||||
|
||||
// local address is in the abstract namespace and ok to include
|
||||
// trailing nul bytes
|
||||
if((bind(fd, (struct sockaddr *) addr, sizeof (struct sockaddr_un))) < 0) {
|
||||
perror("bind");
|
||||
exit(EXIT_FAILURE);
|
||||
die("can't bind socket local address");
|
||||
}
|
||||
|
||||
// peer address is in the actual filesystem, so we need to pass
|
||||
// the real length
|
||||
int namelen = sizeof peer_addr->sun_family + strlen(peer_addr->sun_path);
|
||||
if((connect(fd, (struct sockaddr *) peer_addr, namelen)) < 0) {
|
||||
perror("connect");
|
||||
exit(EXIT_FAILURE);
|
||||
die("can't connect socket to \"%s\"", peer_addr->sun_path);
|
||||
}
|
||||
|
||||
return fd;
|
||||
@@ -76,8 +78,7 @@ int encode_args(char * buf, int len, int argc, char ** argv) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
p += strlcpy(p, argv[i], buf_end - p);
|
||||
if(p >= buf_end) {
|
||||
fprintf(stderr, "comand line too long\n");
|
||||
exit(1);
|
||||
error(1, 0, "comand line too long");
|
||||
}
|
||||
p++;
|
||||
}
|
||||
@@ -147,10 +148,8 @@ int main(int argc, char **argv)
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(myfds));
|
||||
memcpy(CMSG_DATA(cmsg), myfds, sizeof(myfds));
|
||||
|
||||
int bytes = sendmsg(socket, &msg, 0);
|
||||
if(bytes <= 0) {
|
||||
perror("sendmsg");
|
||||
exit(1);
|
||||
if(sendmsg(socket, &msg, 0) <= 0) {
|
||||
die("sendmsg failed");
|
||||
}
|
||||
|
||||
uint32_t exitstatus = 0;
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
#include <signal.h>
|
||||
@@ -25,22 +27,20 @@ static void handle_sigchld(int num) {
|
||||
}
|
||||
}
|
||||
|
||||
#define die(...) error(EXIT_FAILURE, errno, __VA_ARGS__)
|
||||
|
||||
int open_socket(char * pathname) {
|
||||
|
||||
struct sockaddr_un addr;
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, pathname, sizeof(addr.sun_path) - 1);
|
||||
|
||||
int fd = socket(AF_LOCAL, SOCK_DGRAM, PF_LOCAL);
|
||||
if(fd < 0) {
|
||||
perror("socket");
|
||||
exit(EXIT_FAILURE);
|
||||
die("can't create socket \"%s\"", pathname);
|
||||
}
|
||||
|
||||
if((bind(fd, (struct sockaddr *) &addr, sizeof addr)) < 0) {
|
||||
perror("bind");
|
||||
exit(EXIT_FAILURE);
|
||||
die("can't bind socket \"%s\"", pathname);
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
@@ -103,8 +103,7 @@ pid_t fork_lua(lua_State *L,
|
||||
free(command_line);
|
||||
return pid;
|
||||
} else {
|
||||
perror("fork");
|
||||
return -1;
|
||||
die("fork failed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,8 +148,7 @@ int main(int argc, char **argv)
|
||||
peer_table[500].pid = -1;
|
||||
|
||||
if(pipe(sigpipe_fds) < 0) {
|
||||
perror("pipe");
|
||||
exit(EXIT_FAILURE);
|
||||
die("can't create pipe");
|
||||
}
|
||||
sigchild_fd = sigpipe_fds[1];
|
||||
|
||||
@@ -159,8 +157,7 @@ int main(int argc, char **argv)
|
||||
if (argc >= 2) {
|
||||
status = luaL_dofile(L, argv[1]);
|
||||
if (status) {
|
||||
fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
|
||||
return 1;
|
||||
error(1, 0, "Error: %s", lua_tostring(L, -1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user