/* * gcsd * * gcsd the generic command sequencer daemon can be used to serialize * commands sent over various paralell communication channels to a * single command output. It can be seen as a multiplexer for any * kind of communication between a single resource and various clients * which want to submit commands to it or query information from it. * gcsd is written in C and Lua. The goal is to provide an easy to * understand high level API based on Lua which can be used to * implement the business logic of the so formed multiplexer daemon. * * * Copyright (C) 2009-2010 Markus Grueneis * Christian Pointner * * This file is part of gcsd. * * gcsd 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. * * gcsd 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 gcsd. If not, see . */ #include #include #include "datatypes.h" #include "config.h" #include "options.h" #include #include #include #include #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) \ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ { \ if(argc < 1) \ return i; \ VALUE = atoi(argv[i+1]); \ 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++; \ } int options_parse_hex_string(const char* hex, buffer_t* buffer) { if(!hex || !buffer) return -1; uint32_t hex_len = strlen(hex); if(hex_len%2) return 1; if(buffer->buf_) free(buffer->buf_); buffer->length_ = hex_len/2; buffer->buf_ = malloc(buffer->length_); if(!buffer->buf_) { buffer->length_ = 0; return -2; } const char* ptr = hex; int i; for(i=0;ilength_;++i) { uint32_t tmp; sscanf(ptr, "%2X", &tmp); buffer->buf_[i] = (uint8_t)tmp; ptr += 2; } return 0; } int options_parse(options_t* opt, int argc, char* argv[]) { if(!opt) return -1; options_default(opt); if(opt->progname_) free(opt->progname_); opt->progname_ = strdup(argv[0]); if(!opt->progname_) return -2; argc--; int i; for(i=1; argc > 0; ++i) { char* str = argv[i]; argc--; 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_STRING_PARAM("-l","--lua-code", opt->lua_code_) PARSE_STRING_PARAM("-o","--output", opt->cmd_out_) PARSE_STRING_LIST("-i","--input", opt->cmd_ins_) else return i; } if(opt->debug_) { string_list_add(&opt->log_targets_, "stdout:5"); opt->daemonize_ = 0; } if(!opt->log_targets_.first_) { #ifndef WINVER string_list_add(&opt->log_targets_, "syslog:3,gcsd,daemon"); #else string_list_add(&opt->log_targets_, "stdout:3"); #endif } if(!opt->cmd_ins_.first_) { #ifndef WINVER string_list_add(&opt->cmd_ins_, "unix:file=/var/run/gcsd.sock,mode=660"); #else string_list_add(&opt->cmd_ins_, "tcp:host=127.0.0.1,port=1234"); #endif } return 0; } void options_parse_post(options_t* opt) { if(!opt) return; if(opt->debug_) string_list_add(&opt->cmd_ins_, "debug_shell:name=debug"); } void options_default(options_t* opt) { if(!opt) return; opt->progname_ = strdup("gcsd"); opt->daemonize_ = 1; opt->username_ = NULL; opt->groupname_ = NULL; opt->chroot_dir_ = NULL; opt->pid_file_ = NULL; string_list_init(&opt->log_targets_); opt->lua_code_ = NULL; #ifndef WINVER opt->cmd_out_ = strdup("serial:device=/dev/ttyS0,baudrate=9600,data=8,stop=1,parity=no"); #else opt->cmd_out_ = strdup("serial:device=COM0,baudrate=9600,data=8,stop=1,parity=no"); #endif string_list_init(&opt->cmd_ins_); opt->debug_ = 0; } void options_lua_push_string(lua_State* L, const int tidx, const char* key, const char* value) { lua_pushstring(L, key); lua_pushstring(L, value); lua_settable(L, tidx); } void options_lua_push_int(lua_State* L, const int tidx, const char* key, const uint32_t value) { lua_pushstring(L, key); lua_pushinteger(L, value); lua_settable(L, tidx); } void options_lua_push_boolean(lua_State* L, const int tidx, const char* key, const uint32_t value) { lua_pushstring(L, key); lua_pushboolean(L, value); lua_settable(L, tidx); } void options_lua_push_string_list(lua_State* L, const int tidx, const char* name, string_list_t* lst) { if(!lst) return; string_list_element_t* tmp = lst->first_; if(tmp) { lua_pushstring(L, name); lua_newtable(L); int i = 1; while(tmp) { lua_pushinteger(L, i++); lua_pushstring(L, tmp->string_); lua_settable(L, -3); tmp = tmp->next_; } lua_settable(L, tidx); } } void options_lua_push(options_t* opt, lua_State* L) { lua_newtable(L); options_lua_push_string(L, -3, "progname", opt->progname_); options_lua_push_boolean(L, -3, "daemonize", opt->daemonize_); options_lua_push_string(L, -3, "username", opt->username_); options_lua_push_string(L, -3, "groupname", opt->groupname_); options_lua_push_string(L, -3, "chroot_dir", opt->chroot_dir_); options_lua_push_string(L, -3, "pid_file", opt->pid_file_); options_lua_push_string_list(L, -3, "log_targets", &(opt->log_targets_)); options_lua_push_boolean(L, -3, "debug", opt->debug_); options_lua_push_string(L, -3, "lua_code", opt->lua_code_); options_lua_push_string(L, -3, "cmd_out", opt->cmd_out_); options_lua_push_string_list(L, -3, "cmd_ins", &(opt->cmd_ins_)); } 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->lua_code_) free(opt->lua_code_); if(opt->cmd_out_) free(opt->cmd_out_); string_list_clear(&opt->cmd_ins_); } void options_print_usage() { printf("USAGE:\n"); printf("gcsd [-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] change to this user\n"); printf(" [-g|--groupname] change to this group\n"); printf(" [-C|--chroot] chroot to this directory\n"); printf(" [-P|--write-pid] write pid to this file\n"); printf(" [-L|--log] :[,[,..]]\n"); printf(" add a log target, can be invoked several times\n"); printf(" [-l|--lua-code] load lua code containing the dispatch table from this file\n"); printf(" [-o|--output] :[[,[,[..]]]]\n"); printf(" use this as the output module\n"); printf(" [-i|--input] :[[,[,[..]]]]\n"); printf(" add a command input module, can be invoked several times\n"); printf(" [-U|--debug] don't daemonize and log to stdout with maximum log level\n"); } void options_print_version() { printf("%s\n", VERSION_STRING_0); #if defined(__clang__) printf("%s, using CLANG %s\n", VERSION_STRING_1, __clang_version__); #elif defined(__GNUC__) printf("%s, using GCC %d.%d.%d\n", VERSION_STRING_1, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); #else printf("%s\n", VERSION_STRING_1); #endif } 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("lua_code: '%s'\n", opt->lua_code_); printf("cmd_out: '%s'\n", opt->cmd_out_); printf("cmd_ins: \n"); string_list_print(&opt->cmd_ins_, " '", "'\n"); printf("debug: %s\n", !opt->debug_ ? "false" : "true"); }