From 4bb364b6fa63bbf59ca5ffc55e898c45285379e4 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Wed, 13 Mar 2013 14:02:47 +0000 Subject: Clean up error reporting style. --- jaildaemon.c | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/jaildaemon.c b/jaildaemon.c index 9e2f6f7..a4e2d8d 100644 --- a/jaildaemon.c +++ b/jaildaemon.c @@ -112,26 +112,26 @@ static void fork_slave( int master_fd ) { sigemptyset(&sa.sa_mask); sa.sa_flags = SA_NOCLDWAIT; if( sigaction(SIGCHLD, &sa, NULL) == -1 ) - exerr( "when trying to enable auto reap" ); + exerr( "Error: Can not enable auto reap." ); /* Wait for command from master */ while(1) { switch( read( master_fd, g_ipc_packet, sizeof(g_ipc_packet) ) ) { case -1: - exerr( "reading commands from master's socket" ); + exerr( "Error: Can not read command from master's socket." ); case IPC_PACKETSIZE: /* Decode packet and throw a forked child */ *(pid_t*)g_ipc_packet = fork_and_jail( g_ipc_packet_int[0], g_ipc_packet + sizeof(int) ); if( write( master_fd, g_ipc_packet, sizeof(pid_t) ) != sizeof(pid_t) ) - exerr( "replying to master" ); + exerr( "Error: Can not reply to master." ); break; case 0: /* Remote end closed, bye */ exit(0); default: - exerr( "ignoring corrupt command packet" ); + exerr( "Error: Received corrupt command packet." ); break; } } @@ -141,12 +141,12 @@ static int fork_fork_slave( ) { int sockets[2]; if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) - exerr( "opening stream socket pair"); + exerr( "Error: Can not open stream socket pair." ); switch( fork() ) { case -1: pidfile_remove( g_pidfilehandle ); - exerr( "forking fork slave"); + exerr( "Error: Can not fork fork slave." ); break; case 0: /* I am child, close master's socket fd */ @@ -202,7 +202,7 @@ static pid_t fork_and_jail( int jid, char * proctitle ) { /* Throw ourself into the jail */ if( jail_attach( jid ) ) - exerr( "when attaching to jail %d", jid ); + exerr( "Error: Can not attach process to jail %d.", jid ); /* wait for SIGHUP */ sigemptyset(&sigset); @@ -334,11 +334,11 @@ static int add_task_to_kqueue( int kq, daemon_task * t_in ) { IPC_PACKETSIZE - sizeof(int) ); if( write( g_fork_slave_fd, g_ipc_packet, IPC_PACKETSIZE ) != IPC_PACKETSIZE ) - exerr( "sending task to fork slave" ); + exerr( "Error: Can not send task to fork slave." ); if( read( g_fork_slave_fd, g_ipc_packet, sizeof(pid_t) ) < (ssize_t)sizeof(pid_t) ) - exerr( "receiving pid from fork slave" ); + exerr( "Error: Can not receive pid from fork slave." ); /* Expect reply from fork slave */ pid = *(pid_t*)g_ipc_packet; @@ -405,7 +405,7 @@ int main( int argc, char **argv ) { /* If we are not started from root, there is not much we can do, neither access the unix domain socket.*/ if( getuid() != 0 ) - exerr( "when starting. Need to run as root." ); + exerr( "Error: Need to run as root." ); i=1; while(i) { @@ -430,7 +430,7 @@ int main( int argc, char **argv ) { /* Setup unix domain socket descriptors */ if( ( g_uds = socket( AF_UNIX, SOCK_DGRAM, 0 ) ) < 0 ) - exerr( "Can not create control channel." ); + exerr( "Error: Can not create control channel." ); /* Allow huge packets on our unix domain socket */ setsockopt( g_uds, SOL_SOCKET, SO_SNDBUF, &ipc_bytes, sizeof(ipc_bytes) ); @@ -458,7 +458,7 @@ int main( int argc, char **argv ) { if( text_off + 2 + o_command_len + o_proctitle_len > g_ipc_packet + IPC_PACKETSIZE ) - exerr( "Command line and proc title too long" ); + exerr( "Error: Command line and proc title are too long" ); g_ipc_packet_int[0] = o_respawn; g_ipc_packet_int[1] = o_jid; @@ -473,7 +473,8 @@ int main( int argc, char **argv ) { ipc_bytes = sendto( g_uds, g_ipc_packet, IPC_PACKETSIZE, 0, (struct sockaddr*)&addr, sizeof(addr) ); if( ipc_bytes != IPC_PACKETSIZE ) - exerr( "sending command to daemon. Maybe it is not running?" ); + exerr( "Error: Can not send command to daemon." + " Maybe it is not running?" ); exit(0); } @@ -482,7 +483,8 @@ int main( int argc, char **argv ) { if( !( g_pidfilehandle = pidfile_open(o_pidfile, 0600, &second_pid ) ) ) { if (errno == EEXIST) - exerr( "jaildaemon already running." ); + exerr( "Error: %s already running (pid %d).", argv[0], + (int)second_pid ); /* If we cannot create pidfile from other reasons, only warn. */ warn( "Cannot open or create pidfile" ); @@ -494,8 +496,9 @@ int main( int argc, char **argv ) { if( sendto( g_uds, g_ipc_packet, 0, 0, (struct sockaddr*)&addr, sizeof(addr) ) == 0 ) { if( !o_force_daemon ) - exerr( "Found command channel. Refusing to overwrite a working one." - " Another server may be running. Force with -F."); + exerr( "Error: Detected a working command channel on %s.\n" + "Refusing to overwrite a working one. Another server may" + " be running. Force with -F.", g_uds_path ); else warn( "Forcing start of daemon despite working command channel." ); } @@ -504,7 +507,7 @@ int main( int argc, char **argv ) { initialized memory yet. Communicate with this slave via socketpair */ if( daemon(1,0) == -1 ) { pidfile_remove(g_pidfilehandle); - exerr( "daemonzing" ); + exerr( "Error: Can not daemonize" ); } pidfile_write(g_pidfilehandle); @@ -527,24 +530,24 @@ int main( int argc, char **argv ) { syslog, now */ unlink(g_uds_path); if (bind(g_uds, (struct sockaddr*)&addr, sizeof(addr)) == -1) - exerr( "binding to command channel. Maybe another daemon is running?" ); + exerr( "Error: Can not create command channel." ); /* We do not care for the spawned process -- it is checked for in our kqueue filter. So just ignore SIGCHLD */ memset( &sa, 0, sizeof( sa ) ); sa.sa_flags = SA_NOCLDWAIT; if( sigaction(SIGCHLD, &sa, NULL) == -1 ) - exerr( "when trying to enable auto reap" ); + exerr( "Error: Can not enabling auto reap." ); /* When dying gracefully, this signal handler sends TERM signals to all probes */ sa.sa_handler = term_handler; if( sigaction(SIGTERM, &sa, NULL) == -1 ) - exerr( "when trying to install TERM handler" ); + exerr( "Error: Can not install TERM handler." ); /* Create our kqueue */ if( ( kq = kqueue( ) ) == -1 ) - exerr( "when create kqueue" ); + exerr( "Error: Can not create kqueue." ); /* Add our command uds to our kevent list */ memset( &ke, 0, sizeof(ke) ); @@ -565,7 +568,7 @@ int main( int argc, char **argv ) { g_probes = malloc( sizeof(pid_t) * PROBES_VECTOR_SIZE ); g_probes_size = PROBES_VECTOR_SIZE; if( !g_probes ) - exerr( "allocating memory." ); + exerr( "Error: Out of memory." ); memset( g_probes, 0, sizeof(pid_t) * PROBES_VECTOR_SIZE ); atexit( kill_all_probes ); @@ -576,7 +579,7 @@ int main( int argc, char **argv ) { case -1: if( errno == EINTR ) continue; - exerr( "when reading from kqueue" ); + exerr( "Error: Can not read from kqueue." ); case 0: continue; default: @@ -666,14 +669,14 @@ int main( int argc, char **argv ) { or that something strange is going on over there. For now we only warn, we may switch to exerr() in the future. */ if( ke.flags == EV_EOF ) - exerr( "Fork slave died. We die, too." ); + exerr( "Error: Fork slave died. We die, too." ); else warn( "Spurious packet from fork slave." ); } break; case EVFILT_VNODE: if( (int)ke.ident == g_uds && ke.fflags == NOTE_DELETE ) - exerr( "Control channel was deleted. Quitting." ); + exerr( "Error: Control channel was deleted. Quitting." ); break; default: break; -- cgit v1.2.3