From 412c3be4eff4b33a4599dfb3c8d0fa36b2ca135b Mon Sep 17 00:00:00 2001 From: Ben Kibbey Date: Sat, 20 Jan 2007 10:33:56 -0500 Subject: [PATCH] Close file descriptors 0, 1 and 2 before the daemon loop. Reset signals SIGINT, SIGCHLD and SIGHUP to their defaults in the child process. Fixed log_write() and writing to stdout/stderr when daemonized. libxml and other noteworthy errors are written to the log. --- src/commands.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/pwmd.c | 27 ++++++++------- 2 files changed, 113 insertions(+), 16 deletions(-) diff --git a/src/commands.c b/src/commands.c index 9cde55b4..9d28ce19 100644 --- a/src/commands.c +++ b/src/commands.c @@ -62,11 +62,17 @@ gboolean decrypt_xml(gcry_cipher_hd_t gh, void *outbuf, gsize outsize, static gboolean parse_xml(struct client_s *client) { + xmlErrorPtr xml_error; + switch (open_xml(client->xml, client->len, &client->doc, &client->reader)) { case 1: + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; case 2: + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; default: @@ -205,6 +211,7 @@ gboolean open_command(struct client_s *client, gchar **req) if (stat(filename, &st) == 0) { if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) { + log_write("%s: %s", filename, pwmd_strerror(EPWMD_NOT_A_FILE)); send_to_client(client, "ERR %03i %s\n", EPWMD_NOT_A_FILE, pwmd_strerror(EPWMD_NOT_A_FILE)); return FALSE; } @@ -219,12 +226,14 @@ gboolean open_command(struct client_s *client, gchar **req) */ if (access(filename, R_OK|W_OK) != 0) { if (errno != ENOENT) { + log_write("%s: %s", filename, strerror(errno)); send_to_client(client, "ERR %03i %s: %s\n", EPWMD_ERROR, filename, strerror(errno)); return FALSE; } new_doc: if ((client->xml = new_document()) == NULL) { + log_write("%s", strerror(errno)); send_to_client(client, "ERR %03i malloc(): %s\n", EPWMD_ERROR, strerror(errno)); return FALSE; @@ -233,6 +242,7 @@ new_doc: client->len = strlen(client->xml); if (cache_add_file(client->md5file, NULL) == FALSE) { + log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(EPWMD_MAX_SLOTS)); send_error(client, EPWMD_MAX_SLOTS); return FALSE; } @@ -242,6 +252,7 @@ new_doc: } if ((fd = open_file(filename, &st)) == -1) { + log_write("%s: %s", filename, strerror(errno)); send_to_client(client, "ERR %03i %s: %s\n", EPWMD_ERROR, filename, strerror(errno)); return FALSE; } @@ -299,6 +310,7 @@ again: memset(shakey, 0, sizeof(shakey)); gcry_free(inbuf); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(gcryerrno)); send_to_client(client, "ERR %03i gcrypt: %s\n", EPWMD_ERROR, gcry_strerror(gcryerrno)); return FALSE; } @@ -334,6 +346,7 @@ again: memset(tkey, 0, sizeof(tkey)); gcry_free(inbuf); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(gcryerrno)); send_to_client(client, "ERR %03i gcrypt: %s\n", EPWMD_ERROR, gcry_strerror(gcryerrno)); return FALSE; } @@ -351,6 +364,7 @@ again: if (!cached) { if (cache_add_file(client->md5file, shakey) == FALSE) { memset(shakey, 0, sizeof(shakey)); + log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(EPWMD_MAX_SLOTS)); send_error(client, EPWMD_MAX_SLOTS); return FALSE; } @@ -436,6 +450,7 @@ gboolean do_xml_encrypt(struct client_s *client, gcry_cipher_hd_t gh, if (encrypt_xml(gh, inbuf, insize, NULL, 0) == FALSE) { gcry_free(inbuf); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(gcryerrno)); send_to_client(client, "ERR %03i gcrypt: %s\n", EPWMD_ERROR, gcry_strerror(gcryerrno)); return FALSE; } @@ -459,6 +474,7 @@ gboolean do_xml_encrypt(struct client_s *client, gcry_cipher_hd_t gh, if (encrypt_xml(gh, inbuf, insize, NULL, 0) == FALSE) { gcry_free(inbuf); + log_write("%s(%i): %s", __FUNCTION__, __LINE__, gcry_strerror(gcryerrno)); send_to_client(client, "ERR %03i gcrypt: %s\n", EPWMD_ERROR, gcry_strerror(gcryerrno)); return FALSE; } @@ -466,6 +482,7 @@ gboolean do_xml_encrypt(struct client_s *client, gcry_cipher_hd_t gh, if (filename) { if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0600)) == -1) { gcry_free(inbuf); + log_write("%s: %s", filename, strerror(errno)); send_to_client(client, "ERR %03i %s: %s\n", EPWMD_ERROR, filename, strerror(errno)); return FALSE; } @@ -496,6 +513,7 @@ gboolean save_command(struct client_s *client, const gchar *filename, gchar *key if (stat(filename, &st) == 0 && client->mtime) { if (client->mtime != st.st_mtime) { + log_write("%s: %s", filename, pwmd_strerror(EPWMD_FILE_MODIFIED)); send_error(client, EPWMD_FILE_MODIFIED); return FALSE; } @@ -534,6 +552,7 @@ gboolean save_command(struct client_s *client, const gchar *filename, gchar *key if (cache_update_key(client->md5file, shakey) == FALSE) { memset(shakey, 0, sizeof(shakey)); + log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(EPWMD_MAX_SLOTS)); send_error(client, EPWMD_MAX_SLOTS); return FALSE; } @@ -648,8 +667,11 @@ gboolean delete_command(struct client_s *client, gchar **req) { gint i = 1; xmlNodePtr n; + xmlErrorPtr xml_error; if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; } @@ -689,8 +711,12 @@ gboolean delete_command(struct client_s *client, gchar **req) gboolean store_command(struct client_s *client, gchar **req) { + xmlErrorPtr xml_error; + again: if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; } @@ -726,10 +752,15 @@ static gboolean do_get_command(struct client_s *client, xmlTextReaderPtr *reader gchar **nreq; gboolean ret; xmlChar *p; + xmlErrorPtr xml_error; if (reset_reader(client->doc, reader) == FALSE) { - if (!quiet) + if (!quiet) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); + } + return FALSE; } @@ -748,8 +779,12 @@ static gboolean do_get_command(struct client_s *client, xmlTextReaderPtr *reader } if ((n = xmlTextReaderCurrentNode(*reader)) == NULL) { - if (!quiet) + if (!quiet) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); + } + return FALSE; } @@ -790,8 +825,12 @@ static gboolean do_get_command(struct client_s *client, xmlTextReaderPtr *reader switch (xmlTextReaderNext(*reader)) { case -1: - if (!quiet) + if (!quiet) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); + } + return FALSE; case 0: if (!quiet) @@ -810,8 +849,11 @@ static gboolean do_get_command(struct client_s *client, xmlTextReaderPtr *reader case XML_READER_TYPE_TEXT: break; case -1: - if (!quiet) + if (!quiet) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); + } return FALSE; default: @@ -923,8 +965,11 @@ gboolean list_command(struct client_s *client, gchar *str) gboolean ret; gint type; gchar *line; + xmlErrorPtr xml_error; if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; } @@ -977,6 +1022,8 @@ list_only: } if ((n = xmlTextReaderCurrentNode(client->reader)) == NULL) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; } @@ -1006,6 +1053,8 @@ list_only: while (xmlTextReaderNext(client->reader) == 1) { again: if ((n = xmlTextReaderCurrentNode(client->reader)) == NULL) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; } @@ -1124,8 +1173,11 @@ static gboolean add_attribute(struct client_s *client, const gchar *name, const { xmlAttrPtr a; xmlNodePtr n; + xmlErrorPtr xml_error; if ((n = xmlTextReaderCurrentNode(client->reader)) == NULL) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; } @@ -1149,6 +1201,7 @@ static gboolean attribute_list(struct client_s *client, gchar **req) xmlAttrPtr a; xmlNodePtr n, an; gchar *line; + xmlErrorPtr xml_error; if (!req || !req[0]) { send_error(client, EPWMD_COMMAND_SYNTAX); @@ -1166,6 +1219,8 @@ static gboolean attribute_list(struct client_s *client, gchar **req) } if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); goto blah; } @@ -1181,12 +1236,15 @@ static gboolean attribute_list(struct client_s *client, gchar **req) } if ((n = xmlTextReaderCurrentNode(client->reader)) == NULL) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); goto blah; } for (a = n->properties; a; a = a->next) { if ((attrlist = g_realloc(attrlist, (i + 2) * sizeof(gchar *))) == NULL) { + log_write("%s(%i): g_realloc() failed", __FILE__, __LINE__); send_error(client, EPWMD_ERROR); goto blah; } @@ -1221,6 +1279,7 @@ static gboolean attribute_delete(struct client_s *client, gchar **req) xmlAttrPtr a; xmlNodePtr n; gchar **epath = NULL; + xmlErrorPtr xml_error; if (!req || !req[0] || !req[1]) { send_error(client, EPWMD_COMMAND_SYNTAX); @@ -1246,6 +1305,8 @@ static gboolean attribute_delete(struct client_s *client, gchar **req) } if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); goto blah; } @@ -1261,6 +1322,8 @@ static gboolean attribute_delete(struct client_s *client, gchar **req) } if ((n = xmlTextReaderCurrentNode(client->reader)) == NULL) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); goto blah; } @@ -1271,6 +1334,8 @@ static gboolean attribute_delete(struct client_s *client, gchar **req) } if (xmlRemoveProp(a) == -1) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); goto blah; } @@ -1289,6 +1354,7 @@ blah: static gboolean target_attribute(struct client_s *client, gchar **req) { gchar **src, **dst, *line; + xmlErrorPtr xml_error; if (!req || !req[0] || !req[1]) { send_error(client, EPWMD_COMMAND_SYNTAX); @@ -1328,6 +1394,8 @@ static gboolean target_attribute(struct client_s *client, gchar **req) } if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); g_strfreev(src); g_strfreev(dst); @@ -1353,6 +1421,8 @@ static gboolean target_attribute(struct client_s *client, gchar **req) } if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); g_strfreev(src); g_strfreev(dst); @@ -1364,6 +1434,8 @@ static gboolean target_attribute(struct client_s *client, gchar **req) */ if (find_account(client->reader, src[0]) == FALSE) { if (new_account(client->doc, src[0]) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); g_strfreev(src); g_strfreev(dst); @@ -1371,6 +1443,8 @@ static gboolean target_attribute(struct client_s *client, gchar **req) } if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); g_strfreev(src); g_strfreev(dst); @@ -1388,6 +1462,8 @@ static gboolean target_attribute(struct client_s *client, gchar **req) if (src[1]) { if (find_elements(client, client->reader, src+1, 1) == FALSE) { if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); g_strfreev(src); g_strfreev(dst); @@ -1410,6 +1486,8 @@ static gboolean target_attribute(struct client_s *client, gchar **req) } if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); g_strfreev(src); g_strfreev(dst); @@ -1456,7 +1534,11 @@ blah: */ static gboolean name_attribute(struct client_s *client, gchar **req) { + xmlErrorPtr xml_error; + if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; } @@ -1470,6 +1552,8 @@ static gboolean name_attribute(struct client_s *client, gchar **req) return TRUE; if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; } @@ -1483,6 +1567,8 @@ static gboolean name_attribute(struct client_s *client, gchar **req) } if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; } @@ -1514,6 +1600,7 @@ static gboolean attribute_get(struct client_s *client, gchar **req) xmlNodePtr n; xmlChar *a; gchar **nreq = NULL; + xmlErrorPtr xml_error; if (!req || !req[0] || !req[1]) { send_error(client, EPWMD_COMMAND_SYNTAX); @@ -1534,6 +1621,8 @@ static gboolean attribute_get(struct client_s *client, gchar **req) } if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); g_strfreev(nreq); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; @@ -1555,6 +1644,8 @@ static gboolean attribute_get(struct client_s *client, gchar **req) g_strfreev(nreq); if ((n = xmlTextReaderCurrentNode(client->reader)) == NULL) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); return FALSE; } @@ -1577,6 +1668,7 @@ static gboolean attribute_get(struct client_s *client, gchar **req) static gboolean attribute_set(struct client_s *client, gchar **req) { gchar **epath = NULL; + xmlErrorPtr xml_error; if (!req || !req[0] || !req[1] || !req[2]) { send_error(client, EPWMD_COMMAND_SYNTAX); @@ -1608,6 +1700,8 @@ static gboolean attribute_set(struct client_s *client, gchar **req) } if (reset_reader(client->doc, &client->reader) == FALSE) { + xml_error = xmlGetLastError(); + log_write("%s(%i): %s", __FILE__, __LINE__, xml_error->message); send_error(client, EPWMD_LIBXML_ERROR); goto blah; } diff --git a/src/pwmd.c b/src/pwmd.c index 98c57ea6..cbb614b3 100644 --- a/src/pwmd.c +++ b/src/pwmd.c @@ -90,15 +90,11 @@ void log_write(const gchar *fmt, ...) time_t now; gchar tbuf[21]; gint fd; - FILE *fp; - gint tofile = 0; + gint tofile = !isatty(STDOUT_FILENO); if (!logfile || !fmt) return; - if ((fp = fdopen(STDOUT_FILENO, "w")) == NULL) - tofile = 1; - if (tofile) { if ((fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600)) == -1) { warn("logfile"); @@ -648,11 +644,16 @@ static void doit(int fd, gint iter) GPollFD gfd = { fd, G_IO_IN|G_IO_OUT|G_IO_HUP|G_IO_ERR, 0 }; GMemVTable mtable = { xmalloc, xrealloc, xfree, xcalloc, NULL, NULL }; + signal(SIGCHLD, SIG_DFL); + signal(SIGHUP, SIG_DFL); + signal(SIGINT, SIG_DFL); g_mem_set_vtable(&mtable); #ifdef HAVE_MLOCKALL - if (use_mlock && mlockall(MCL_FUTURE) == -1) - err(EXIT_FAILURE, "mlockall()"); + if (use_mlock && mlockall(MCL_FUTURE) == -1) { + log_write("mlockall(): %s", strerror(errno)); + exit(EXIT_FAILURE); + } #endif gloop = g_main_loop_new(NULL, TRUE); @@ -665,11 +666,6 @@ static void doit(int fd, gint iter) g_source_add_poll(cl->src, &cl->gfd); g_source_set_callback(cl->src, source_cb, NULL, NULL); g_source_attach(cl->src, NULL); -#ifndef DEBUG - close(0); - close(1); - close(2); -#endif xmlMemSetup(xfree, xmalloc, xrealloc, xstrdup); xmlInitMemory(); @@ -1351,6 +1347,13 @@ int main(int argc, char *argv[]) signal(SIGTERM, catchsig); signal(SIGINT, catchsig); signal(SIGHUP, catchsig); + +#ifndef DEBUG + close(0); + close(1); + close(2); +#endif + log_write("%s starting: %li slots available", PACKAGE_STRING, cache_size / sizeof(file_cache_t)); while (!quit) { -- 2.11.4.GIT