From cc1fcd363455779275ef7a0bc296f71025ac6af7 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Sun, 26 May 2019 23:28:19 +0200 Subject: asdf --- src/main.c | 50 +++++++++++++++++++++++++++++++++++++++----------- src/main.h | 8 ++++++++ 2 files changed, 47 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index b2847ee..d6493cd 100644 --- a/src/main.c +++ b/src/main.c @@ -16,7 +16,7 @@ struct prog_params parse_args(int argc, char* argv[]) switch (argv[i_cpy][o]) { case 'h': - printf("Help goes here\n"); + ERROR_HELP(""); break; case 'p': ret.telnet = 1; @@ -39,11 +39,12 @@ struct prog_params parse_args(int argc, char* argv[]) i++; break; case 'f'://PID file for spawned children + ret.fork = 1; ret.pidfile = argv[i_cpy + 1]; i++; break; default: - printf("Unrecognized Option: '%c'\n", argv[i_cpy][o]); + ERROR_HELP("Unrecognized Option: '%c'\n", argv[i_cpy][o]); break; };//switch }//for @@ -53,9 +54,12 @@ struct prog_params parse_args(int argc, char* argv[]) //Copy the rest as arguments for prog to exec ret.run_argc = argc - i_cpy; ret.run_argv = &(argv[i_cpy]); - }//else + }//else }//for + if(ret.telnet == ret.serial)//run EITHER in telnet OR modem mode + ERROR_HELP("Select either modem OR telnet.\n"); + return ret; } @@ -103,20 +107,46 @@ int main(int argc, char* argv[]) { signal(SIGCHLD,SIG_IGN); //Ignore sigchld struct prog_params params = parse_args(argc, argv); - - FILE* pidfile = fopen(prog_params.pidfile == NULL ? "pidfile" : prog_params.pidfile, "w"); - close (pidfile); + //Fork and write PID to pidfile + if(params.fork) + { + FILE* pidfile = fopen(params.pidfile, "w"); + if(!pidfile) + { + PRINT_ERROR("Unable to open pidfile for writing"); + exit(1); + } + pid_t pid = fork(); + + if(pid < 0) + { + PRINT_ERROR("fork() failed"); + exit(1); + } + else if(pid > 0) + { + fprintf(pidfile, "%i", pid); + printf("Forked with PID %i\n", pid); + fclose (pidfile); + } + + fclose (pidfile); + }//if params.pidfile + + if(params.serial) + printf("asdf"); + else if (params.telnet) + return 0; } -int main__q(int argc, char* argv[]) +void telnet_server(struct prog_params params) { signal(SIGCHLD,SIG_IGN); //Ignore sigchld - struct prog_params params = parse_args(argc, argv); int server_socket, client_socket; struct sockaddr_in socket_address, client_address; size_t claddrsize = sizeof(client_address); @@ -130,7 +160,7 @@ int main__q(int argc, char* argv[]) memset (&socket_address, 0, sizeof(socket_address)); socket_address.sin_family = AF_INET; - socket_address.sin_port = htons( 1111 ); + socket_address.sin_port = htons( params.port ); if ( (bind(server_socket, &socket_address, sizeof(socket_address))) == -1 ) { @@ -150,6 +180,4 @@ int main__q(int argc, char* argv[]) DEBUG_PRINTF("Connection: %s\n", inet_ntoa(client_address.sin_addr)); handle_connection(client_socket, client_address); } - - return 0; } diff --git a/src/main.h b/src/main.h index 78329c6..25521be 100644 --- a/src/main.h +++ b/src/main.h @@ -30,6 +30,11 @@ #define PRINT_ERROR( str ) { printf("%s: %s\n", str, strerror(errno)); } #endif +#define ERROR_HELP( ... ) { \ + printf(__VA_ARGS__); \ + printf("Help goes here"); \ + exit(1); } + struct prog_params { uint8_t telnet; @@ -43,6 +48,7 @@ struct prog_params char* serial_port; uint32_t serial_baudrate; + uint8_t fork; char* pidfile; }; @@ -51,3 +57,5 @@ struct prog_params parse_args(int argc, char* argv[]); void handle_connection(int _socket, struct sockaddr_in _addr); int main(int argc, char* argv[]); + +void telnet_server(struct prog_params params); -- cgit v1.2.3