diff options
author | deva <deva> | 2005-06-09 11:00:03 +0000 |
---|---|---|
committer | deva <deva> | 2005-06-09 11:00:03 +0000 |
commit | 4bede1d02de893b450f5eb581aa0a384246c162d (patch) | |
tree | c4863551b362fe1949642f70c08df46fc68d1119 /src/miav.cc | |
parent | 64efd74ecfa761c83b70ec2374bf448b6e42e254 (diff) |
Added daemon code, and cleaned up using -Wall and -Werror
Diffstat (limited to 'src/miav.cc')
-rw-r--r-- | src/miav.cc | 152 |
1 files changed, 29 insertions, 123 deletions
diff --git a/src/miav.cc b/src/miav.cc index d1211f1..188b1d7 100644 --- a/src/miav.cc +++ b/src/miav.cc @@ -31,6 +31,9 @@ /* * $Log$ + * Revision 1.13 2005/06/09 11:00:03 deva + * Added daemon code, and cleaned up using -Wall and -Werror + * * Revision 1.12 2005/05/25 12:31:59 deva * * Made info (error message system) work correctly. @@ -60,26 +63,27 @@ #include <config.h> -#include "server.h" -#include "socket.h" - #ifdef USE_GUI #include <qapplication.h> #include "mainwindow.h" #include "miav.h" #endif /* USE_GUI */ + +#include "miav_daemon.h" + #include "miav_config.h" -#include "info_console.h" #include "info_gui.h" +#include "info_console.h" #include <stdio.h> -enum { +typedef enum { + MODE_UNKNOWN, MODE_GRAB, MODE_SERVER -}; +} run_mode; /** @@ -111,137 +115,35 @@ int grab(int argc, char *argv[]) { } -#if 0 -/** - * Peters DAEMON code - */ -#include <sys/types.h> -#include <sys/stat.h> -#include <unistd.h> - -int rundaemon() -{ - int pipes[2]; - - int f; - - pipe(pipes); - - f = fork(); - switch(f) { - case -1: // error - fprintf(stderr, "Error, could not fork()!\n"); - exit(0); - break; - case 0: // child - return communicationCtl(pipes[0]); - break; - default: // parent - signal(SIGCHLD, reportAndExit); - - return serialportCtl(pipes[1]); - break; - } - - return 0; -} - -void daemon() { - int f; - int fd; - - chdir("/"); - umask(0); - - f = fork(); - switch(f) { - case -1: - fprintf(stderr, "fork() error!\n"); - return 0; - break; - case 0: - if( (fp = fopen("/tmp/termo.out", "w")) == NULL) { - fprintf(stderr, "Outfile open error!\n"); - exit(0); - } - fd = open("/dev/null", O_NOCTTY | O_RDWR, 0666); - dup2(0, fd); - dup2(1, fd); - dup2(2, fd); - setsid(); - signal (SIGTERM, SIG_IGN); - signal (SIGINT, SIG_IGN); - signal (SIGHUP, SIG_IGN); - serialfd = initSerialPort(INDEVICE); - if(setgid(NOBODY_GROUP) != 0) {fprintf(fp, "GRP ch ERR\n");return 1;} - if(setuid(NOBODY_USER) != 0) {fprintf(fp, "USER ch ERR\n");return 1;} - - return rundaemon(); - break; - default: - exit(0); - } - return 0; -} -/** - * End og Peters DAEMON code - */ -#endif /** * This function starts the MIaV server. */ -int server(int argc, char *argv[]) { +int server(int argc, char *argv[]) +{ InfoConsole info; - int port = config->readInt("server_port"); - pid_t childpid; // variable to store the child's pid - signal(SIGCLD, SIG_IGN); // Ved SIGCHILD til IGNORE maa wait/waitpid ikke kaldes - // (ellers kommer der kernel-brok) - - printf("Listening on port %d\n", port); - Socket *socket = new Socket(port, &info); + MiavDaemon daemon; - while(1) { - Socket *csocket = new Socket(socket->slisten()); - if(csocket->isConnected()) { - childpid = fork(); - - if(childpid == -1) { - // fork() returns -1 on failure - perror("fork"); - exit(1); - } else if(childpid == 0) { - // fork() returns 0 to the child process - delete socket; // Close listen socket. - newConnection(csocket); - delete csocket; // Close communication socket. - exit(0); - } else { - // fork() returns new pid to the parent process - } - } - } - delete socket; - return 0; + MiavConfig cfg(ETC"/miav.conf", &info); + + int uid = cfg.readInt("server_uid"); + int gid = cfg.readInt("server_gid"); + + return daemon.run(uid, gid); } #include "debug.h" int main(int argc, char *argv[]) { + run_mode mode = MODE_UNKNOWN; - int mode = MODE_GRAB; - - if(argc < 2) { - printf("Usage: miav [mode] [mode options]\n"); - printf("[mode] can be one of the following: grab or server.\n"); - return 1; + if(argc >= 2) { + if(!strcmp(argv[1], "grab")) mode = MODE_GRAB; + if(!strcmp(argv[1], "server")) mode = MODE_SERVER; } - if(!strcmp(argv[1], "grab")) mode = MODE_GRAB; - if(!strcmp(argv[1], "server")) mode = MODE_SERVER; - switch(mode) { case MODE_GRAB: { @@ -249,10 +151,14 @@ int main(int argc, char *argv[]) } case MODE_SERVER: { - InfoConsole info; - config = new MiavConfig(ETC"/miav.conf", &info); return server(argc - 2, &argv[2]); } + case MODE_UNKNOWN: + { + printf("Usage: %s [mode] [mode options]\n", argv[0]); + printf("[mode] can be one of the following: grab or server.\n"); + return 1; + } } return 0; } |