diff options
author | Christian Pointner <equinox@spreadspace.org> | 2016-05-25 23:50:32 +0200 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2016-05-25 23:50:32 +0200 |
commit | 5f9dfffc60e022ee7da315e9dc891fc0e4622001 (patch) | |
tree | 02178ac91b2fafdf07ae19d65b70ae0fcf71d60a | |
parent | major cleanup and refactoring (diff) |
further cleanups, improved options parser based on glib
-rw-r--r-- | src/Makefile | 3 | ||||
-rw-r--r-- | src/datatypes.h | 2 | ||||
-rw-r--r-- | src/gstdvbbackend.c | 150 | ||||
-rw-r--r-- | src/options.c | 408 | ||||
-rw-r--r-- | src/options.h | 38 | ||||
-rw-r--r-- | src/slist.c | 128 | ||||
-rw-r--r-- | src/slist.h | 49 | ||||
-rw-r--r-- | src/string_list.c | 67 | ||||
-rw-r--r-- | src/string_list.h | 39 | ||||
-rw-r--r-- | src/sysexec.c | 223 | ||||
-rw-r--r-- | src/sysexec.h | 47 |
11 files changed, 292 insertions, 862 deletions
diff --git a/src/Makefile b/src/Makefile index ad05106..1fac122 100644 --- a/src/Makefile +++ b/src/Makefile @@ -31,9 +31,6 @@ EXECUTABLE := gstdvbbackend C_OBJS := log.o \ options.o \ - slist.o \ - string_list.o \ - sysexec.o \ streamer.o \ gstdvbbackend.o diff --git a/src/datatypes.h b/src/datatypes.h index ef79b8f..bf9616d 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -28,6 +28,8 @@ #include <stdint.h> +#include <glib.h> + struct buffer_struct { uint32_t length_; uint8_t* buf_; diff --git a/src/gstdvbbackend.c b/src/gstdvbbackend.c index 277d16b..a58d570 100644 --- a/src/gstdvbbackend.c +++ b/src/gstdvbbackend.c @@ -23,11 +23,13 @@ * along with gstdvbbackend. If not, see <http://www.gnu.org/licenses/>. */ +#define _GNU_SOURCE #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> -#include <sys/select.h> +#include <sys/types.h> +#include <sys/socket.h> #include <glib.h> #include <glib-unix.h> @@ -35,7 +37,6 @@ #include "datatypes.h" #include "options.h" -#include "string_list.h" #include "log.h" #include "daemon.h" #include "streamer.h" @@ -59,6 +60,18 @@ static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data) g_main_loop_quit(loop); break; } + case GST_MESSAGE_APPLICATION: { + log_printf(DEBUG, "Got Application Message!"); + const GstStructure* ms = gst_message_get_structure(msg); + gboolean quit; + gst_structure_get_boolean(ms, "quit", &quit); + if(quit) { + const gchar* reason = gst_structure_get_string (ms, "reason"); + log_printf(NOTICE, "closing due to message: %s", reason); + g_main_loop_quit(loop); + } + break; + } case GST_MESSAGE_INFO: { GError *info; gst_message_parse_info(msg, &info, NULL); @@ -81,29 +94,86 @@ static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data) g_main_loop_quit(loop); break; } - default: + case GST_MESSAGE_STATE_CHANGED: { + GstState old_state, new_state; + gst_message_parse_state_changed(msg, &old_state, &new_state, NULL); + log_printf(DEBUG, "Element '%s' changed state from %s to %s", + (msg->src ? GST_OBJECT_NAME(msg->src) : "NULL"), + gst_element_state_get_name(old_state), + gst_element_state_get_name(new_state)); + break; + } + case GST_MESSAGE_NEW_CLOCK: + { + GstClock *clock; + gst_message_parse_new_clock(msg, &clock); + log_printf(NOTICE, "New clock: %s", (clock ? GST_OBJECT_NAME (clock) : "NULL")); break; } + case GST_MESSAGE_QOS: { + guint64 running_time, stream_time, timestamp, duration; + gst_message_parse_qos(msg, NULL, &running_time, &stream_time, ×tamp, &duration); + log_printf(WARNING, "Element '%s' dropped frames running_time=%lu, stream_time=%lu, timestamp=%lu, duration=%lu", + (msg->src ? GST_OBJECT_NAME(msg->src) : "NULL"), running_time, stream_time, timestamp, duration); + break; + } + /* case GST_MESSAGE_STREAM_STATUS: */ + /* { */ + /* GstStreamStatusType type; */ + /* GstElement *owner; */ + /* const GValue *val; */ + /* gchar *path, *ownerstr; */ + /* GstTask *task = NULL; */ + + /* gst_message_parse_stream_status (msg, &type, &owner); */ + /* val = gst_message_get_stream_status_object (msg); */ + + /* path = gst_object_get_path_string (GST_MESSAGE_SRC (msg)); */ + /* ownerstr = gst_object_get_path_string (GST_OBJECT (owner)); */ + /* log_printf(DEBUG,"Recevied Stream-Status message type: %d, source: %s, owner: %s, object: type %s, value %p", */ + /* type, path, ownerstr, G_VALUE_TYPE_NAME (val), g_value_get_object (val)); */ + /* g_free (path); */ + /* g_free (ownerstr); */ + + /* /\* see if we know how to deal with this object *\/ */ + /* if (G_VALUE_TYPE (val) == GST_TYPE_TASK) { */ + /* task = g_value_get_object (val); */ + /* } */ + + /* switch (type) { */ + /* case GST_STREAM_STATUS_TYPE_CREATE: */ + /* log_printf(DEBUG," created task %p", task); */ + /* break; */ + /* case GST_STREAM_STATUS_TYPE_ENTER: */ + /* /\* log_printf(DEBUG," raising task priority"); *\/ */ + /* /\* setpriority (PRIO_PROCESS, 0, -10); *\/ */ + /* break; */ + /* case GST_STREAM_STATUS_TYPE_LEAVE: */ + /* break; */ + /* default: */ + /* break; */ + /* } */ + /* break; */ + /* } */ + default: + /* log_printf(DEBUG, "unkonwn message %s from %s", GST_MESSAGE_TYPE_NAME(msg), GST_MESSAGE_SRC_NAME(msg)); */ + return TRUE; + } return TRUE; } int main_loop(options_t* opt) { - log_printf(INFO, "entering main loop"); - - GMainLoop *loop; - GstElement *pipeline, *source; - GstBus *bus; - streamer_t streamer; + log_printf(NOTICE, "entering main loop"); - loop = g_main_loop_new(NULL, FALSE); - pipeline = gst_pipeline_new("gstdvbbackend"); + GMainLoop *loop = g_main_loop_new(NULL, FALSE); + GstElement *pipeline = gst_pipeline_new("gstdvbbackend"); if(!pipeline || !loop) { log_printf(ERROR, "the pipeline/loop object could not be created. Exiting."); return -1; } - source = gst_element_factory_make ("dvbsrc", "dvb-source"); + GstElement *source = gst_element_factory_make ("dvbsrc", "dvb-source"); if(!source) { log_printf(ERROR, "Error creating dvb source"); gst_object_unref(GST_OBJECT(pipeline)); @@ -119,6 +189,7 @@ int main_loop(options_t* opt) g_object_set(G_OBJECT(source), "trans-mode", 1, NULL); // 8k g_object_set(G_OBJECT(source), "guard", 4, NULL); // AUTO + streamer_t streamer; int ret = streamer_init(&streamer, loop, opt->host_, opt->port_); if(ret) { gst_object_unref(GST_OBJECT(pipeline)); @@ -128,13 +199,20 @@ int main_loop(options_t* opt) gst_bin_add_many(GST_BIN(pipeline), source, streamer.sink_, NULL); gst_element_link_many(source, streamer.sink_, NULL); - bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); + + GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); gst_bus_add_watch(bus, bus_call, loop); - gst_object_unref(bus); + gst_object_unref(GST_OBJECT(bus)); + + gulong deep_notify_id = 0; + if(opt->debug_) { + deep_notify_id = g_signal_connect(pipeline, "deep-notify", + G_CALLBACK(gst_object_default_deep_notify), NULL); + } - log_printf(INFO, "Set State: Paused"); + log_printf(NOTICE, "Set State: Paused"); gst_element_set_state(pipeline, GST_STATE_PAUSED); - log_printf(INFO, "Set State: Playing"); + log_printf(NOTICE, "Set State: Playing"); gst_element_set_state(pipeline, GST_STATE_PLAYING); ret = streamer_start(&streamer); @@ -146,8 +224,11 @@ int main_loop(options_t* opt) streamer_stop(&streamer); } + if (deep_notify_id != 0) + g_signal_handler_disconnect(pipeline, deep_notify_id); + log_printf(NOTICE, "Stopping pipeline"); - gst_element_set_state (pipeline, GST_STATE_NULL); + gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(GST_OBJECT(pipeline)); return 0; @@ -160,45 +241,37 @@ int main(int argc, char* argv[]) options_t opt; int ret = options_parse(&opt, argc, argv); if(ret) { - if(ret > 0) - fprintf(stderr, "syntax error near: %s\n\n", argv[ret]); - if(ret == -2) - fprintf(stderr, "memory error on options_parse, exitting\n"); - if(ret == -3) + if(ret == -1) { options_print_version(); - if(ret == -4) - fprintf(stderr, "value out of range\n"); - - if(ret != -2 && ret != -3 && ret != -4) - options_print_usage(); - - if(ret == -1 || ret == -3) ret = 0; - + } options_clear(&opt); log_close(); exit(ret); } - slist_element_t* tmp = opt.log_targets_.first_; - while(tmp) { - ret = log_add_target(tmp->data_); + + guint len = g_strv_length(opt.log_targets_); + guint i; + for(i = 0; i < len; ++i) { + ret = log_add_target(opt.log_targets_[i]); if(ret) { switch(ret) { case -2: fprintf(stderr, "memory error on log_add_target, exitting\n"); break; - case -3: fprintf(stderr, "unknown log target: '%s', exitting\n", (char*)(tmp->data_)); break; - case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", (char*)(tmp->data_)); break; - default: fprintf(stderr, "syntax error near: '%s', exitting\n", (char*)(tmp->data_)); break; + case -3: fprintf(stderr, "unknown log target: '%s', exitting\n", (char*)(opt.log_targets_[i])); break; + case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", (char*)(opt.log_targets_[i])); break; + default: fprintf(stderr, "syntax error near: '%s', exitting\n", (char*)(opt.log_targets_[i])); break; } options_clear(&opt); log_close(); exit(ret); } - tmp = tmp->next_; } log_printf(NOTICE, "just started..."); - options_parse_post(&opt); + + if(opt.debug_) + options_print(&opt); priv_info_t priv; if(opt.username_) @@ -241,7 +314,6 @@ int main(int argc, char* argv[]) fclose(pid_file); } - gst_init(NULL, NULL); const gchar *nano_str; guint major, minor, micro, nano; gst_version(&major, &minor, µ, &nano); diff --git a/src/options.c b/src/options.c index e8de727..e730073 100644 --- a/src/options.c +++ b/src/options.c @@ -29,274 +29,178 @@ #include "options.h" #include "log.h" -#include <stdlib.h> #include <stdio.h> #include <string.h> -#include <ctype.h> +#include <glib.h> +#include <gst/gst.h> +#include <locale.h> -#define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - VALUE = 1; - -#define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - VALUE = 0; - -#define PARSE_INT_PARAM(SHORT, LONG, VALUE, BASE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1) \ - return i; \ - VALUE = strtol(argv[i+1], (char **)NULL, BASE); \ - argc--; \ - i++; \ - } - -#define PARSE_STRING_PARAM(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - if(VALUE) free(VALUE); \ - VALUE = strdup(argv[i+1]); \ - if(!VALUE) \ - return -2; \ - argc--; \ - i++; \ - } - -#define PARSE_STRING_PARAM_SEC(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - if(VALUE) free(VALUE); \ - VALUE = strdup(argv[i+1]); \ - if(!VALUE) \ - return -2; \ - size_t j; \ - for(j=0; j < strlen(argv[i+1]); ++j) \ - argv[i+1][j] = '#'; \ - argc--; \ - i++; \ - } - -#define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - int ret; \ - ret = options_parse_hex_string(argv[i+1], &VALUE); \ - if(ret > 0) \ - return i+1; \ - else if(ret < 0) \ - return ret; \ - size_t j; \ - for(j=0; j < strlen(argv[i+1]); ++j) \ - argv[i+1][j] = '#'; \ - argc--; \ - i++; \ - } - -#define PARSE_STRING_LIST(SHORT, LONG, LIST) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - int ret = string_list_add(&LIST, argv[i+1]); \ - if(ret == -2) \ - return ret; \ - else if(ret) \ - return i+1; \ - argc--; \ - i++; \ - } - -#define PARSE_RESOLV_TYPE(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - if(!strcmp(argv[i+1], "4") || \ - !strcmp(argv[i+1], "ipv4")) \ - VALUE = IPV4_ONLY; \ - else if(!strcmp(argv[i+1], "6") || \ - !strcmp(argv[i+1], "ipv6")) \ - VALUE = IPV6_ONLY; \ - else \ - return i+1; \ - argc--; \ - i++; \ - } - -int options_parse_hex_string(const char* hex, buffer_t* buffer) +static void options_defaults(options_t* opt) { - if(!hex || !buffer) - return -1; + if(!opt) + return; - uint32_t hex_len = strlen(hex); - if(hex_len%2) - return 1; + opt->progname_ = g_strdup("sydra-rtp"); + opt->daemonize_ = TRUE; + opt->username_ = NULL; + opt->groupname_ = NULL; + opt->chroot_dir_ = NULL; + opt->pid_file_ = NULL; + opt->log_targets_ = NULL; + opt->debug_ = FALSE; - if(buffer->buf_) - free(buffer->buf_); + opt->adapter_ = 0; + opt->frontend_ = 0; + opt->frequency_ = 514000000; + opt->polarity_ = g_strdup("H"); + opt->pids_ = g_strdup("5010:5011"); + opt->host_ = NULL; + opt->port_ = g_strdup("80"); +} - buffer->length_ = hex_len/2; - buffer->buf_ = malloc(buffer->length_); - if(!buffer->buf_) { - buffer->length_ = 0; - return -2; - } +static GQuark options_error_quark() +{ + static GQuark quark = 0; + if (!quark) + quark = g_quark_from_static_string("sydra_options_error"); - const char* ptr = hex; - int i; - for(i=0;i<buffer->length_;++i) { - uint32_t tmp; - sscanf(ptr, "%2X", &tmp); - buffer->buf_[i] = (uint8_t)tmp; - ptr += 2; - } + return quark; +} - return 0; +static gboolean options_parse_remaining(const gchar *option_name, const gchar *value, gpointer data, GError **error) +{ + g_set_error(error, options_error_quark(), G_OPTION_ERROR_FAILED, "unkown option '%s'", value); + return FALSE; } +static int options_parse_post(options_t* opt); int options_parse(options_t* opt, int argc, char* argv[]) { if(!opt) return -1; - options_default(opt); + setlocale (LC_ALL, ""); + options_defaults(opt); - if(opt->progname_) - free(opt->progname_); - opt->progname_ = strdup(argv[0]); + g_free(opt->progname_); + opt->progname_ = g_strdup(argv[0]); if(!opt->progname_) - return -2; + return -127; + + gboolean show_version = FALSE; + GOptionEntry main_entries[] = { + { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version, + "print version info and exit", NULL }, + { "nodaemonize", 'D', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt->daemonize_, + "don't run in background", NULL }, + { "username", 'u', 0, G_OPTION_ARG_STRING, &opt->username_, + "change to this user", "USERNAME" }, + { "group", 'g', 0, G_OPTION_ARG_STRING, &opt->groupname_, + "change to this group", "GROUP" }, + { "chroot", 'C', 0, G_OPTION_ARG_STRING, &opt->chroot_dir_, + "chroot to this directory", "PATH" }, + { "write-pid", 'P', 0, G_OPTION_ARG_STRING, &opt->pid_file_, + "write pid to this file", "PATH" }, + { "log", 'L', 0, G_OPTION_ARG_STRING_ARRAY, &opt->log_targets_, + "add a log target, can be invoked several times", "<TARGET>:<LEVEL>[,<PARAM1>[,<PARAM2>..]]" }, + { "debug", 'U', 0, G_OPTION_ARG_NONE, &opt->debug_, + "don't daemonize and log to stdout with maximum log level", NULL }, + { "adapter", 'a', 0, G_OPTION_ARG_INT, &opt->adapter_, + "the DVB adapter to use", "NUM" }, + { "frontend", 'F', 0, G_OPTION_ARG_INT, &opt->frontend_, + "the frontend of the DVB adapter to use", "NUM" }, + { "frequency", 'f', 0, G_OPTION_ARG_INT, &opt->frequency_, + "the frequency to tune to", "Hz" }, + { "polarity", 'o', 0, G_OPTION_ARG_STRING, &opt->polarity_, + "the polarity of the signal", "(H|V)" }, + { "pids", 'i', 0, G_OPTION_ARG_STRING, &opt->pids_, + "the pids of the stream", "PID:PID" }, + { "host", 'H', 0, G_OPTION_ARG_STRING, &opt->host_, + "the local interface to bind to", "ADDR" }, + { "port", 'p', 0, G_OPTION_ARG_STRING, &opt->port_, + "the port to listen to", "NUM" }, + { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, options_parse_remaining, NULL, NULL}, + { NULL } + }; + GOptionContext *ctx = g_option_context_new("- spreadspace streaming hydra "); + GOptionGroup* main_group = g_option_group_new ("main", "Application Options", + "Show Application Options", opt, NULL); + if(main_group) + g_option_group_add_entries(main_group, main_entries); + + GOptionGroup* gst_group = gst_init_get_option_group(); + if(!main_group || !gst_group) { + printf("ERROR: Failed to initialize: memory error\n"); + return -127; + } + g_option_context_set_main_group(ctx, main_group); + g_option_context_add_group(ctx, gst_group); - argc--; + GError *err = NULL; + if(!g_option_context_parse(ctx, &argc, &argv, &err)) { + printf("ERROR: Failed to initialize: %s\n", err->message); + g_error_free(err); + return 1; + } - int i; - for(i=1; argc > 0; ++i) - { - char* str = argv[i]; - argc--; + if(show_version) + return -1; - if(!strcmp(str,"-h") || !strcmp(str,"--help")) - return -1; - else if(!strcmp(str,"-v") || !strcmp(str,"--version")) - return -3; - PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", opt->daemonize_) - PARSE_STRING_PARAM("-u","--username", opt->username_) - PARSE_STRING_PARAM("-g","--groupname", opt->groupname_) - PARSE_STRING_PARAM("-C","--chroot", opt->chroot_dir_) - PARSE_STRING_PARAM("-P","--write-pid", opt->pid_file_) - PARSE_STRING_LIST("-L","--log", opt->log_targets_) - PARSE_BOOL_PARAM("-U", "--debug", opt->debug_) - PARSE_INT_PARAM("-a","--adapter", opt->adapter_, 10) - PARSE_INT_PARAM("-F","--frontend", opt->frontend_, 10) - PARSE_INT_PARAM("-f","--frequency", opt->frequency_, 10) - PARSE_STRING_PARAM("-o","--polarity", opt->polarity_) - PARSE_STRING_PARAM("-i","--pids", opt->pids_) - PARSE_STRING_PARAM("-H","--host", opt->host_) - PARSE_STRING_PARAM("-p","--port", opt->port_) + return options_parse_post(opt); +} - else - return i; +static int options_parse_post(options_t* opt) +{ + if(opt->adapter_ < 0) { + printf("ERROR: adapter must be a >= 0!\n"); + return -2; + } + if(opt->frontend_ < 0) { + printf("ERROR: frontend must be a >= 0!\n"); + return -2; + } + if(opt->frequency_ < 0) { + printf("ERROR: frequency must be a >= 0!\n"); + return -2; } - - if(opt->frequency_ < 0 || opt->adapter_ < 0 || opt->frontend_ < 0) - return -4; if(opt->debug_) { - string_list_add(&opt->log_targets_, "stdout:5"); opt->daemonize_ = 0; + g_strfreev(opt->log_targets_); + opt->log_targets_ = g_new(gchar*, 2); + if(!opt->log_targets_) return -127; + opt->log_targets_[0] = g_strdup("stdout:5"); + if(!(opt->log_targets_[0])) return -127; + opt->log_targets_[1] = NULL; } - if(!opt->log_targets_.first_) { - string_list_add(&opt->log_targets_, "syslog:3,gstdvbbackend,daemon"); + if(!opt->log_targets_ || !g_strv_length(opt->log_targets_)) { + opt->log_targets_ = g_new(gchar*, 2); + if(!opt->log_targets_) return -127; + opt->log_targets_[0] = g_strdup("syslog:3,sydra-launch,daemon"); + if(!(opt->log_targets_[0])) return -127; + opt->log_targets_[1] = NULL; } return 0; } -void options_parse_post(options_t* opt) -{ - if(!opt) - return; - -// nothing yet -} - -void options_default(options_t* opt) -{ - if(!opt) - return; - - opt->progname_ = strdup("gstdvbbackend"); - opt->daemonize_ = 1; - opt->username_ = NULL; - opt->groupname_ = NULL; - opt->chroot_dir_ = NULL; - opt->pid_file_ = NULL; - string_list_init(&opt->log_targets_); - opt->debug_ = 0; - opt->adapter_ = 0; - opt->frontend_ = 0; - opt->frequency_ = 514000000; - opt->polarity_ = strdup("H"); - opt->pids_ = strdup("5010:5011"); - opt->host_ = NULL; - opt->port_ = strdup("80"); -} - void options_clear(options_t* opt) { if(!opt) return; - if(opt->progname_) - free(opt->progname_); - if(opt->username_) - free(opt->username_); - if(opt->groupname_) - free(opt->groupname_); - if(opt->chroot_dir_) - free(opt->chroot_dir_); - if(opt->pid_file_) - free(opt->pid_file_); - string_list_clear(&opt->log_targets_); - if(opt->polarity_) - free(opt->polarity_); - if(opt->pids_) - free(opt->pids_); - if(opt->host_) - free(opt->host_); - if(opt->port_) - free(opt->port_); -} - -void options_print_usage() -{ - printf("USAGE:\n"); - printf("gstdvbbackend [-h|--help] prints this...\n"); - printf(" [-v|--version] print version info and exit\n"); - printf(" [-D|--nodaemonize] don't run in background\n"); - printf(" [-u|--username] <username> change to this user\n"); - printf(" [-g|--groupname] <groupname> change to this group\n"); - printf(" [-C|--chroot] <path> chroot to this directory\n"); - printf(" [-P|--write-pid] <path> write pid to this file\n"); - printf(" [-L|--log] <target>:<level>[,<param1>[,<param2>..]]\n"); - printf(" add a log target, can be invoked several times\n"); - printf(" [-U|--debug] don't daemonize and log to stdout with maximum log level\n"); - printf(" [-a|--adapter] <num> the dvb adapter to use (default 0)\n"); - printf(" [-F|--frontend] <num> the dvb adapter to use (default 0)\n"); - printf(" [-f|--frequency <hertz> the frequency to tune to (default 514000000)\n"); - printf(" [-o|--polarity (H,V) polarity of the signal (default H)\n"); - printf(" [-i|--pids <pid:pid> the pids of the stream (default 5010:5011)\n"); - printf(" [-H|--host <addr> the local interface to bind to (default any interface)\n"); - printf(" [-p|--port <port> the port to listen to (default 80)\n"); + g_free(opt->progname_); + g_free(opt->username_); + g_free(opt->groupname_); + g_free(opt->chroot_dir_); + g_free(opt->pid_file_); + g_strfreev(opt->log_targets_); + g_free(opt->polarity_); + g_free(opt->pids_); + g_free(opt->host_); + g_free(opt->port_); } void options_print_version() @@ -309,6 +213,16 @@ void options_print_version() #else printf("%s\n", VERSION_STRING_1); #endif + const gchar *nano_str; + guint major, minor, micro, nano; + gst_version(&major, &minor, µ, &nano); + if (nano == 1) + nano_str = " (CVS)"; + else if (nano == 2) + nano_str = " (Prerelease)"; + else + nano_str = ""; + printf(" linked against GStreamer %d.%d.%d%s\n", major, minor, micro, nano_str); } void options_print(options_t* opt) @@ -316,20 +230,24 @@ void options_print(options_t* opt) if(!opt) return; - printf("progname: '%s'\n", opt->progname_); - printf("daemonize: %d\n", opt->daemonize_); - printf("username: '%s'\n", opt->username_); - printf("groupname: '%s'\n", opt->groupname_); - printf("chroot_dir: '%s'\n", opt->chroot_dir_); - printf("pid_file: '%s'\n", opt->pid_file_); - printf("log_targets: \n"); - string_list_print(&opt->log_targets_, " '", "'\n"); - printf("debug: %s\n", !opt->debug_ ? "false" : "true"); - printf("adapter: %d\n", opt->adapter_); - printf("frontend: %d\n", opt->frontend_); - printf("frequency: %d\n", opt->frequency_); - printf("polarity: '%s'\n", opt->polarity_); - printf("pids: '%s'\n", opt->pids_); - printf("host: '%s'\n", opt->host_); - printf("port: '%s'\n", opt->port_); + printf(" progname: '%s'\n", opt->progname_); + printf(" daemonize: %s\n", opt->daemonize_ ? "true" : "false"); + printf(" username: '%s'\n", opt->username_); + printf(" groupname: '%s'\n", opt->groupname_); + printf(" chroot_dir: '%s'\n", opt->chroot_dir_); + printf(" pid_file: '%s'\n", opt->pid_file_); + printf(" log_targets: \n"); + gchar* lt = opt->log_targets_ ? g_strjoinv ("'\n '", opt->log_targets_) : NULL; + if(lt) { + printf(" '%s'\n", lt); + g_free(lt); + } + printf(" debug: %s\n", opt->debug_ ? "true" : "false"); + printf(" adapter: %d\n", opt->adapter_); + printf(" frontend: %d\n", opt->frontend_); + printf(" frequency: %d\n", opt->frequency_); + printf(" polarity: '%s'\n", opt->polarity_); + printf(" pids: '%s'\n", opt->pids_); + printf(" host: '%s'\n", opt->host_); + printf(" port: '%s'\n", opt->port_); } diff --git a/src/options.h b/src/options.h index 28857fe..a170df6 100644 --- a/src/options.h +++ b/src/options.h @@ -26,36 +26,30 @@ #ifndef GSTDVBBACKEND_options_h_INCLUDED #define GSTDVBBACKEND_options_h_INCLUDED -#include <sys/types.h> -#include "string_list.h" #include "datatypes.h" struct options_struct { - char* progname_; - int daemonize_; - char* username_; - char* groupname_; - char* chroot_dir_; - char* pid_file_; - string_list_t log_targets_; - int debug_; - int adapter_; - int frontend_; - int frequency_; - char* polarity_; - char* pids_; - char* host_; - char* port_; + gchar* progname_; + gboolean daemonize_; + gchar* username_; + gchar* groupname_; + gchar* chroot_dir_; + gchar* pid_file_; + gchar** log_targets_; + gboolean debug_; + + gint adapter_; + gint frontend_; + gint frequency_; + gchar* polarity_; + gchar* pids_; + gchar* host_; + gchar* port_; }; typedef struct options_struct options_t; -int options_parse_hex_string(const char* hex, buffer_t* buffer); - int options_parse(options_t* opt, int argc, char* argv[]); -void options_parse_post(options_t* opt); -void options_default(options_t* opt); void options_clear(options_t* opt); -void options_print_usage(); void options_print_version(); void options_print(options_t* opt); diff --git a/src/slist.c b/src/slist.c deleted file mode 100644 index 249a3cf..0000000 --- a/src/slist.c +++ /dev/null @@ -1,128 +0,0 @@ -/* - * gstdvbbackend - * - * gstdvbbackend is a small programm which captures a given set of dvb - * channels from one dvb device and provides the streams via minimal http. - * - * - * Copyright (C) 2011-2016 Christian Pointner <equinox@spreadspace.org> - * - * This file is part of gstdvbbackend. - * - * gstdvbbackend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * gstdvbbackend is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with gstdvbbackend. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <unistd.h> -#include <stdlib.h> - -#include "datatypes.h" - -#include "slist.h" - -slist_element_t* slist_get_last(slist_element_t* first) -{ - if(!first) - return NULL; - - while(first->next_) - first = first->next_; - - return first; -} - -int slist_init(slist_t* lst, void (*delete_element)(void*)) -{ - if(!lst || !delete_element) - return -1; - - lst->delete_element = delete_element; - lst->first_ = NULL; - - return 0; -} - -slist_element_t* slist_add(slist_t* lst, void* data) -{ - if(!lst || !data) - return NULL; - - slist_element_t* new_element = malloc(sizeof(slist_element_t)); - if(!new_element) - return NULL; - - new_element->data_ = data; - new_element->next_ = NULL; - - if(!lst->first_) - lst->first_ = new_element; - else - slist_get_last(lst->first_)->next_ = new_element; - - return new_element; -} - -void slist_remove(slist_t* lst, void* data) -{ - if(!lst || !lst->first_ || !data) - return; - - slist_element_t* tmp = lst->first_->next_; - slist_element_t* prev = lst->first_; - if(lst->first_->data_ == data) { - lst->first_ = tmp; - lst->delete_element(prev->data_); - free(prev); - } - else { - while(tmp) { - if(tmp->data_ == data) { - prev->next_ = tmp->next_; - lst->delete_element(tmp->data_); - free(tmp); - return; - } - prev = tmp; - tmp = tmp->next_; - } - } -} - -void slist_clear(slist_t* lst) -{ - if(!lst || !lst->first_) - return; - - do { - slist_element_t* deletee = lst->first_; - lst->first_ = lst->first_->next_; - lst->delete_element(deletee->data_); - free(deletee); - } - while(lst->first_); - - lst->first_ = NULL; -} - -int slist_length(slist_t* lst) -{ - if(!lst || !lst->first_) - return 0; - - int len = 0; - slist_element_t* tmp; - for(tmp = lst->first_; tmp; tmp = tmp->next_) - len++; - - return len; -} diff --git a/src/slist.h b/src/slist.h deleted file mode 100644 index acc8d1a..0000000 --- a/src/slist.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * gstdvbbackend - * - * gstdvbbackend is a small programm which captures a given set of dvb - * channels from one dvb device and provides the streams via minimal http. - * - * - * Copyright (C) 2011-2016 Christian Pointner <equinox@spreadspace.org> - * - * This file is part of gstdvbbackend. - * - * gstdvbbackend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * gstdvbbackend is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with gstdvbbackend. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef GSTDVBBACKEND_slist_h_INCLUDED -#define GSTDVBBACKEND_slist_h_INCLUDED - -struct slist_element_struct { - void* data_; - struct slist_element_struct* next_; -}; -typedef struct slist_element_struct slist_element_t; - -slist_element_t* slist_get_last(slist_element_t* first); - -struct slist_struct { - void (*delete_element)(void* element); - slist_element_t* first_; -}; -typedef struct slist_struct slist_t; - -int slist_init(slist_t* lst, void (*delete_element)(void*)); -slist_element_t* slist_add(slist_t* lst, void* data); -void slist_remove(slist_t* lst, void* data); -void slist_clear(slist_t* lst); -int slist_length(slist_t* lst); - -#endif diff --git a/src/string_list.c b/src/string_list.c deleted file mode 100644 index c1beed2..0000000 --- a/src/string_list.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * gstdvbbackend - * - * gstdvbbackend is a small programm which captures a given set of dvb - * channels from one dvb device and provides the streams via minimal http. - * - * - * Copyright (C) 2011-2016 Christian Pointner <equinox@spreadspace.org> - * - * This file is part of gstdvbbackend. - * - * gstdvbbackend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * gstdvbbackend is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with gstdvbbackend. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <string.h> -#include <stdlib.h> -#include <stdio.h> - -#include "string_list.h" -#include "slist.h" - -int string_list_init(string_list_t* list) -{ - return slist_init(list, &free); -} - -void string_list_clear(string_list_t* list) -{ - slist_clear(list); -} - -int string_list_add(string_list_t* list, const char* string) -{ - if(!list) - return -1; - - char* tmp = strdup(string); - if(slist_add(list, tmp) == NULL) { - free(tmp); - return -2; - } - - return 0; -} - -void string_list_print(string_list_t* list, const char* head, const char* tail) -{ - if(!list) - return; - - slist_element_t* tmp = list->first_; - while(tmp) { - printf("%s%s%s", head, (char*)(tmp->data_), tail); - tmp = tmp->next_; - } -} diff --git a/src/string_list.h b/src/string_list.h deleted file mode 100644 index 7f4c067..0000000 --- a/src/string_list.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * gstdvbbackend - * - * gstdvbbackend is a small programm which captures a given set of dvb - * channels from one dvb device and provides the streams via minimal http. - * - * - * Copyright (C) 2011-2016 Christian Pointner <equinox@spreadspace.org> - * - * This file is part of gstdvbbackend. - * - * gstdvbbackend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * gstdvbbackend is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with gstdvbbackend. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef GSTDVBBACKEND_string_list_h_INCLUDED -#define GSTDVBBACKEND_string_list_h_INCLUDED - -#include "slist.h" - -typedef slist_t string_list_t; - -int string_list_init(string_list_t* list); -void string_list_clear(string_list_t* list); -int string_list_add(string_list_t* list, const char* string); - -void string_list_print(string_list_t* list, const char* head, const char* tail); - -#endif diff --git a/src/sysexec.c b/src/sysexec.c deleted file mode 100644 index c248ec6..0000000 --- a/src/sysexec.c +++ /dev/null @@ -1,223 +0,0 @@ -/* - * gstdvbbackend - * - * gstdvbbackend is a small programm which captures a given set of dvb - * channels from one dvb device and provides the streams via minimal http. - * - * - * Copyright (C) 2011-2016 Christian Pointner <equinox@spreadspace.org> - * - * This file is part of gstdvbbackend. - * - * gstdvbbackend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * gstdvbbackend is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with gstdvbbackend. If not, see <http://www.gnu.org/licenses/>. - */ - -#include "datatypes.h" - -#include <unistd.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <errno.h> -#include <sys/wait.h> -#include <sys/select.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "sysexec.h" -#include "log.h" - -char** dup_ptrptr(char* const ptrptr[]) -{ - if(!ptrptr) - return NULL; - - int n = 0; - while(ptrptr[n]) - n++; - - char** my_ptrptr; - my_ptrptr = malloc((n+1)*sizeof(char*)); - if(!my_ptrptr) - return NULL; - - int i; - for(i = 0; i < n; ++i) { - my_ptrptr[i] = strdup(ptrptr[i]); - if(!my_ptrptr[i]) { - i--; - for(; i >= 0; --i) - free(my_ptrptr[i]); - - free(my_ptrptr); - return NULL; - } - } - - my_ptrptr[n] = NULL; - - return my_ptrptr; -} - -void free_ptrptr(char** ptrptr) -{ - if(!ptrptr) - return; - - int i; - for(i = 0; ptrptr[i]; ++i) - free(ptrptr[i]); - - free(ptrptr); -} - -child_t* new_child(const char* script, char* const argv[], char* const evp[]) -{ - child_t* new_child; - - new_child = malloc(sizeof(child_t)); - if(!new_child) - return NULL; - - new_child->pid_ = -1; - new_child->err_fd_ = -1; - new_child->script_ = strdup(script); - if(!new_child->script_) { - free(new_child); - return NULL; - } - - new_child->argv_ = dup_ptrptr(argv); - if(!new_child->argv_) { - free(new_child->script_); - free(new_child); - return NULL; - - } - - new_child->evp_ = dup_ptrptr(evp); - if(!new_child->evp_) { - free_ptrptr(new_child->argv_); - free(new_child->script_); - free(new_child); - return NULL; - } - return new_child; -} - -void free_child(child_t* child) -{ - if(!child) - return; - - free_ptrptr(child->argv_); - free_ptrptr(child->evp_); - if(child->script_) - free(child->script_); - if(child->err_fd_ >= 0) close(child->err_fd_); - free(child); -} - -child_t* rh_exec(const char* script, char* const argv[], char* const evp[]) -{ - if(!script) - return NULL; - - child_t* child = new_child(script, argv, evp); - if(!child) - return NULL; - - int pipefd[2]; - if(pipe(pipefd) == -1) { - log_printf(ERROR, "executing script '%s' failed: pipe() error: %s", child->script_, strerror(errno)); // TODO: thread safe strerror - free_child(child); - return NULL; - } - - pid_t pid; - pid = fork(); - if(pid == -1) { - log_printf(ERROR, "executing script '%s' failed: fork() error: %s", child->script_, strerror(errno)); // TODO: thread safe strerror - close(pipefd[0]); - close(pipefd[1]); - free_child(child); - return NULL; - } - - if(!pid) { - int fd; - for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors - if(fd != pipefd[1]) close(fd); - - fd = open("/dev/null",O_RDWR); // stdin - if(fd == -1) - log_printf(WARNING, "can't open stdin"); - else { - if(dup(fd) == -1) // stdout - log_printf(WARNING, "can't open stdout"); - if(dup(fd) == -1) // stderr - log_printf(WARNING, "can't open stderr"); - } - execve(child->script_, child->argv_, child->evp_); - // if execve returns, an error occurred, but logging doesn't work - // because we closed all file descriptors, so just write errno to - // pipe and call exit - int ret = write(pipefd[1], (void*)(&errno), sizeof(errno)); - if(ret == -1) exit(-1); - exit(-1); - } - close(pipefd[1]); - - child->pid_ = pid; - child->err_fd_ = pipefd[0]; - - log_printf(INFO, "called script '%s' with pid %d", child->script_, child->pid_); - - return child; -} - -int rh_waitpid(child_t* child, int* status_return) -{ - int status = 0; - pid_t pid = waitpid(child->pid_, &status, WNOHANG); - if(!pid || (pid < 0 && errno == ECHILD)) - return 0; - if(pid < 0) { - log_printf(ERROR, "waitpid returned with error: %s", strerror(errno)); // TODO: thread safe strerror - return pid; - } - - fd_set rfds; - FD_ZERO(&rfds); - FD_SET(child->err_fd_, &rfds); - struct timeval tv = { 0 , 0 }; - if(select(child->err_fd_+1, &rfds, NULL, NULL, &tv) == 1) { - int err = 0; - if(read(child->err_fd_, (void*)(&err), sizeof(err)) >= sizeof(err)) { - log_printf(INFO, "script '%s' exec() error: %s", child->script_, strerror(err)); // TODO: thread safe strerror - return -1; - } - } - if(WIFEXITED(status)) - log_printf(INFO, "script '%s' (pid %d) returned %d", child->script_, child->pid_, WEXITSTATUS(status)); - else if(WIFSIGNALED(status)) - log_printf(INFO, "script '%s' (pid %d) terminated after signal %d", child->script_, child->pid_, WTERMSIG(status)); - else - log_printf(INFO, "executing script '%s' (pid %d): unkown error", child->script_, child->pid_); - - if(status_return) *status_return = status; - - return 1; -} diff --git a/src/sysexec.h b/src/sysexec.h deleted file mode 100644 index 3402581..0000000 --- a/src/sysexec.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * gstdvbbackend - * - * gstdvbbackend is a small programm which captures a given set of dvb - * channels from one dvb device and provides the streams via minimal http. - * - * - * Copyright (C) 2011-2016 Christian Pointner <equinox@spreadspace.org> - * - * This file is part of gstdvbbackend. - * - * gstdvbbackend is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * gstdvbbackend is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with gstdvbbackend. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef GSTDVBBACKEND_sysexec_h_INCLUDED -#define GSTDVBBACKEND_sysexec_h_INCLUDED - -#include <sys/types.h> -#include "options.h" - -struct child_struct { - pid_t pid_; - char* script_; - int err_fd_; - char** argv_; - char** evp_; -}; -typedef struct child_struct child_t; - -child_t* new_child(const char* script, char* const argv[], char* const evp[]); -void free_child(child_t* child); - -child_t* rh_exec(const char* script, char* const argv[], char* const evp[]); -int rh_waitpid(child_t* child, int* status); - -#endif |