libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

commit b57601ab81464c1539f14a763f35fccba751f6c1
parent 9cb851a3d59e4287837d3a49699e60d0494af6a4
Author: Christian Grothoff <christian@grothoff.org>
Date:   Mon, 30 Jul 2012 14:46:36 +0000

adding logging

Diffstat:
Msrc/main/extractor.c | 8++++++++
Msrc/main/extractor_common.c | 19++++++++++++++++---
Msrc/main/extractor_common.h | 6+++++-
Msrc/main/extractor_datasource.c | 163+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Msrc/main/extractor_ipc.c | 12++++++++++--
Msrc/main/extractor_ipc_gnu.c | 40+++++++++++++++++++++++++++++++++-------
Msrc/main/extractor_logging.h | 13++++++++++---
Msrc/main/extractor_plugin_main.c | 128++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/main/extractor_plugins.c | 80+++++++++++++++++++++++++++++++------------------------------------------------
Msrc/main/extractor_plugpath.c | 57+++++++++++++++++++++++++++++++++++++--------------------
Msrc/main/extractor_print.c | 10+++++++---
11 files changed, 385 insertions(+), 151 deletions(-)

diff --git a/src/main/extractor.c b/src/main/extractor.c @@ -27,6 +27,7 @@ #include <ltdl.h> #include "extractor_datasource.h" #include "extractor_ipc.h" +#include "extractor_logging.h" #include "extractor_plugpath.h" #include "extractor_plugins.h" @@ -87,6 +88,7 @@ send_update_message (struct EXTRACTOR_PluginList *plugin, &um, sizeof (um)) ) { + LOG ("Failed to send UPDATED_SHM message to plugin\n"); EXTRACTOR_IPC_channel_destroy_ (plugin->channel); plugin->channel = NULL; plugin->round_finished = 1; @@ -110,6 +112,7 @@ send_discard_message (struct EXTRACTOR_PluginList *plugin) &disc_msg, sizeof (disc_msg)) ) { + LOG ("Failed to send DISCARD_STATE message to plugin\n"); EXTRACTOR_IPC_channel_destroy_ (plugin->channel); plugin->channel = NULL; plugin->round_finished = 1; @@ -182,6 +185,7 @@ process_plugin_reply (void *cls, &cont_msg, sizeof (cont_msg)) ) { + LOG ("Failed to send CONTINUE_EXTRACTING message to plugin\n"); EXTRACTOR_IPC_channel_destroy_ (plugin->channel); plugin->channel = NULL; plugin->round_finished = 1; @@ -240,6 +244,7 @@ do_extract (struct EXTRACTOR_PluginList *plugins, &start, sizeof (start)) ) ) { + LOG ("Failed to send EXTRACT_START message to plugin\n"); EXTRACTOR_IPC_channel_destroy_ (pos->channel); pos->channel = NULL; } @@ -265,6 +270,7 @@ do_extract (struct EXTRACTOR_PluginList *plugins, &prp)) { /* serious problem in IPC; reset *all* channels */ + LOG ("Failed to receive message from channels; full reset\n"); abort_all_channels (plugins); break; } @@ -298,6 +304,7 @@ do_extract (struct EXTRACTOR_PluginList *plugins, min_seek, DEFAULT_SHM_SIZE))) { + LOG ("Failed to seek; full reset\n"); abort_all_channels (plugins); break; } @@ -334,6 +341,7 @@ do_extract (struct EXTRACTOR_PluginList *plugins, { if (EXTRACTOR_OPTION_IN_PROCESS != pos->flags) continue; + LOG ("In-process plugins not implemented\n"); // FIXME: initialize read/seek context... // pos->extract_method (FIXME); } diff --git a/src/main/extractor_common.c b/src/main/extractor_common.c @@ -17,9 +17,14 @@ Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - +/** + * @file main/extractor_common.c + * @brief commonly used functions within the library + * @author Christian Grothoff + */ #include "platform.h" #include "extractor_common.h" +#include "extractor_logging.h" #include "extractor.h" #include <sys/types.h> #include <signal.h> @@ -48,7 +53,11 @@ EXTRACTOR_write_all_ (int fd, { ret = write (fd, &data[off], size - off); if (ret <= 0) - return -1; + { + if (-1 == ret) + LOG_STRERROR ("write"); + return -1; + } off += ret; } return size; @@ -76,7 +85,11 @@ EXTRACTOR_read_all_ (int fd, { ret = read (fd, &data[off], size - off); if (ret <= 0) - return -1; + { + if (-1 == ret) + LOG_STRERROR ("write"); + return -1; + } off += ret; } return size; diff --git a/src/main/extractor_common.h b/src/main/extractor_common.h @@ -17,7 +17,11 @@ Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ - +/** + * @file main/extractor_common.h + * @brief commonly used functions within the library + * @author Christian Grothoff + */ #ifndef EXTRACTOR_COMMON_H #define EXTRACTOR_COMMON_H diff --git a/src/main/extractor_datasource.c b/src/main/extractor_datasource.c @@ -23,6 +23,7 @@ * @author Christian Grothoff */ #include "platform.h" +#include "extractor_logging.h" #include "extractor_datasource.h" #if HAVE_LIBBZ2 @@ -218,7 +219,10 @@ bfds_pick_next_buffer_at (struct BufferedFileDataSource *bfds, ssize_t rd; if (pos > bfds->fsize) - return -1; /* invalid */ + { + LOG ("Invalid seek operation\n"); + return -1; /* invalid */ + } if (NULL == bfds->buffer) { bfds->buffer_pos = pos; @@ -231,7 +235,10 @@ bfds_pick_next_buffer_at (struct BufferedFileDataSource *bfds, bfds->buffer_pos = 0; rd = read (bfds->fd, bfds->buffer, bfds->buffer_size); if (rd < 0) - return -1; + { + LOG_STRERROR ("read"); + return -1; + } bfds->buffer_bytes = rd; return 0; } @@ -258,13 +265,19 @@ bfds_new (const void *data, else xtra = (size_t) fsize; if ( (-1 == fd) && (NULL == data) ) - return NULL; + { + LOG ("Invalid arguments\n"); + return NULL; + } if ( (-1 != fd) && (NULL != data) ) fd = -1; /* don't need fd */ if (NULL != data) xtra = 0; if (NULL == (result = malloc (sizeof (struct BufferedFileDataSource) + xtra))) - return NULL; + { + LOG_STRERROR ("malloc"); + return NULL; + } memset (result, 0, sizeof (struct BufferedFileDataSource)); result->data = (NULL != data) ? data : &result[1]; result->buffer = (NULL != data) ? NULL : &result[1]; @@ -308,9 +321,15 @@ bfds_seek (struct BufferedFileDataSource *bfds, { case SEEK_CUR: if (bfds->fpos + bfds->buffer_pos + pos < 0) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } if (bfds->fpos + bfds->buffer_pos + pos > bfds->fsize) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } if ( (NULL == bfds->buffer) || ( (bfds->buffer_pos + pos < bfds->buffer_bytes) && (bfds->buffer_pos + pos >= 0) ) ) @@ -320,20 +339,35 @@ bfds_seek (struct BufferedFileDataSource *bfds, } if (0 != bfds_pick_next_buffer_at (bfds, bfds->fpos + bfds->buffer_pos + pos)) - return -1; + { + LOG ("seek operation failed\n"); + return -1; + } return bfds->fpos; case SEEK_END: if (pos > 0) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } if (bfds->fsize < - pos) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } pos = bfds->fsize + pos; /* fall-through! */ case SEEK_SET: if (pos < 0) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } if (pos > bfds->fsize) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } if ( (NULL == bfds->buffer) || ( (bfds->buffer_pos <= pos) && (bfds->buffer_pos + bfds->buffer_bytes > pos) ) ) @@ -342,7 +376,10 @@ bfds_seek (struct BufferedFileDataSource *bfds, return bfds->buffer_pos; } if (0 != bfds_pick_next_buffer_at (bfds, pos)) - return -1; + { + LOG ("seek operation failed\n"); + return -1; + } return bfds->fpos; } return -1; @@ -383,13 +420,13 @@ bfds_read (struct BufferedFileDataSource *bfds, bfds->fpos = old_off; bfds->buffer_bytes = 0; bfds->buffer_pos = 0; + LOG ("read operation failed\n"); return -1; /* getting more failed */ } avail = bfds->buffer_bytes - bfds->buffer_pos; if (avail > count) avail = count; - if (0 == avail) - abort (); /* must not happen */ + ASSERT (0 != avail); memcpy (&cbuf[ret], bfds->data + bfds->buffer_pos, avail); bfds->buffer_pos += avail; count -= avail; @@ -433,6 +470,7 @@ cfs_reset_stream_zlib (struct CompressedFileSource *cfs) #endif )) { + LOG ("Failed to initialize zlib decompression\n"); return -1; } cfs->fpos = 0; @@ -450,6 +488,7 @@ static int cfs_reset_stream_bz2 (struct CompressedFileSource *cfs) { /* not implemented */ + LOG ("bz2 decompression not implemented\n"); return -1; } @@ -473,6 +512,7 @@ cfs_reset_stream (struct CompressedFileSource *cfs) case COMP_TYPE_BZ2: return cfs_reset_stream_bz2 (cfs); default: + LOG ("invalid compression type selected\n"); return -1; } } @@ -575,6 +615,7 @@ static int cfs_init_decompressor_bz2 (struct CompressedFileSource *cfs, EXTRACTOR_MetaDataProcessor proc, void *proc_cls) { + LOG ("bz2 decompression not implemented\n"); return -1; } @@ -599,6 +640,7 @@ cfs_init_decompressor (struct CompressedFileSource *cfs, case COMP_TYPE_BZ2: return cfs_init_decompressor_bz2 (cfs, proc, proc_cls); default: + LOG ("invalid compression type selected\n"); return -1; } } @@ -627,6 +669,7 @@ cfs_deinit_decompressor_zlib (struct CompressedFileSource *cfs) static int cfs_deinit_decompressor_bz2 (struct CompressedFileSource *cfs) { + LOG ("bz2 decompression not implemented\n"); return -1; } @@ -647,6 +690,7 @@ cfs_deinit_decompressor (struct CompressedFileSource *cfs) case COMP_TYPE_BZ2: return cfs_deinit_decompressor_bz2 (cfs); default: + LOG ("invalid compression type selected\n"); return -1; } } @@ -684,7 +728,10 @@ cfs_new (struct BufferedFileDataSource *bfds, struct CompressedFileSource *cfs; if (NULL == (cfs = malloc (sizeof (struct CompressedFileSource)))) - return NULL; + { + LOG_STRERROR ("malloc"); + return NULL; + } memset (cfs, 0, sizeof (struct CompressedFileSource)); cfs->compression_type = compression_type; cfs->bfds = bfds; @@ -784,6 +831,7 @@ cfs_read_bz2 (struct CompressedFileSource *cfs, void *data, size_t size) { + LOG ("bz2 decompression not implemented\n"); return -1; } @@ -810,6 +858,7 @@ cfs_read (struct CompressedFileSource *cfs, case COMP_TYPE_BZ2: return cfs_read_bz2 (cfs, data, size); default: + LOG ("invalid compression type selected\n"); return -1; } } @@ -837,33 +886,53 @@ cfs_seek (struct CompressedFileSource *cfs, { case SEEK_CUR: if (cfs->fpos + position < 0) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } if ( (-1 != cfs->uncompressed_size) && (cfs->fpos + position > cfs->uncompressed_size) ) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } nposition = cfs->fpos + position; break; case SEEK_END: if (-1 == cfs->uncompressed_size) { /* yuck, need to first find end of file! */ + LOG ("Seeking from end-of-file in compressed files not implemented\n"); return -1; // FIXME: not implemented } if (position > 0) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } if (cfs->uncompressed_size < - position) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } nposition = cfs->uncompressed_size + position; break; case SEEK_SET: if (position < 0) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } if ( (-1 != cfs->uncompressed_size) && (cfs->uncompressed_size < position ) ) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } nposition = (uint64_t) position; break; default: + LOG ("Invalid seek operation\n"); return -1; } @@ -878,7 +947,10 @@ cfs_seek (struct CompressedFileSource *cfs, else { if (-1 == cfs_reset_stream (cfs)) - return -1; + { + LOG ("Failed to restart compressed stream for seek operation\n"); + return -1; + } delta = nposition; } } @@ -891,7 +963,10 @@ cfs_seek (struct CompressedFileSource *cfs, max = (sizeof (buf) > delta) ? delta : sizeof (buf); ret = cfs_read (cfs, buf, max); if (-1 == ret) - return -1; + { + LOG ("Failed to read decompressed stream for seek operation\n"); + return -1; + } delta -= ret; } return cfs->fpos; @@ -979,10 +1054,14 @@ EXTRACTOR_datasource_create_from_file_ (const char *filename, int64_t fsize; if (-1 == (fd = open (filename, O_RDONLY | O_LARGEFILE))) - return NULL; + { + LOG_STRERROR_FILE ("open", filename); + return NULL; + } if ( (0 != fstat (fd, &sb)) || (S_ISDIR (sb.st_mode)) ) { + LOG_STRERROR_FILE ("fstat", filename); (void) close (fd); return NULL; } @@ -1000,21 +1079,26 @@ EXTRACTOR_datasource_create_from_file_ (const char *filename, } if (NULL == (ds = malloc (sizeof (struct EXTRACTOR_Datasource)))) { + LOG_STRERROR ("malloc"); bfds_delete (bfds); return NULL; } ds->bfds = bfds; ds->fd = fd; + ds->cfs = NULL; ct = get_compression_type (bfds); if ( (COMP_TYPE_ZLIB == ct) || (COMP_TYPE_BZ2 == ct) ) - ds->cfs = cfs_new (bfds, fsize, ct, proc, proc_cls); - if (NULL == ds->cfs) { - bfds_delete (bfds); - free (ds); - (void) close (fd); - return NULL; + ds->cfs = cfs_new (bfds, fsize, ct, proc, proc_cls); + if (NULL == ds->cfs) + { + LOG ("Failed to initialize decompressor\n"); + bfds_delete (bfds); + free (ds); + (void) close (fd); + return NULL; + } } return ds; } @@ -1041,9 +1125,13 @@ EXTRACTOR_datasource_create_from_buffer_ (const char *buf, if (0 == size) return NULL; if (NULL == (bfds = bfds_new (buf, -1, size))) - return NULL; + { + LOG ("Failed to initialize buffer data source\n"); + return NULL; + } if (NULL == (ds = malloc (sizeof (struct EXTRACTOR_Datasource)))) { + LOG_STRERROR ("malloc"); bfds_delete (bfds); return NULL; } @@ -1052,12 +1140,15 @@ EXTRACTOR_datasource_create_from_buffer_ (const char *buf, ct = get_compression_type (bfds); if ( (COMP_TYPE_ZLIB == ct) || (COMP_TYPE_BZ2 == ct) ) - ds->cfs = cfs_new (bfds, size, ct, proc, proc_cls); - if (NULL == ds->cfs) { - bfds_delete (bfds); - free (ds); - return NULL; + ds->cfs = cfs_new (bfds, size, ct, proc, proc_cls); + if (NULL == ds->cfs) + { + LOG ("Failed to initialize decompressor\n"); + bfds_delete (bfds); + free (ds); + return NULL; + } } return ds; } diff --git a/src/main/extractor_ipc.c b/src/main/extractor_ipc.c @@ -23,6 +23,7 @@ * @author Christian Grothoff */ #include "platform.h" +#include "extractor_logging.h" #include "extractor_ipc.h" #include "extractor_plugins.h" @@ -78,7 +79,10 @@ EXTRACTOR_IPC_process_reply_ (struct EXTRACTOR_PluginList *plugin, memcpy (&meta, cdata, sizeof (meta)); /* check hdr for sanity */ if (meta.value_size > MAX_META_DATA) - return -1; /* not allowing more than MAX_META_DATA meta data */ + { + LOG ("Meta data exceeds size limit\n"); + return -1; /* not allowing more than MAX_META_DATA meta data */ + } if (size < sizeof (meta) + meta.mime_length + meta.value_size) { plugin->seek_request = -1; @@ -92,7 +96,10 @@ EXTRACTOR_IPC_process_reply_ (struct EXTRACTOR_PluginList *plugin, { mime_type = &cdata[sizeof (struct MetaMessage)]; if ('\0' != mime_type[meta.mime_length - 1]) - return -1; + { + LOG ("Mime type not 0-terminated\n"); + return -1; + } } if (0 == meta.value_size) value = NULL; @@ -106,6 +113,7 @@ EXTRACTOR_IPC_process_reply_ (struct EXTRACTOR_PluginList *plugin, mime_type, value); return sizeof (struct MetaMessage) + meta.mime_length + meta.value_size; default: + LOG ("Invalid message type %d\n", (int) code); return -1; } } diff --git a/src/main/extractor_ipc_gnu.c b/src/main/extractor_ipc_gnu.c @@ -26,6 +26,7 @@ #include "plibc.h" #include "extractor.h" #include "extractor_datasource.h" +#include "extractor_logging.h" #include "extractor_plugin_main.h" #include "extractor_ipc.h" #include <dirent.h> @@ -136,7 +137,10 @@ EXTRACTOR_IPC_shared_memory_create_ (size_t size) const char *tpath; if (NULL == (shm = malloc (sizeof (struct EXTRACTOR_SharedMemory)))) - return NULL; + { + LOG_STRERROR ("malloc"); + return NULL; + } #if SOMEBSD /* this works on FreeBSD, not sure about others... */ tpath = getenv ("TMPDIR"); @@ -153,6 +157,7 @@ EXTRACTOR_IPC_shared_memory_create_ (size_t size) if (-1 == (shm->shm_id = shm_open (shm->shm_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR))) { + LOG_STRERROR_FILE ("shm_open", shm->shm_name); free (shm); return NULL; } @@ -162,6 +167,7 @@ EXTRACTOR_IPC_shared_memory_create_ (size_t size) shm->shm_id, 0))) || (((void*) -1) == shm->shm_ptr) ) { + LOG_STRERROR ("ftruncate/mmap"); (void) close (shm->shm_id); (void) shm_unlink (shm->shm_name); free (shm); @@ -251,16 +257,21 @@ EXTRACTOR_IPC_channel_create_ (struct EXTRACTOR_PluginList *plugin, size_t slen; if (NULL == (channel = malloc (sizeof (struct EXTRACTOR_Channel)))) - return NULL; + { + LOG_STRERROR ("malloc"); + return NULL; + } channel->shm = shm; channel->plugin = plugin; if (0 != pipe (p1)) { + LOG_STRERROR ("pipe"); free (channel); return NULL; } if (0 != pipe (p2)) { + LOG_STRERROR ("pipe"); (void) close (p1[0]); (void) close (p1[1]); free (channel); @@ -269,6 +280,7 @@ EXTRACTOR_IPC_channel_create_ (struct EXTRACTOR_PluginList *plugin, pid = fork (); if (pid == -1) { + LOG_STRERROR ("fork"); (void) close (p1[0]); (void) close (p1[1]); (void) close (p2[0]); @@ -291,6 +303,7 @@ EXTRACTOR_IPC_channel_create_ (struct EXTRACTOR_PluginList *plugin, slen = strlen (shm->shm_name) + 1; if (NULL == (init = malloc (sizeof (struct InitMessage) + slen))) { + LOG_STRERROR ("malloc"); EXTRACTOR_IPC_channel_destroy_ (channel); return NULL; } @@ -304,6 +317,7 @@ EXTRACTOR_IPC_channel_create_ (struct EXTRACTOR_PluginList *plugin, init, sizeof (init) + slen) ) { + LOG ("Failed to send INIT_STATE message to plugin\n"); EXTRACTOR_IPC_channel_destroy_ (channel); return NULL; } @@ -322,10 +336,14 @@ EXTRACTOR_IPC_channel_destroy_ (struct EXTRACTOR_Channel *channel) { int status; - (void) kill (channel->cpid, SIGKILL); - (void) waitpid (channel->cpid, &status, 0); - (void) close (channel->cpipe_out); - (void) close (channel->cpipe_in); + if (0 != kill (channel->cpid, SIGKILL)) + LOG_STRERROR ("kill"); + if (-1 == waitpid (channel->cpid, &status, 0)) + LOG_STRERROR ("waitpid"); + if (0 != close (channel->cpipe_out)) + LOG_STRERROR ("close"); + if (0 != close (channel->cpipe_in)) + LOG_STRERROR ("close"); free (channel); } @@ -352,7 +370,11 @@ EXTRACTOR_IPC_channel_send_ (struct EXTRACTOR_Channel *channel, { ret = write (channel->cpipe_in, &cdata[off], size - off); if (ret <= 0) - return -1; + { + if (-1 == ret) + LOG_STRERROR ("write"); + return -1; + } off += ret; } return size; @@ -404,6 +426,8 @@ EXTRACTOR_IPC_channel_recv_ (struct EXTRACTOR_Channel **channels, if (-1 == select (max + 1, &to_check, NULL, NULL, &tv)) { /* an error or timeout -> something's wrong or all plugins hung up */ + if (EINTR != errno) + LOG_STRERROR ("select"); return -1; } for (i=0;i<num_channels;i++) @@ -421,6 +445,8 @@ EXTRACTOR_IPC_channel_recv_ (struct EXTRACTOR_Channel **channels, channel->size + iret, proc, proc_cls)) ) { + if (-1 == iret) + LOG_STRERROR ("read"); EXTRACTOR_IPC_channel_destroy_ (channel); channels[i] = NULL; } diff --git a/src/main/extractor_logging.h b/src/main/extractor_logging.h @@ -42,12 +42,19 @@ EXTRACTOR_log_ (const char *file, int line, const char *format, ...); /** * Log a message. * - * @param fmt format string - * @param ... arguments for fmt (printf-style) + * @param ... format string and arguments for fmt (printf-style) */ -#define LOG(fmt, ...) EXTRACTOR_log_ (__FILE__, __LINE__, fmt, __VA_ARGS__) +#define LOG(...) EXTRACTOR_log_(__FILE__, __LINE__, __VA_ARGS__) + #else + +/** + * Log a message. + * + * @param ... format string and arguments for fmt (printf-style) + */ #define LOG(...) + #endif diff --git a/src/main/extractor_plugin_main.c b/src/main/extractor_plugin_main.c @@ -22,7 +22,6 @@ * @brief main loop for an out-of-process plugin * @author Christian Grothoff */ - #include "platform.h" #include "plibc.h" #include "extractor.h" @@ -30,6 +29,7 @@ #include "extractor_datasource.h" #include "extractor_plugins.h" #include "extractor_ipc.h" +#include "extractor_logging.h" #include "extractor_plugin_main.h" #include <dirent.h> #include <sys/types.h> @@ -120,24 +120,37 @@ plugin_env_seek (void *cls, { case SEEK_CUR: if ( (pos < 0) && (pc->read_position < - pos) ) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } if ( (pos > 0) && ( (pc->read_position + pos < pc->read_position) || (pc->read_position + pos > pc->file_size) ) ) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } npos = (uint64_t) (pc->read_position + pos); break; case SEEK_END: if (pos > 0) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } pos = (int64_t) (pc->file_size + pos); /* fall-through! */ case SEEK_SET: if ( (pos < 0) || (pc->file_size < pos) ) - return -1; + { + LOG ("Invalid seek operation\n"); + return -1; + } npos = (uint64_t) pos; break; default: + LOG ("Invalid seek operation\n"); return -1; } if ( (pc->shm_off <= npos) && @@ -155,15 +168,24 @@ plugin_env_seek (void *cls, srm.requested_bytes = pc->file_size - npos; srm.file_offset = npos; if (-1 == EXTRACTOR_write_all_ (pc->out, &srm, sizeof (srm))) - return -1; + { + LOG ("Failed to send MESSAGE_SEEK\n"); + return -1; + } if (-1 == EXTRACTOR_read_all_ (pc->in, &reply, sizeof (reply))) - return -1; - if (MESSAGE_SEEK != reply) + { + LOG ("Failed to read response to MESSAGE_SEEK\n"); + return -1; + } + if (MESSAGE_SEEK != reply) return -1; /* was likely a MESSAGE_DISCARD_STATE */ if (-1 == EXTRACTOR_read_all_ (pc->in, &um.reserved, sizeof (um) - 1)) - return -1; + { + LOG ("Failed to read UPDATE_MESSAGE\n"); + return -1; + } pc->shm_off = um.shm_off; pc->shm_ready_bytes = um.shm_ready_bytes; if ( (pc->shm_off <= npos) && @@ -174,6 +196,7 @@ plugin_env_seek (void *cls, } /* oops, serious missunderstanding, we asked to seek and then were notified about a different position!? */ + LOG ("Got invalid UPDATE_MESSAGE in response to my seek\n"); return -1; } @@ -200,7 +223,10 @@ plugin_env_read (void *cls, if ( ( (pc->read_position >= pc->shm_off + pc->shm_ready_bytes) || (pc->read_position < pc->shm_off) ) && (-1 == plugin_env_seek (pc, pc->read_position, SEEK_SET)) ) - return -1; + { + LOG ("Failed to seek to satisfy read\n"); + return -1; + } if (pc->read_position + count > pc->shm_off + pc->shm_ready_bytes) count = pc->shm_off + pc->shm_ready_bytes - pc->read_position; dp = pc->shm; @@ -220,6 +246,7 @@ static uint64_t plugin_env_get_size (void *cls) { struct ProcessingContext *pc = cls; + return pc->file_size; } @@ -275,11 +302,17 @@ plugin_env_send_proc (void *cls, (data_len != EXTRACTOR_write_all_ (pc->out, data, data_len)) ) - return 1; + { + LOG ("Failed to send meta message\n"); + return 1; + } if (-1 == EXTRACTOR_read_all_ (pc->in, &reply, sizeof (reply))) - return 1; + { + LOG ("Failed to read response to meta message\n"); + return 1; + } if (MESSAGE_CONTINUE_EXTRACTING != reply) return 1; return 0; @@ -298,14 +331,23 @@ handle_init_message (struct ProcessingContext *pc) struct InitMessage init; if (NULL != pc->shm) - return -1; + { + LOG ("Cannot handle 'init' message, have already been initialized\n"); + return -1; + } if (sizeof (struct InitMessage) - 1 != EXTRACTOR_read_all_ (pc->in, &init.reserved, sizeof (struct InitMessage) - 1)) - return -1; + { + LOG ("Failed to read 'init' message\n"); + return -1; + } if (init.shm_name_length > MAX_SHM_NAME) - return -1; + { + LOG ("Invalid 'init' message\n"); + return -1; + } { char shm_name[init.shm_name_length + 1]; @@ -313,7 +355,10 @@ handle_init_message (struct ProcessingContext *pc) != EXTRACTOR_read_all_ (pc->in, shm_name, init.shm_name_length)) - return -1; + { + LOG ("Failed to read 'init' message\n"); + return -1; + } shm_name[init.shm_name_length] = '\0'; pc->shm_map_size = init.shm_map_size; @@ -324,14 +369,20 @@ handle_init_message (struct ProcessingContext *pc) #else pc->shm_id = open (shm_name, O_RDONLY, 0); if (-1 == pc->shm_id) - return -1; + { + LOG_STRERROR_FILE ("open", shm_name); + return -1; + } pc->shm = mmap (NULL, pc->shm_map_size, PROT_READ, MAP_SHARED, pc->shm_id, 0); if ( ((void*) -1) == pc->shm) - return -1; + { + LOG_STRERROR_FILE ("mmap", shm_name); + return -1; + } #endif } return 0; @@ -355,7 +406,10 @@ handle_start_message (struct ProcessingContext *pc) != EXTRACTOR_read_all_ (pc->in, &start.reserved, sizeof (struct StartMessage) - 1)) - return -1; + { + LOG ("Failed to read 'start' message\n"); + return -1; + } pc->shm_ready_bytes = start.shm_ready_bytes; pc->file_size = start.file_size; pc->read_position = 0; @@ -369,7 +423,10 @@ handle_start_message (struct ProcessingContext *pc) pc->plugin->extract_method (&ec); done = MESSAGE_DONE; if (-1 == EXTRACTOR_write_all_ (pc->out, &done, sizeof (done))) - return -1; + { + LOG ("Failed to write 'done' message\n"); + return -1; + } if ( (NULL != pc->plugin->specials) && (NULL != strstr (pc->plugin->specials, "force-kill")) ) { @@ -400,7 +457,10 @@ process_requests (struct ProcessingContext *pc) unsigned char code; if (1 != EXTRACTOR_read_all_ (pc->in, &code, 1)) - break; + { + LOG ("Failed to read next request\n"); + break; + } switch (code) { case MESSAGE_INIT_STATE: @@ -417,6 +477,7 @@ process_requests (struct ProcessingContext *pc) /* odd, we're already in the start state... */ continue; default: + LOG ("Received invalid messag %d\n", (int) code); /* error, unexpected message */ return; } @@ -440,16 +501,21 @@ open_dev_null (int target_fd, fd = open ("/dev/null", flags); if (-1 == fd) - return; /* good luck */ + { + LOG_STRERROR_FILE ("open", "/dev/null"); + return; /* good luck */ + } if (fd == target_fd) return; /* already done */ if (-1 == dup2 (fd, target_fd)) { + LOG_STRERROR ("dup2"); (void) close (fd); return; /* good luck */ } /* close original result from 'open' */ - (void) close (fd); + if (0 != close (fd)) + LOG_STRERROR ("close"); } #endif @@ -479,7 +545,8 @@ EXTRACTOR_plugin_main_ (struct EXTRACTOR_PluginList *plugin, if ( (NULL != plugin->specials) && (NULL != strstr (plugin->specials, "close-stderr"))) { - (void) close (2); + if (0 != close (2)) + LOG_STRERROR ("close"); #ifndef WINDOWS open_dev_null (2, O_WRONLY); #endif @@ -487,7 +554,8 @@ EXTRACTOR_plugin_main_ (struct EXTRACTOR_PluginList *plugin, if ( (NULL != plugin->specials) && (NULL != strstr (plugin->specials, "close-stdout"))) { - (void) close (1); + if (0 != close (1)) + LOG_STRERROR ("close"); #ifndef WINDOWS open_dev_null (1, O_WRONLY); #endif @@ -507,7 +575,10 @@ EXTRACTOR_plugin_main_ (struct EXTRACTOR_PluginList *plugin, (((void*) 1) != pc.shm) ) munmap (pc.shm, pc.shm_map_size); if (-1 != pc.shm_id) - (void) close (pc.shm_id); + { + if (0 != close (pc.shm_id)) + LOG_STRERROR ("close"); + } #endif } @@ -529,7 +600,10 @@ read_plugin_data (int fd) // FIXME: check for errors from 'EXTRACTOR_read_all_'! if (NULL == (ret = malloc (sizeof (struct EXTRACTOR_PluginList)))) - return NULL; + { + LOG_STRERROR ("malloc"); + return NULL; + } GetSystemInfo (&si); ret->allocation_granularity = si.dwAllocationGranularity; EXTRACTOR_read_all_ (fd, &i, sizeof (size_t)); diff --git a/src/main/extractor_plugins.c b/src/main/extractor_plugins.c @@ -25,6 +25,7 @@ #include "extractor_plugins.h" #include "extractor_plugpath.h" #include "extractor_ipc.h" +#include "extractor_logging.h" /** @@ -55,9 +56,11 @@ get_symbol_with_prefix (void *lib_handle, return NULL; sym_name++; if (NULL == (sym = strdup (sym_name))) - return NULL; - dot = strchr (sym, '.'); - if (NULL != dot) + { + LOG_STRERROR ("strdup"); + return NULL; + } + if (NULL != (dot = strchr (sym, '.'))) *dot = '\0'; if (NULL == (name = malloc(strlen(sym) + strlen(template) + 1))) { @@ -72,34 +75,29 @@ get_symbol_with_prefix (void *lib_handle, if (NULL == symbol) { /* now try with the '_' */ -#if DEBUG char *first_error = strdup (lt_dlerror ()); -#endif symbol = lt_dlsym (lib_handle, name); -#if DEBUG if (NULL == symbol) { - fprintf (stderr, - "Resolving symbol `%s' failed, " - "so I tried `%s', but that failed also. Errors are: " - "`%s' and `%s'.\n", - name+1, - name, - first_error == NULL ? "out of memory" : first_error, - lt_dlerror()); + LOG ("Resolving symbol `%s' failed, " + "so I tried `%s', but that failed also. Errors are: " + "`%s' and `%s'.\n", + name+1, + name, + first_error == NULL ? "out of memory" : first_error, + lt_dlerror ()); } if (NULL != first_error) free (first_error); -#endif } if ( (NULL != symbol) && (NULL != options) ) { /* get special options */ - sprintf(name, - "_EXTRACTOR_%s_options", - sym); + sprintf (name, + "_EXTRACTOR_%s_options", + sym); /* try without '_' first */ opt_fun = lt_dlsym (lib_handle, name + 1); if (NULL == opt_fun) @@ -134,11 +132,8 @@ EXTRACTOR_plugin_load_ (struct EXTRACTOR_PluginList *plugin) plugin->libname = EXTRACTOR_find_plugin_ (plugin->short_libname); if (NULL == plugin->libname) { -#if DEBUG - fprintf (stderr, - "Failed to find plugin `%s'\n", - plugin->short_libname); -#endif + LOG ("Failed to find plugin `%s'\n", + plugin->short_libname); plugin->flags = EXTRACTOR_OPTION_DISABLED; return -1; } @@ -153,12 +148,9 @@ EXTRACTOR_plugin_load_ (struct EXTRACTOR_PluginList *plugin) (WideCharToMultiByte (CP_ACP, 0, wlibname, -1, llibname, sizeof (llibname), NULL, NULL) < 0) ) { -#if DEBUG - fprintf (stderr, - "Loading `%s' plugin failed: %s\n", - plugin->short_libname, - "can't convert plugin name to local encoding"); -#endif + LOG ("Loading `%s' plugin failed: %s\n", + plugin->short_libname, + "can't convert plugin name to local encoding"); free (plugin->libname); plugin->libname = NULL; plugin->flags = EXTRACTOR_OPTION_DISABLED; @@ -173,12 +165,9 @@ EXTRACTOR_plugin_load_ (struct EXTRACTOR_PluginList *plugin) lt_dladvise_destroy (&advise); if (NULL == plugin->libraryHandle) { -#if DEBUG - fprintf (stderr, - "Loading `%s' plugin failed: %s\n", - plugin->short_libname, - lt_dlerror ()); -#endif + LOG ("Loading `%s' plugin failed: %s\n", + plugin->short_libname, + lt_dlerror ()); free (plugin->libname); plugin->libname = NULL; plugin->flags = EXTRACTOR_OPTION_DISABLED; @@ -190,12 +179,9 @@ EXTRACTOR_plugin_load_ (struct EXTRACTOR_PluginList *plugin) &plugin->specials); if (NULL == plugin->extract_method) { -#if DEBUG - fprintf (stderr, - "Resolving `extract' method of plugin `%s' failed: %s\n", - plugin->short_libname, - lt_dlerror ()); -#endif + LOG ("Resolving `extract' method of plugin `%s' failed: %s\n", + plugin->short_libname, + lt_dlerror ()); lt_dlclose (plugin->libraryHandle); free (plugin->libname); plugin->libname = NULL; @@ -230,9 +216,8 @@ EXTRACTOR_plugin_add (struct EXTRACTOR_PluginList *prev, return prev; /* no change, library already loaded */ if (NULL == (libname = EXTRACTOR_find_plugin_ (library))) { - fprintf (stderr, - "Could not load `%s'\n", - library); + LOG ("Could not load plugin `%s'\n", + library); return prev; } if (NULL == (result = malloc (sizeof (struct EXTRACTOR_PluginList)))) @@ -367,11 +352,8 @@ EXTRACTOR_plugin_remove (struct EXTRACTOR_PluginList *prev, } if (NULL == pos) { -#if DEBUG - fprintf(stderr, - "Unloading plugin `%s' failed!\n", - library); -#endif + LOG ("Unloading plugin `%s' failed!\n", + library); return first; } /* found, close library */ diff --git a/src/main/extractor_plugpath.c b/src/main/extractor_plugpath.c @@ -32,7 +32,7 @@ #include <ltdl.h> #include "extractor_plugpath.h" - +#include "extractor_logging.h" /** * Function to call on paths. @@ -78,6 +78,7 @@ cut_bin (char * in) return in; } + #if LINUX /** * Try to determine path by reading /proc/PID/exe or @@ -146,6 +147,7 @@ get_path_from_proc_exe () lnk = cut_bin (lnk); if (NULL == (ret = realloc (lnk, strlen(lnk) + 6))) { + LOG_STRERROR ("realloc"); free (lnk); return NULL; } @@ -178,6 +180,7 @@ get_path_from_module_filename () path = cut_bin (path); if (NULL == (ret = realloc (path, strlen(path) + 6))) { + LOG_STRERROR ("realloc"); free (path); return NULL; } @@ -223,7 +226,11 @@ get_path_from_NSGetExecutablePath () (void) func (path, &len); if (0 == len) return NULL; - path = GNUNET_malloc (len); + if (NULL == (path = malloc (len))) + { + LOG_STRERROR ("malloc"); + return NULL; + } if (0 != func (path, &len)) { GNUNET_free (path); @@ -259,7 +266,10 @@ get_path_from_dyld_image () if ( (NULL == path) || (0 == strlen (path)) ) continue; if (NULL == (p = strdup (path))) - return NULL; + { + LOG_STRERROR ("strdup"); + return NULL; + } s = p + strlen (p); while ( (s > p) && ('/' != *s) ) s--; @@ -291,9 +301,13 @@ get_path_from_PATH() { if (NULL == (p = getenv ("PATH"))) return NULL; if (NULL == (path = strdup (p))) /* because we write on it */ - return NULL; + { + LOG_STRERROR ("strdup"); + return NULL; + } if (NULL == (buf = malloc (strlen(path) + 20))) { + LOG_STRERROR ("malloc"); free (path); return NULL; } @@ -304,14 +318,17 @@ get_path_from_PATH() { sprintf(buf, "%s/%s", pos, "extract"); if (0 == stat(buf, &sbuf)) { - pos = strdup(pos); free (buf); free (path); - if (NULL == pos) - return NULL; + if (NULL == (pos = strdup(pos))) + { + LOG_STRERROR ("strdup"); + return NULL; + } pos = cut_bin (pos); if (NULL == (ret = realloc (pos, strlen(pos) + 5))) { + LOG_STRERROR ("realloc"); free (pos); return NULL; } @@ -329,9 +346,9 @@ get_path_from_PATH() { if (NULL == pos) return NULL; pos = cut_bin (pos); - ret = realloc (pos, strlen(pos) + 5); - if (NULL == ret) + if (NULL == (ret = realloc (pos, strlen(pos) + 5))) { + LOG_STRERROR ("realloc"); free (pos); return NULL; } @@ -412,7 +429,10 @@ get_installation_paths (EXTRACTOR_PathProcessor pp, if (NULL != (p = getenv ("LIBEXTRACTOR_PREFIX"))) { if (NULL == (d = strdup (p))) - return; + { + LOG_STRERROR ("strdup"); + return; + } for (prefix = strtok (d, PATH_SEPARATOR_STR); NULL != prefix; prefix = strtok (NULL, PATH_SEPARATOR_STR)) @@ -500,9 +520,9 @@ find_plugin_in_path (void *cls, if (NULL == (sym_name = strrchr (ent->d_name, '_'))) continue; sym_name++; - sym = strdup (sym_name); - if (NULL == sym) + if (NULL == (sym = strdup (sym_name))) { + LOG_STRERROR ("strdup"); CLOSEDIR (dir); return; } @@ -573,8 +593,7 @@ load_plugins_from_dir (void *cls, char *sym; char *dot; - dir = opendir (path); - if (NULL == dir) + if (NULL == (dir = opendir (path))) return; while (NULL != (ent = readdir (dir))) { @@ -586,18 +605,16 @@ load_plugins_from_dir (void *cls, (la[2] == '\0')) ) continue; /* only load '.so' and '.dll' */ - sym_name = strrchr (ent->d_name, '_'); - if (sym_name == NULL) + if (NULL == (sym_name = strrchr (ent->d_name, '_'))) continue; sym_name++; - sym = strdup (sym_name); - if (NULL == sym) + if (NULL == (sym = strdup (sym_name))) { + LOG_STRERROR ("strdup"); closedir (dir); return; } - dot = strchr (sym, '.'); - if (dot != NULL) + if (NULL != (dot = strchr (sym, '.'))) *dot = '\0'; #if DEBUG > 1 fprintf (stderr, diff --git a/src/main/extractor_print.c b/src/main/extractor_print.c @@ -20,7 +20,7 @@ #include "platform.h" #include "extractor.h" - +#include "extractor_logging.h" #include "iconv.c" /** @@ -58,10 +58,14 @@ EXTRACTOR_meta_data_print (void *handle, cd = iconv_open (nl_langinfo(CODESET), "UTF-8"); if (((iconv_t) -1) == cd) - return 1; - buf = iconv_helper(cd, data); + { + LOG_STRERROR ("iconv_open"); + return 1; + } + buf = iconv_helper (cd, data); if (NULL == buf) { + LOG_STRERROR ("iconv_helper"); ret = -1; } else