/* * rhctl * * Copyright (C) 2009-2014 Christian Pointner * * This file is part of rhctl. * * rhctl 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. * * rhctl 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 rhctl. If not, see . */ #include "datatypes.h" #include #include #include #include #include "log.h" #include "sig_handler.h" #include "options.h" #include "command_queue.h" #include "client_list.h" #include "key_value_storage.h" #include "daemon.h" #include "utils.h" int send_command(int switch_fd, cmd_t* cmd) { if(!cmd) return -1; char* c = NULL; switch(cmd->cmd) { case SWITCH: case CHANNEL: c = cmd->param; break; case STATUS: c = "*0SL"; break; default: break; } if(c == NULL) return 0; int ret = send_string(switch_fd, c); if(ret > 0) { cmd_sent(cmd); return 0; } return ret; } int send_response(int fd, const char* response) { if(!response) return -1; if(fd < 0) return 0; int ret = send_string(fd, response); do { ret = write(fd, "\n", 1); } while(!ret || (ret == -1 && errno == EINTR)); if(ret > 0) return 0; return ret; } void send_usage(int fd) { if(fd < 0) return; send_response(fd, "Usage: "); send_response(fd, " help prints this"); send_response(fd, " type set client type, one of: master, standby, heartbeat"); send_response(fd, " channel switch to channel main or music"); send_response(fd, " client type master and standby only"); send_response(fd, " mode switch to mode master or standby"); send_response(fd, " status get actual status from switch"); send_response(fd, " listen register for events, no parameter for all"); send_response(fd, " one of: request, mode, status, gpi, oc, relay, silence, none"); send_response(fd, " log add line to daemons log file"); send_response(fd, " switch send raw commands to the switch"); } int process_cmd_request(const char* cmd, cmd_id_t cmd_id, const char* param, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) { client_t* c = client_find(client_lst, fd); if(!c) { log_printf(WARNING, "ignoring request from unknown client"); send_response(fd, "EEE: switch: client not found in client list?!"); return 0; } char* cmd_param = NULL; if(cmd_id == SWITCH) { if((opt->mode_ == MODE_MASTER && c->type == STANDBY )|| (opt->mode_ == MODE_STANDBY && c->type == MASTER )) { log_printf(INFO, "silently ignoring request from inactive system (%s)", c->type == MASTER ? "master" : "standby"); return 0; } if(!param) { log_printf(INFO, "ignoring switch command without parameter"); send_response(fd, "EEE: switch: missing parameter"); return 0; } if(param[0] == '*') { cmd_param = strdup(param); } else { const char* ch_name = NULL; char* ch_nr = NULL; if(!strncmp(param, "up ", 3)) { cmd_param = strdup("*0FUii"); ch_name = &(param[3]); ch_nr = &(cmd_param[4]); } else if(!strncmp(param, "down ", 5)) { cmd_param = strdup("*0FDii"); ch_name = &(param[5]); ch_nr = &(cmd_param[4]); } else if(!strncmp(param, "select ", 7)) { cmd_param = strdup("*0ii1"); ch_name = &(param[7]); ch_nr = &(cmd_param[2]); } else if(!strncmp(param, "add ", 4)) { cmd_param = strdup("*0ii3"); ch_name = &(param[4]); ch_nr = &(cmd_param[2]); } else if(!strncmp(param, "rm ", 3)) { cmd_param = strdup("*0ii5"); ch_name = &(param[3]); ch_nr = &(cmd_param[2]); } else if(!strncmp(param, "mute ", 5)) { cmd_param = strdup("*0iiM1"); ch_name = &(param[5]); ch_nr = &(cmd_param[2]); } else if(!strncmp(param, "select2 ", 8)) { cmd_param = strdup("*0ii2"); ch_name = &(param[8]); ch_nr = &(cmd_param[2]); } else if(!strncmp(param, "add2 ", 5)) { cmd_param = strdup("*0ii4"); ch_name = &(param[5]); ch_nr = &(cmd_param[2]); } else if(!strncmp(param, "rm2 ", 4)) { cmd_param = strdup("*0ii6"); ch_name = &(param[4]); ch_nr = &(cmd_param[2]); } else if(!strncmp(param, "mute2 ", 6)) { cmd_param = strdup("*0iiM2"); ch_name = &(param[6]); ch_nr = &(cmd_param[2]); } else { log_printf(INFO, "ignoring invalid switch command: '%s'", param); send_response(fd, "EEE: switch: invalid command"); return 0; } char* ch_tmp = key_value_storage_find(&opt->alias_table_, ch_name); if(!ch_tmp || ch_tmp[0] == 0 || ch_tmp[1] == 0 || ch_tmp[2] != 0) { log_printf(ERROR, "invalid channel name or number: %s", ch_name); send_response(fd, "EEE: switch: invalid channel name or number"); free(cmd_param); return 0; } ch_nr[0] = ch_tmp[0]; ch_nr[1] = ch_tmp[1]; } log_printf(DEBUG, "enqueing command to switch: '%s' (request was: '%s')", cmd_param, param); } int ret = cmd_push(cmd_q, fd, cmd_id, cmd_param); if(cmd_param) free(cmd_param); if(ret) return ret; if(cmd_id == STATUS) { char buf[2][30]; snprintf(buf[0], 30, "Current Mode: %s", opt->mode_ == MODE_MASTER ? "Master" : "Standby"); send_response(fd, buf[0]); snprintf(buf[1], 30, "Master Channel: %s", opt->channel_master_ == CHAN_MAIN ? "Main" : "Music"); send_response(fd, buf[1]); snprintf(buf[1], 30, "Standby Channel: %s", opt->channel_standby_ == CHAN_MAIN ? "Main" : "Music"); send_response(fd, buf[1]); client_t* client; int listener_cnt = 0; for(client = client_lst; client; client = client->next) if(client->status_listener && client->fd != fd) { send_response(client->fd, buf[0]); send_response(client->fd, buf[1]); listener_cnt++; } log_printf(DEBUG, "sent status to %d additional listeners", listener_cnt); } log_printf(NOTICE, "command: %s", cmd); return 0; } int crossfade(const char* ch_from, const char* ch_to, int fd, cmd_t **cmd_q, options_t* opt) { char* cmd_param = strdup("*0FDii*0FUii"); char* ch_nr_from = key_value_storage_find(&opt->alias_table_, ch_from); if(!ch_nr_from || ch_nr_from[0] == 0 || ch_nr_from[1] == 0 || ch_nr_from[2] != 0) { log_printf(ERROR, "invalid channel name or number: %s", ch_from); send_response(fd, "EEE: channel: invalid channel name or number"); free(cmd_param); return 1; } char* ch_nr_to = key_value_storage_find(&opt->alias_table_, ch_to); if(!ch_nr_to || ch_nr_to[0] == 0 || ch_nr_to[1] == 0 || ch_nr_to[2] != 0) { log_printf(ERROR, "invalid channel name or number: %s", ch_to); send_response(fd, "EEE: channel: invalid channel name or number"); free(cmd_param); return 1; } cmd_param[4] = ch_nr_from[0]; cmd_param[5] = ch_nr_from[1]; cmd_param[10] = ch_nr_to[0]; cmd_param[11] = ch_nr_to[1]; log_printf(DEBUG, "enqueing command to switch: '%s'", cmd_param); int ret = cmd_push(cmd_q, fd, CHANNEL, cmd_param); free(cmd_param); return ret; } int process_cmd_channel(const char* cmd, const char* param, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) { client_t* c = client_find(client_lst, fd); if(!c) { log_printf(WARNING, "ignoring request from unknown client"); send_response(fd, "EEE: channel: client not found in client list?!"); return 0; } if(c->type != MASTER && c->type != STANDBY) { log_printf(WARNING, "ignoring request from client of wrong type"); send_response(fd, "EEE: channel: client type doesn't fit"); return 0; } if(!param) { log_printf(INFO, "ignoring channel command without parameter"); send_response(fd, "EEE: channel: missing parameter"); return 0; } switchctl_channel_t old_channel; if(c->type == MASTER) old_channel = opt->channel_master_; else old_channel = opt->channel_standby_; char* ch_from = NULL; char* ch_to = NULL; if(!strcmp(param, "main")) { if(opt->mode_ == MODE_MASTER) { ch_from = "master_music"; ch_to = "master_main"; } else { ch_from = "standby_music"; ch_to = "standby_main"; } if(c->type == MASTER) opt->channel_master_ = CHAN_MAIN; else opt->channel_standby_ = CHAN_MAIN; } else if(!strcmp(param, "music")) { if(opt->mode_ == MODE_MASTER) { ch_from = "master_main"; ch_to = "master_music"; } else { ch_from = "standby_main"; ch_to = "standby_music"; } if(c->type == MASTER) opt->channel_master_ = CHAN_MUSIC; else opt->channel_standby_ = CHAN_MUSIC; } if((opt->mode_ == MODE_MASTER && c->type == STANDBY )|| (opt->mode_ == MODE_STANDBY && c->type == MASTER )) { log_printf(INFO, "no crossfade for inactive system (%s), just updated channel info", c->type == MASTER ? "master" : "standby"); return 0; } int ret = crossfade(ch_from, ch_to, fd, cmd_q, opt); if(ret) { if(c->type == MASTER) opt->channel_master_ = old_channel; else opt->channel_standby_ = old_channel; if(ret > 0) return 0; return ret; } log_printf(NOTICE, "command: %s", cmd); return 0; } void process_cmd_type(const char* param, int fd, client_t* client_lst) { if(param) { client_t* client = client_find(client_lst, fd); if(client) { if(!strncmp(param, "master", 6)) { client->type = MASTER; client->gpi_listener = 1; } else if(!strncmp(param, "standby", 7)) { client->type = STANDBY; client->gpi_listener = 1; } else if(!strncmp(param, "master_hb", 9)) client->type = MASTER_HB; else if(!strncmp(param, "standby_hb", 10)) client->type = STANDBY_HB; else { log_printf(DEBUG, "unkown client type '%s'", param); send_response(fd, "EEE: type: unknown client type"); return; } log_printf(DEBUG, "client %d type set to %s", fd, param); } else { log_printf(ERROR, "unable to set client type for %d: client not found", fd); send_response(fd, "EEE: type: client not found in client list?!"); } } else { log_printf(ERROR, "unable to set client type for %d: empty parameter", fd); send_response(fd, "EEE: type: missing parameter"); } } int process_cmd_mode(const char* param, int fd, cmd_t **cmd_q, options_t* opt, client_t* client_lst) { switchctl_mode_t old_mode = opt->mode_; if(param) { if(!strncmp(param, "master", 6)) opt->mode_ = MODE_MASTER; else if(!strncmp(param, "standby", 7)) opt->mode_ = MODE_STANDBY; else { log_printf(DEBUG, "unkown mode '%s'", param); send_response(fd, "EEE: mode: unknown mode"); return 0; } // swap master with standby channels only when mode has changed if(old_mode != opt->mode_) { char* ch_from = NULL; if(old_mode == MODE_MASTER && opt->channel_master_ == CHAN_MAIN) ch_from = "master_main"; else if(old_mode == MODE_MASTER && opt->channel_master_ == CHAN_MUSIC) ch_from = "master_music"; else if(old_mode == MODE_STANDBY && opt->channel_standby_ == CHAN_MAIN) ch_from = "standby_main"; else if(old_mode == MODE_STANDBY && opt->channel_standby_ == CHAN_MUSIC) ch_from = "standby_music"; else { opt->mode_ = old_mode; log_printf(ERROR, "EEE: mode: old config is illegal?!"); return 0; } char* ch_to = NULL; if(opt->mode_ == MODE_MASTER && opt->channel_master_ == CHAN_MAIN) ch_to = "master_main"; else if(opt->mode_ == MODE_MASTER && opt->channel_master_ == CHAN_MUSIC) ch_to = "master_music"; else if(opt->mode_ == MODE_STANDBY && opt->channel_standby_ == CHAN_MAIN) ch_to = "standby_main"; else if(opt->mode_ == MODE_STANDBY && opt->channel_standby_ == CHAN_MUSIC) ch_to = "standby_music"; else { opt->mode_ = old_mode; log_printf(ERROR, "EEE: mode: current config is illegal?!"); return 0; } int ret = crossfade(ch_from, ch_to, fd, cmd_q, opt); if(ret) { opt->mode_ = old_mode; if(ret > 0) return 0; return ret; } } } else { log_printf(ERROR, "unable to set mode: empty parameter"); send_response(fd, "EEE: mode: missing parameter"); } if(old_mode != opt->mode_) { char* mode_str; int len = asprintf(&mode_str, "new Mode: %s", opt->mode_ == MODE_MASTER ? "master" : "standby"); if(len > 0) { log_printf(NOTICE, "%s", mode_str); client_t* client; int listener_cnt = 0; for(client = client_lst; client; client = client->next) if(client->mode_listener && client->fd != fd) { send_response(client->fd, mode_str); listener_cnt++; } free(mode_str); log_printf(DEBUG, "sent new mode to %d additional listeners", listener_cnt); } } return 0; } void process_cmd_listen(const char* param, int fd, client_t* client_lst) { client_t* listener = client_find(client_lst, fd); if(listener) { if(!param) { listener->request_listener = 1; listener->mode_listener = 1; listener->status_listener = 1; listener->gpi_listener = 1; listener->oc_listener = 1; listener->relay_listener = 1; listener->silence_listener = 1; } else { if(!strncmp(param, "request", 7)) listener->request_listener = 1; else if(!strncmp(param, "mode", 6)) listener->mode_listener = 1; else if(!strncmp(param, "status", 6)) listener->status_listener = 1; else if(!strncmp(param, "gpi", 3)) listener->gpi_listener = 1; else if(!strncmp(param, "oc", 2)) listener->oc_listener = 1; else if(!strncmp(param, "relay", 5)) listener->relay_listener = 1; else if(!strncmp(param, "silence", 7)) listener->silence_listener = 1; else if(!strncmp(param, "none", 4)) { listener->request_listener = 0; listener->mode_listener = 0; listener->status_listener = 0; listener->gpi_listener = 0; listener->oc_listener = 0; listener->relay_listener = 0; listener->silence_listener = 0; } else { log_printf(DEBUG, "unkown listener type '%s'", param); send_response(fd, "EEE: listen: unkown type"); return; } } log_printf(DEBUG, "listener %d requests %s messages", fd, param ? param:"all"); } else { log_printf(ERROR, "unable to add listener %d", fd); send_response(fd, "EEE: listen: client not found in client list?!"); } } int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) { log_printf(DEBUG, "processing command from %d", fd); if(!cmd_q || !cmd) return -1; cmd_id_t cmd_id; if(!strncmp(cmd, "switch", 6)) cmd_id = SWITCH; else if(!strncmp(cmd, "channel", 7)) cmd_id = CHANNEL; else if(!strncmp(cmd, "type", 4)) cmd_id = TYPE; else if(!strncmp(cmd, "mode", 4)) cmd_id = MODE; else if(!strncmp(cmd, "status", 6)) cmd_id = STATUS; else if(!strncmp(cmd, "log", 3)) cmd_id = LOG; else if(!strncmp(cmd, "listen", 6)) cmd_id = LISTEN; else { if(!strncmp(cmd, "help", 4)) { send_usage(fd); } else { log_printf(WARNING, "unknown command '%s'", cmd); send_response(fd, "EEE: unknown command"); } return 0; } char* param = strchr(cmd, ' '); if(param) param++; if(cmd_id == SWITCH || cmd_id == CHANNEL || cmd_id == MODE) { char* resp; int len = asprintf(&resp, "Request: %s", cmd); if(len > 0) { char* linefeed = strchr(resp, '\n'); if(linefeed) linefeed[0] = 0; client_t* client; int listener_cnt = 0; for(client = client_lst; client; client = client->next) if(client->request_listener && client->fd != fd) { send_response(client->fd, resp); listener_cnt++; } free(resp); log_printf(DEBUG, "sent request to %d additional listeners", listener_cnt); } // else silently ignore memory alloc error } switch(cmd_id) { case SWITCH: case STATUS: { int ret = process_cmd_request(cmd, cmd_id, param, fd, cmd_q, client_lst, opt); if(ret) return ret; break; } case CHANNEL: { int ret = process_cmd_channel(cmd, param, fd, cmd_q, client_lst, opt); if(ret) return ret; break; } case TYPE: process_cmd_type(param, fd, client_lst); break; case MODE: { int ret = process_cmd_mode(param, fd, cmd_q, opt, client_lst); if(ret) return ret; break; } case LOG: { if(param && param[0]) log_printf(NOTICE, "ext msg: %s", param); else log_printf(DEBUG, "ignoring empty ext log message"); break; } case LISTEN: process_cmd_listen(param, fd, client_lst); break; } return 0; } int nonblock_recvline(read_buffer_t* buffer, int fd, cmd_t** cmd_q, client_t* client_lst, options_t* opt) { int ret = 0; for(;;) { ret = recv(fd, &buffer->buf[buffer->offset], 1, 0); if(!ret) return 2; if(ret == -1 && errno == EAGAIN) return 0; else if(ret < 0) break; if(buffer->buf[buffer->offset] == '\n') { buffer->buf[buffer->offset] = 0; ret = process_cmd((char *)(buffer->buf), fd, cmd_q, client_lst, opt); buffer->offset = 0; break; } buffer->offset++; if(buffer->offset >= sizeof(buffer->buf)) { log_printf(DEBUG, "string too long (fd=%d)", fd); buffer->offset = 0; return 0; } } return ret; } #define SEND_TO_LISTENER(STRING, LEN, FLAG) \ if(!strncmp((char *)(buffer->buf), STRING, LEN)) { \ client_t* client; \ int listener_cnt = 0; \ for(client = client_lst; client; client = client->next) \ if(client->FLAG && client->fd != cmd_fd) { \ send_response(client->fd, (char *)(buffer->buf)); \ listener_cnt++; \ } \ log_printf(DEBUG, "sent message to %d additional listeners", listener_cnt); \ } \ int process_switch(read_buffer_t* buffer, int switch_fd, cmd_t **cmd_q, client_t* client_lst) { int ret = 0; struct timeval tv; fd_set fds; FD_ZERO(&fds); FD_SET(switch_fd, &fds); for(;;) { tv.tv_sec = 0; tv.tv_usec = 0; ret = select(switch_fd+1, &fds, NULL, NULL, &tv); if(!ret) return 0; else if(ret < 0) return ret; ret = read(switch_fd, &buffer->buf[buffer->offset], 1); if(!ret) return 2; if(ret == -1 && errno == EAGAIN) return 0; else if(ret < 0) break; if(buffer->buf[buffer->offset] == '\n') { buffer->buf[buffer->offset] = 0; if(buffer->offset > 0 && buffer->buf[buffer->offset-1] == '\r') buffer->buf[buffer->offset-1] = 0; if(strlen((char *)(buffer->buf))) { log_printf(NOTICE, "switch-firmware: '%s'", (char *)(buffer->buf)); int cmd_fd = -1; if(cmd_q && (*cmd_q)) { cmd_fd = (*cmd_q)->fd; send_response(cmd_fd, (char *)(buffer->buf)); } SEND_TO_LISTENER("S0L", 3, status_listener); SEND_TO_LISTENER("S0P", 3, gpi_listener); SEND_TO_LISTENER("S0O", 3, oc_listener); SEND_TO_LISTENER("S0R", 3, relay_listener); SEND_TO_LISTENER("S0S", 3, silence_listener); if((!strncmp((char *)(buffer->buf), "RRR", 3)) || (!strncmp((char *)(buffer->buf), "EEE", 3))) cmd_pop(cmd_q); } buffer->offset = 0; return 0; } buffer->offset++; if(buffer->offset >= sizeof(buffer->buf)) { log_printf(DEBUG, "string too long (fd=%d)", switch_fd); buffer->offset = 0; return 0; } } return ret; } int main_loop(int switch_fd, int cmd_listen_fd, options_t* opt) { log_printf(NOTICE, "entering main loop"); fd_set readfds, tmpfds; FD_ZERO(&readfds); FD_SET(switch_fd, &readfds); FD_SET(cmd_listen_fd, &readfds); int max_fd = switch_fd > cmd_listen_fd ? switch_fd : cmd_listen_fd; cmd_t* cmd_q = NULL; client_t* client_lst = NULL; read_buffer_t switch_buffer; switch_buffer.offset = 0; int sig_fd = signal_init(); if(sig_fd < 0) return -1; FD_SET(sig_fd, &readfds); max_fd = (max_fd < sig_fd) ? sig_fd : max_fd; int return_value = 0; char* channel = "unknown"; if(opt->mode_ == MODE_MASTER && opt->channel_master_ == CHAN_MAIN) channel = "master_main"; else if(opt->mode_ == MODE_MASTER && opt->channel_master_ == CHAN_MUSIC) channel = "master_music"; else if(opt->mode_ == MODE_STANDBY && opt->channel_standby_ == CHAN_MAIN) channel = "standby_main"; else if(opt->mode_ == MODE_STANDBY && opt->channel_standby_ == CHAN_MUSIC) channel = "standby_music"; char* cmd_param = strdup("*0M1*0ii1"); char* ch_nr = key_value_storage_find(&opt->alias_table_, channel); if(!ch_nr || ch_nr[0] == 0 || ch_nr[1] == 0 || ch_nr[2] != 0) { log_printf(ERROR, "invalid channel name or number: %s", channel); free(cmd_param); return_value = -1; } else { cmd_param[6] = ch_nr[0]; cmd_param[7] = ch_nr[1]; return_value = cmd_push(&cmd_q, -1, SWITCH, cmd_param); free(cmd_param); send_command(switch_fd, cmd_q); } struct timeval timeout; while(!return_value) { memcpy(&tmpfds, &readfds, sizeof(tmpfds)); timeout.tv_sec = 0; timeout.tv_usec = 200000; int ret = select(max_fd+1, &tmpfds, NULL, NULL, &timeout); if(ret == -1 && errno != EINTR) { log_printf(ERROR, "select returned with error: %s", strerror(errno)); return_value = -1; break; } if(ret == -1) continue; if(!ret) { if(cmd_q && cmd_has_expired(*cmd_q)) { log_printf(ERROR, "last command expired"); cmd_pop(&cmd_q); } else continue; } if(FD_ISSET(sig_fd, &tmpfds)) { if(signal_handle()) { return_value = 1; break; } } if(FD_ISSET(switch_fd, &tmpfds)) { return_value = process_switch(&switch_buffer, switch_fd, &cmd_q, client_lst); if(return_value) break; } if(FD_ISSET(cmd_listen_fd, &tmpfds)) { int new_fd = accept(cmd_listen_fd, NULL, NULL); if(new_fd < 0) { log_printf(ERROR, "accept returned with error: %s", strerror(errno)); return_value = -1; break; } log_printf(DEBUG, "new command connection (fd=%d)", new_fd); FD_SET(new_fd, &readfds); max_fd = (max_fd < new_fd) ? new_fd : max_fd; fcntl(new_fd, F_SETFL, O_NONBLOCK); client_add(&client_lst, new_fd); } client_t* lst = client_lst; while(lst) { if(FD_ISSET(lst->fd, &tmpfds)) { return_value = nonblock_recvline(&(lst->buffer), lst->fd, &cmd_q, client_lst, opt); if(return_value == 2) { log_printf(DEBUG, "removing closed command connection (fd=%d)", lst->fd); client_t* deletee = lst; lst = lst->next; FD_CLR(deletee->fd, &readfds); client_remove(&client_lst, deletee->fd); return_value = 0; continue; } if(return_value) break; } if(lst) lst = lst->next; } if(cmd_q && !cmd_q->sent) send_command(switch_fd, cmd_q); } cmd_clear(&cmd_q); client_clear(&client_lst); signal_stop(); return return_value; } int main(int argc, char* argv[]) { log_init(); 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, exiting\n"); } if(ret == -3) { fprintf(stderr, "syntax error: mode name must be either master or standby\n"); } if(ret == -4) { fprintf(stderr, "syntax error: channel name must be either main or music\n"); } if(ret == -5) { fprintf(stderr, "syntax error: invalid baudrate\n"); } if(ret != -2) options_print_usage(); options_clear(&opt); log_close(); exit(ret); } string_list_element_t* tmp = opt.log_targets_.first_; if(!tmp) { log_add_target("syslog:3,switchctl,daemon"); } else { while(tmp) { ret = log_add_target(tmp->string_); 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", tmp->string_); break; case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", tmp->string_); break; default: fprintf(stderr, "syntax error near: '%s', exitting\n", tmp->string_); break; } options_clear(&opt); log_close(); exit(ret); } tmp = tmp->next_; } } log_printf(NOTICE, "just started..."); if(options_parse_post(&opt)) { options_clear(&opt); log_close(); exit(-1); } priv_info_t priv; if(opt.username_) if(priv_init(&priv, opt.username_, opt.groupname_)) { options_clear(&opt); log_close(); exit(-1); } FILE* pid_file = NULL; if(opt.pid_file_) { pid_file = fopen(opt.pid_file_, "w"); if(!pid_file) { log_printf(WARNING, "unable to open pid file: %s", strerror(errno)); } } if(opt.chroot_dir_) if(do_chroot(opt.chroot_dir_)) { options_clear(&opt); log_close(); exit(-1); } if(opt.username_) if(priv_drop(&priv)) { options_clear(&opt); log_close(); exit(-1); } if(opt.daemonize_) { pid_t oldpid = getpid(); daemonize(); log_printf(INFO, "running in background now (old pid: %d)", oldpid); } if(pid_file) { pid_t pid = getpid(); fprintf(pid_file, "%d", pid); fclose(pid_file); } int cmd_listen_fd = init_command_socket(opt.command_sock_); if(cmd_listen_fd < 0) { options_clear(&opt); log_close(); exit(-1); } int switch_fd = 0; for(;;) { switch_fd = open(opt.switch_dev_, O_RDWR | O_NOCTTY); if(switch_fd < 0) ret = 2; else { ret = setup_tty(switch_fd, opt.baudrate_); if(ret) ret = 2; else ret = main_loop(switch_fd, cmd_listen_fd, &opt); } if(ret == 2) { log_printf(ERROR, "%s error, trying to reopen in 5 seconds..", opt.switch_dev_); if(switch_fd > 0) close(switch_fd); sleep(5); } else break; } close(cmd_listen_fd); if(switch_fd > 0) close(switch_fd); if(!ret) log_printf(NOTICE, "normal shutdown"); else if(ret < 0) log_printf(NOTICE, "shutdown after error (code %d)", ret); else log_printf(NOTICE, "shutdown after signal"); options_clear(&opt); log_close(); return ret; }