diff --git a/Makefile b/Makefile index de4c6d3..c35c452 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ PREFIX=/usr/local DATAPREFIX=/var/lib RUNPID=/var/run/miniwebcam.pid ETCPREFIX=/etc +SYSTEMDUNITDIR=$(ETCPREFIX)/systemd/system DEFAULT_SERVERPORT=20010 ARCH=$(shell uname -m) CXX=g++ @@ -33,10 +34,13 @@ miniwebcam: $(OBJFILES) assets.obj rm -rf assets.obj install: miniwebcam - cp -rf miniwebcam $(PREFIX)/bin + install -d $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(SYSTEMDUNITDIR) + install -m 755 miniwebcam $(DESTDIR)$(PREFIX)/bin/miniwebcam + sed 's|@PREFIX@|$(PREFIX)|g' miniwebcam.service > $(DESTDIR)$(SYSTEMDUNITDIR)/miniwebcam.service uninstall: - rm -rf $(PREFIX)/bin/miniwebcam + rm -f $(DESTDIR)$(PREFIX)/bin/miniwebcam + rm -f $(DESTDIR)$(SYSTEMDUNITDIR)/miniwebcam.service config: echo "#ifndef _CONFIG_H_" > config.h diff --git a/README.md b/README.md index 6926f26..a5202d7 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,20 @@ The configuration is loaded in the following order. - program parameters - user defined configurtion file defined in the program parameters +The program runs in the foreground by default. Use `-D` to detach and run as a +daemon, or `-F` to explicitly run in the foreground. Daemon mode redirects +standard input, output, and error to `/dev/null`; use `-debug` only when running +in the foreground if you need to see debug output. + +The default system configuration file is `/etc/miniwebcam.conf`. `make install` +installs the executable and a systemd service unit. After installing, reload +systemd and enable/start the service with: + +```sh +sudo systemctl daemon-reload +sudo systemctl enable --now miniwebcam.service +``` + # INFORMATION here are some usefull commands, maybe it helps to set the resolution to a readonable value diff --git a/configuration.cc b/configuration.cc index 610086c..be01907 100644 --- a/configuration.cc +++ b/configuration.cc @@ -257,7 +257,7 @@ int Configuration::LoadArgs(int argc, char **argv) { void Configuration::Help() { printf ("Parameters:\n"); - printf (" -F run in foreground\n"); + printf (" -F run in foreground (default)\n"); printf (" -D run as daemon\n"); printf (" -debug enable debug output\n"); printf (" -debug_off disable debug output\n"); @@ -281,7 +281,7 @@ void Configuration::Help() { printf (" -config FILE load this configfile\n"); printf (" -dump_config print the config file\n"); printf ("\n"); - printf (" -H print this help\n"); + printf (" -h, --help print this help\n"); printf ("\n\nSample:\n"); printf (" ./miniwebcam -sslkey ./ssl-key.pem -sslcert ./ssl-cert.pem -websize 800x600\n"); } diff --git a/configuration.h b/configuration.h index 9828c8e..e649c60 100644 --- a/configuration.h +++ b/configuration.h @@ -10,7 +10,7 @@ #define DEFAULT_HTTP_PORT 8080 #define DEFAULT_HTTPS_PORT 8081 -#define DEFAULT_CONFIG_FILE "/etc/miniwebcam/miniwebcam.conf" +#define DEFAULT_CONFIG_FILE "/etc/miniwebcam.conf" #define DEFAULT_SSL_KEY "/etc/miniwebcam/ssl-key.pem" #define DEFAULT_SSL_CERT "/etc/miniwebcam/ssl-cert.pem" #define DEFAULT_VDEV_IOMODE 1 @@ -71,4 +71,3 @@ public: extern Configuration config; #endif - diff --git a/main.cc b/main.cc index 9c0b54a..cc6b2c9 100644 --- a/main.cc +++ b/main.cc @@ -1,8 +1,11 @@ +#define _DEFAULT_SOURCE + #include #include #include #include +#include #include @@ -146,13 +149,17 @@ int main(int argc, char **argv) { return 0; } - printf ("MiniWebCam:\n"); - - if (config.GetInitFlags() & CONF_INITFLAGS_HELP) { config.Help(); return 0; } + + if (config.runasdaemon && daemon(0, 0) != 0) { + perror("could not daemonize"); + return EX_OSERR; + } + + printf ("MiniWebCam:\n"); debug_enable(config.debug); pthread_create(&thr_webserver, NULL, &thread_webserver, NULL); @@ -231,4 +238,3 @@ int timedeiff_ms(timeval *t1) { return res; }; - diff --git a/miniwebcam.service b/miniwebcam.service new file mode 100644 index 0000000..e8bb946 --- /dev/null +++ b/miniwebcam.service @@ -0,0 +1,11 @@ +[Unit] +Description=MiniWebCam server +After=network.target + +[Service] +Type=simple +ExecStart=@PREFIX@/bin/miniwebcam -F +Restart=on-failure + +[Install] +WantedBy=multi-user.target