From 4bede1d02de893b450f5eb581aa0a384246c162d Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 9 Jun 2005 11:00:03 +0000 Subject: Added daemon code, and cleaned up using -Wall and -Werror --- src/Makefile.am | 4 ++ src/daemon.cc | 84 ++++++++++++++++++++++++++ src/daemon.h | 45 ++++++++++++++ src/decoder.cc | 4 +- src/info.h | 2 +- src/info_console.cc | 2 + src/info_gui.cc | 1 + src/miav.cc | 152 +++++++++-------------------------------------- src/miav_config.cc | 5 +- src/miav_daemon.cc | 91 ++++++++++++++++++++++++++++ src/miav_daemon.h | 54 +++++++++++++++++ src/mov_encoder.cc | 5 +- src/mov_encoder_thread.h | 5 +- src/player.cc | 6 +- 14 files changed, 328 insertions(+), 132 deletions(-) create mode 100644 src/daemon.cc create mode 100644 src/daemon.h create mode 100644 src/miav_daemon.cc create mode 100644 src/miav_daemon.h diff --git a/src/Makefile.am b/src/Makefile.am index 499e49b..f1eeb83 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,6 +9,7 @@ miav_SOURCES = $(shell if [ $QT_CXXFLAGS ] ; then ../tools/MocList cc; fi ) \ aboutwindow.cc \ camera.cc \ cprquerydialog.cc \ + daemon.cc \ decoder.cc \ dv1394.cc \ encoder.cc \ @@ -19,6 +20,7 @@ miav_SOURCES = $(shell if [ $QT_CXXFLAGS ] ; then ../tools/MocList cc; fi ) \ mainwindow.cc \ messagebox.cc \ miav.cc \ + miav_daemon.cc \ miav_config.cc \ mov_encoder.cc \ mov_encoder_thread.cc \ @@ -36,6 +38,7 @@ EXTRA_DIST = \ aboutwindow.h \ camera.h \ cprquerydialog.h \ + daemon.h \ debug.h \ decoder.h \ dv.h \ @@ -49,6 +52,7 @@ EXTRA_DIST = \ mainwindow.h \ messagebox.h \ miav.h \ + miav_daemon.h \ miav_config.h \ mov_encoder.h \ mov_encoder_thread.h \ diff --git a/src/daemon.cc b/src/daemon.cc new file mode 100644 index 0000000..95cf0df --- /dev/null +++ b/src/daemon.cc @@ -0,0 +1,84 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * daemon.cc + * + * Thu Jun 9 10:27:59 CEST 2005 + * Copyright 2005 Bent Bisballe + * deva@aasimon.org + ****************************************************************************/ + +/* + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "daemon.h" + +#include +#include +#include +#include +#include + +Daemon::Daemon() +{} + +Daemon::~Daemon() +{} + +int Daemon::run(uid_t uid, gid_t gid) +{ + int f; + int fd; + + chdir("/"); + umask(0); + + f = fork(); + switch(f) { + case -1: // Fork error + fprintf(stderr, "fork() error!\n"); + return 1; + break; + + case 0: // Forked child + if(setgid(gid) != 0) { + fprintf(stderr, "Failed to change to gid %d, quitting.\n", gid); + return 1; + } + + if(setuid(uid) != 0) { + fprintf(stderr, "Failed to change to uid %d, quitting.\n", uid); + return 1; + } + + 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); + + return daemon_main(); + + default: // Parent + // exit(0); + return 0; + } +} diff --git a/src/daemon.h b/src/daemon.h new file mode 100644 index 0000000..0859034 --- /dev/null +++ b/src/daemon.h @@ -0,0 +1,45 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * daemon.h + * + * Thu Jun 9 10:27:59 CEST 2005 + * Copyright 2005 Bent Bisballe + * deva@aasimon.org + ****************************************************************************/ + +/* + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __DAEMON_H__ +#define __DAEMON_H__ + +#include + +class Daemon { +public: + Daemon(); + virtual ~Daemon(); + + /** + * Use NOBODY_GROUP and NOBODY_USER if no privileges are needed to run. + */ + int run(uid_t uid, gid_t gid); + +private: + virtual int daemon_main() = 0; +}; + +#endif/*__DAEMON_H__*/ diff --git a/src/decoder.cc b/src/decoder.cc index 7b3dd97..404fc5f 100644 --- a/src/decoder.cc +++ b/src/decoder.cc @@ -39,6 +39,9 @@ /* * $Log$ + * Revision 1.32 2005/06/09 11:00:03 deva + * Added daemon code, and cleaned up using -Wall and -Werror + * * Revision 1.31 2005/06/02 20:45:01 deva * * Added clear button @@ -149,7 +152,6 @@ void Decoder::decode() while(*running) { uint8_t *ptr; - int len; SDL_Event user_event; // Read a dvframe diff --git a/src/info.h b/src/info.h index e8884fd..7a9147b 100644 --- a/src/info.h +++ b/src/info.h @@ -40,7 +40,7 @@ class Info { public: Info() {} - ~Info() {} + virtual ~Info() {} virtual void error(char* fmt, ...) = 0; virtual void warn(char* fmt, ...) = 0; diff --git a/src/info_console.cc b/src/info_console.cc index 6acfd4f..a0ca236 100644 --- a/src/info_console.cc +++ b/src/info_console.cc @@ -41,10 +41,12 @@ InfoConsole::InfoConsole(): Info() { + pthread_mutex_init (&mutex, NULL); } InfoConsole::~InfoConsole() { + pthread_mutex_destroy(&mutex); } void InfoConsole::error(char *fmt, ...) diff --git a/src/info_gui.cc b/src/info_gui.cc index 4865ae0..db23827 100644 --- a/src/info_gui.cc +++ b/src/info_gui.cc @@ -67,6 +67,7 @@ InfoGui::InfoGui(QApplication *a, QWidget *p): Info() InfoGui::~InfoGui() { + pthread_mutex_destroy(&mutex); } void InfoGui::setParent(QWidget *p) 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 -#include "server.h" -#include "socket.h" - #ifdef USE_GUI #include #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 -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 -#include -#include - -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; } diff --git a/src/miav_config.cc b/src/miav_config.cc index 9a6233b..2839c5c 100644 --- a/src/miav_config.cc +++ b/src/miav_config.cc @@ -31,6 +31,9 @@ /* * $Log$ + * Revision 1.9 2005/06/09 11:00:03 deva + * Added daemon code, and cleaned up using -Wall and -Werror + * * Revision 1.8 2005/05/03 08:31:59 deva * Removed the error object, and replaced it with a more generic info object. * @@ -88,7 +91,6 @@ MiavConfig::~MiavConfig() */ _cfg *MiavConfig::parseError(char* msg, char* line) { - char errbuf[512]; info->error("Error parsing file %s at line:\n\t%s\n\t%s\n", filename.c_str(), line, msg); return NULL; } @@ -263,7 +265,6 @@ _cfg *MiavConfig::findNode(char* node) if(!strcmp(node, cfg->name->c_str())) return cfg; cfg = cfg->next; } - char errbuf[256]; info->error("Request for nonexisting node \"%s\"!\n", node); return NULL; } diff --git a/src/miav_daemon.cc b/src/miav_daemon.cc new file mode 100644 index 0000000..633ab7d --- /dev/null +++ b/src/miav_daemon.cc @@ -0,0 +1,91 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * miav_daemon.cc + * + * Thu Jun 9 11:14:19 CEST 2005 + * Copyright 2005 Bent Bisballe + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of MIaV. + * + * MIaV 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 2 of the License, or + * (at your option) any later version. + * + * MIaV 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 MIaV; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +/* + * $Id$ + */ + +/* + * $Log$ + * Revision 1.1 2005/06/09 11:00:03 deva + * Added daemon code, and cleaned up using -Wall and -Werror + * + */ + +#include +#include "miav_daemon.h" + +#include "info_console.h" +#include "miav_config.h" + +#include "server.h" +#include "socket.h" + +MiavDaemon::MiavDaemon() +{} + +MiavDaemon::~MiavDaemon() +{} + +int MiavDaemon::daemon_main() +{ + InfoConsole info; + config = new MiavConfig(ETC"/miav.conf", &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); + + while(1) { + Socket *csocket = new Socket(socket->slisten()); + if(csocket->isConnected()) { + childpid = fork(); + + switch(childpid) { + case -1: // fork() returns -1 on failure + perror("fork"); + exit(1); + case 0: // fork() returns 0 to the child process + delete socket; // Close listen socket. + newConnection(csocket); + delete csocket; // Close communication socket. + exit(0); + + default: // fork() returns new pid to the parent process + break; + } + } + } + delete socket; + return 0; +} + diff --git a/src/miav_daemon.h b/src/miav_daemon.h new file mode 100644 index 0000000..91430f1 --- /dev/null +++ b/src/miav_daemon.h @@ -0,0 +1,54 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * miav_daemon.h + * + * Thu Jun 9 11:14:19 CEST 2005 + * Copyright 2005 Bent Bisballe + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of MIaV. + * + * MIaV 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 2 of the License, or + * (at your option) any later version. + * + * MIaV 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 MIaV; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +/* + * $Id$ + */ + +/* + * $Log$ + * Revision 1.1 2005/06/09 11:00:03 deva + * Added daemon code, and cleaned up using -Wall and -Werror + * + */ + +#include +#ifndef __MIAV_MIAV_DAEMON_H__ +#define __MIAV_MIAV_DAEMON_H__ + +#include "daemon.h" + +class MiavDaemon: public Daemon { +public: + MiavDaemon(); + ~MiavDaemon(); + +private: + int daemon_main(); +}; + +#endif/*__MIAV_MIAV_DAEMON_H__*/ diff --git a/src/mov_encoder.cc b/src/mov_encoder.cc index c5aec9d..fff5600 100644 --- a/src/mov_encoder.cc +++ b/src/mov_encoder.cc @@ -39,6 +39,9 @@ /* * $Log$ + * Revision 1.27 2005/06/09 11:00:03 deva + * Added daemon code, and cleaned up using -Wall and -Werror + * * Revision 1.26 2005/05/26 12:48:36 deva * *** empty log message *** * @@ -381,7 +384,7 @@ void MovEncoder::run() continue; } - for(int cnt = 0; cnt < item->size(); cnt++) { + for(unsigned int cnt = 0; cnt < item->size(); cnt++) { in_frame = item->at(cnt); out_frame = encode(in_frame); out_frame->number = in_frame->number; diff --git a/src/mov_encoder_thread.h b/src/mov_encoder_thread.h index 989dd87..87a8f63 100644 --- a/src/mov_encoder_thread.h +++ b/src/mov_encoder_thread.h @@ -31,6 +31,9 @@ /* * $Log$ + * Revision 1.6 2005/06/09 11:00:03 deva + * Added daemon code, and cleaned up using -Wall and -Werror + * * Revision 1.5 2005/05/22 15:49:22 deva * Added multithreaded encoding support. * @@ -92,7 +95,7 @@ private: int current_frame; unsigned int frame_number; - int num_frames_in_block; + unsigned int num_frames_in_block; MovEncoderWriter *writer; pthread_t* writer_tid; diff --git a/src/player.cc b/src/player.cc index aa80605..051c910 100644 --- a/src/player.cc +++ b/src/player.cc @@ -39,6 +39,9 @@ /* * $Log$ + * Revision 1.24 2005/06/09 11:00:03 deva + * Added daemon code, and cleaned up using -Wall and -Werror + * * Revision 1.23 2005/05/03 08:31:59 deva * Removed the error object, and replaced it with a more generic info object. * @@ -66,7 +69,6 @@ Player::Player(Info *ginfo, // No errors has ocurred... yet! noErrors = true; - char errbuf[256]; info = ginfo; running = grunning; @@ -117,10 +119,8 @@ void Player::player() SDL_Rect rect; Frame *frame; - unsigned char *pxs[3]; int pitches[3]; - int i; struct timespec ts; if(!noErrors) return; // FIXME: Gracefully exit... -- cgit v1.2.3