diff options
author | Christian Pointner <equinox@helsinki.at> | 2009-11-18 11:27:28 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2009-11-18 11:27:28 (GMT) |
commit | 7289da5dcf7d689a005cba6d645daea23aa3104c (patch) | |
tree | f5e660c5ed963ddc35afda0123f8c0de9297d4b3 | |
parent | 9044c3b05f6809ceca2f35d582a1601602f088d9 (diff) |
added heartbeat client
-rw-r--r-- | Makefile | 15 | ||||
-rw-r--r-- | heartbeatclient.c | 296 | ||||
-rw-r--r-- | options.c | 30 | ||||
-rw-r--r-- | options.h | 7 |
4 files changed, 343 insertions, 5 deletions
@@ -26,6 +26,7 @@ endif EXE_SWITCHCTL := switchctl EXE_SERIALCLIENT := serialclient EXE_STDIOCLIENT := stdioclient +EXE_HEARTBEATCLIENT := heartbeatclient COMMONOBJ := log.o \ sig_handler.o \ @@ -44,12 +45,15 @@ SERIALCLIENTOBJ := opt-serialclient.o \ STDIOCLIENTOBJ := opt-stdioclient.o \ stdioclient.o +HEARTBEATCLIENTOBJ := opt-heartbeatclient.o \ + heartbeatclient.o -SRC := $(COMMONOBJ:%.o=%.c) $(SWITCHCTLOBJ:%.o=%.c) $(SERIALCLIENTOBJ:%.o=%.c) $(STDIOCLIENTOBJ:%.o=%.c) options.c + +SRC := $(COMMONOBJ:%.o=%.c) $(SWITCHCTLOBJ:%.o=%.c) $(SERIALCLIENTOBJ:%.o=%.c) $(STDIOCLIENTOBJ:%.o=%.c) $(HEARTBEATCLIENTOBJ:%.o=%.c) options.c .PHONY: clean distclean -all: $(EXE_SWITCHCTL) $(EXE_SERIALCLIENT) $(EXE_STDIOCLIENT) +all: $(EXE_SWITCHCTL) $(EXE_SERIALCLIENT) $(EXE_STDIOCLIENT) $(EXE_HEARTBEATCLIENT) %.d: %.c @set -e; rm -f $@; \ @@ -70,6 +74,9 @@ $(EXE_SERIALCLIENT): $(COMMONOBJ) $(SERIALCLIENTOBJ) $(EXE_STDIOCLIENT): $(COMMONOBJ) $(STDIOCLIENTOBJ) $(CC) $(COMMONOBJ) $(STDIOCLIENTOBJ) -o $@ $(LDFLAGS) +$(EXE_HEARTBEATCLIENT): $(COMMONOBJ) $(HEARTBEATCLIENTOBJ) + $(CC) $(COMMONOBJ) $(HEARTBEATCLIENTOBJ) -o $@ $(LDFLAGS) + opt-switchctl.o: options.c $(CC) $(CFLAGS) -DOPT_SWITCHCTL -o $@ -c $< @@ -79,6 +86,9 @@ opt-serialclient.o: options.c opt-stdioclient.o: options.c $(CC) $(CFLAGS) -DOPT_STDIOCLIENT -o $@ -c $< +opt-heartbeatclient.o: options.c + $(CC) $(CFLAGS) -DOPT_HEARTBEATCLIENT -o $@ -c $< + %.o: %.c $(CC) $(CFLAGS) -c $< @@ -95,3 +105,4 @@ clean: rm -f $(EXE_SWITCHCTL) rm -f $(EXE_SERIALCLIENT) rm -f $(EXE_STDIOCLIENT) + rm -f $(EXE_HEARTBEATCLIENT)
\ No newline at end of file diff --git a/heartbeatclient.c b/heartbeatclient.c new file mode 100644 index 0000000..7111525 --- /dev/null +++ b/heartbeatclient.c @@ -0,0 +1,296 @@ +/* + * rhctl + * + * Copyright (C) 2009 Christian Pointner <equinox@spreadspace.org> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include "datatypes.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> + +#include "log.h" +#include "sig_handler.h" +#include "options.h" + +#include "daemon.h" +#include "utils.h" + +int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) +{ +} + +int process_serial(read_buffer_t* buffer, int serial_fd) +{ + int ret = 0; + struct timeval tv; + fd_set fds; + FD_ZERO(&fds); + FD_SET(serial_fd, &fds); + + for(;;) { + tv.tv_sec = 0; + tv.tv_usec = 0; + ret = select(serial_fd+1, &fds, NULL, NULL, &tv); + if(!ret) + return 0; + else if(ret < 0) + return ret; + + ret = read(serial_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(buffer->buf)) { + log_printf(INFO, "heartbeat: '%s'", buffer->buf); + // reset heartbeat cnt + } + + buffer->offset = 0; + return 0; + } + + buffer->offset++; + if(buffer->offset >= sizeof(buffer->buf)) { + log_printf(DEBUG, "string too long (fd=%d)", serial_fd); + buffer->offset = 0; + return 0; + } + } + + return ret; +} + +int main_loop(int serial_fd, int cmd_fd, options_t* opt) +{ + log_printf(NOTICE, "entering main loop"); + + fd_set readfds, tmpfds; + FD_ZERO(&readfds); + FD_SET(serial_fd, &readfds); + FD_SET(cmd_fd, &readfds); + int max_fd = serial_fd > cmd_fd ? serial_fd : cmd_fd; + + 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; + return_value = send_string(cmd_fd, "type heartbeat"); + if(return_value <= 0) { + log_printf(ERROR, "error setting client type"); + return_value = -1; + } + else { + log_printf(NOTICE, "connecting as type '%s'", opt->type_); + return_value = 0; + } + + read_buffer_t serial_buffer; + serial_buffer.offset = 0; + struct timeval timeout; + while(!return_value) { + memcpy(&tmpfds, &readfds, sizeof(tmpfds)); + + timeout.tv_sec = 0; + timeout.tv_usec = 100000; + 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 || !ret) + continue; + if(!ret) { + // timeout reached ??? + // if yes send command: "mode standby" + } + + if(FD_ISSET(sig_fd, &tmpfds)) + if(signal_handle()) + return_value = 1; + + if(FD_ISSET(serial_fd, &tmpfds)) { + return_value = process_serial(&serial_buffer, serial_fd); + if(return_value) + break; + } + + if(FD_ISSET(cmd_fd, &tmpfds)) + return_value = 0; // ignore data from command sock + } + + 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 == -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,heartbeatclient,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_fd = connect_command_socket(opt.command_sock_); + if(cmd_fd < 0) { + options_clear(&opt); + log_close(); + exit(-1); + } + + int serial_fd = 0; + for(;;) { + serial_fd = open(opt.serial_dev_, O_RDWR | O_NOCTTY); + if(serial_fd < 0) + ret = 2; + else { + ret = setup_tty(serial_fd, opt.baudrate_); + if(ret) + ret = 2; + else + ret = main_loop(serial_fd, cmd_fd, &opt); + } + + if(ret == 2) { + log_printf(ERROR, "%s error, trying to reopen in 5 seconds..", opt.serial_dev_); + if(serial_fd > 0) + close(serial_fd); + sleep(5); + } + else + break; + } + + close(cmd_fd); + if(serial_fd > 0) + close(serial_fd); + + if(!ret) + log_printf(NOTICE, "normal shutdown"); + else if(ret < 0) + log_printf(NOTICE, "shutdown after error"); + else + log_printf(NOTICE, "shutdown after signal"); + + options_clear(&opt); + log_close(); + + return ret; +} @@ -192,6 +192,10 @@ int options_parse(options_t* opt, int argc, char* argv[]) PARSE_STRING_PARAM("-d","--device", opt->serial_dev_) PARSE_STRING_PARAM("-t","--type", opt->type_) #endif +#ifdef OPT_HEARTBEATCLIENT + PARSE_STRING_PARAM("-d","--device", opt->serial_dev_) + PARSE_INT_PARAM("-t","--timeout", opt->timeout_) +#endif else return i; } @@ -290,6 +294,9 @@ void options_default(options_t* opt) #ifdef OPT_STDIOCLIENT opt->progname_ = strdup("stdioclient"); #endif +#ifdef OPT_HEARTBEATCLIENT + opt->progname_ = strdup("heartbeatclient"); +#endif /* common */ opt->daemonize_ = 1; @@ -309,9 +316,14 @@ void options_default(options_t* opt) opt->switch_dev_ = strdup("/dev/audioswitch"); key_value_storage_init(&opt->alias_table_); -/* serialclient */ +/* serialclient and heartbeatclient */ opt->serial_dev_ = strdup("/dev/ttyUSB0"); + +/* serialclient only */ opt->type_ = NULL; + +/* heartbeatclient only */ + opt->timeout_ = 15; } void options_clear(options_t* opt) @@ -342,9 +354,11 @@ void options_clear(options_t* opt) free(opt->switch_dev_); key_value_storage_clear(&opt->alias_table_); -/* serialclient */ +/* serialclient and heartbeatclient */ if(opt->serial_dev_) free(opt->serial_dev_); + +/* serialclient only */ if(opt->type_) free(opt->type_); } @@ -361,6 +375,9 @@ void options_print_usage() #ifdef OPT_STDIOCLIENT printf("serialclient\n"); #endif +#ifdef OPT_HEARTBEATCLIENT + printf("heartbeatclient\n"); +#endif printf(" [-h|--help] prints this...\n"); #ifndef OPT_STDIOCLIENT printf(" [-D|--nodaemonize] don't run in background\n"); @@ -385,6 +402,10 @@ void options_print_usage() printf(" [-d|--device] <tty> the tty to connect to e.g. /dev/ttyUSB0\n"); printf(" [-t|--type] <type> use this client type\n"); #endif +#ifdef OPT_HEARTBEATCLIENT + printf(" [-d|--device] <tty> the tty to connect to e.g. /dev/ttyUSB0\n"); + printf(" [-t|--timeout] <timeout> heartbeat timeout in tenths of a second e.g. 15 -> 1.5s\n"); +#endif } void options_print(options_t* opt) @@ -434,4 +455,9 @@ void options_print(options_t* opt) printf("serial_dev: '%s'\n", opt->serial_dev_); printf("type: '%s'\n", opt->type_); #endif + +#ifdef OPT_HEARTBEATCLIENT + printf("serial_dev: '%s'\n", opt->serial_dev_); + printf("timeout: %d\m", opt->timeout_); +#endif } @@ -52,9 +52,14 @@ struct options_struct { char* switch_dev_; key_value_storage_t alias_table_; -/* serialclient */ +/* serialclient and heartbeatclient */ char* serial_dev_; + +/* serialclient only */ char* type_; + +/* heartbeatclient only */ + u_int32_t timeout_; }; typedef struct options_struct options_t; |