Warning, /sdk/cutehmi/tools/cutehmi.daemon.3/README.md is written in an unsupported language. File is not indexed.

0001 # Daemon
0002 
0003 ![Development status](doc/status-gamma.svg)
0004 
0005 Console program, which allows one to run QML project in the background.
0006 
0007 Daemon mode is currently supported only on Linux. On Windows program can be run in application mode only (`--app` option).
0008 
0009 Any extension that does not provide graphical UI QML component can be loaded as *cutehmi.daemon.3* project. Use positional argument
0010 to specify an extension. For example to run [Count Daemon](../../extensions/CuteHMI/Examples/CountDaemon.3/) example use following
0011 command.
0012 
0013 ```
0014 cutehmi.daemon.3 CuteHMI.Examples.CountDaemon.3
0015 ```
0016 
0017 Read system logs to investigate whether daemon is running (e.g. `journalctl -n20` on a system with *systemd*).
0018 
0019 One may use `--app` option to tell the program to work as a foreground process (this can be useful when testing projects).
0020 For example following command runs [Count Daemon](../../extensions/CuteHMI/Examples/CountDaemon.3/) in application mode.
0021 
0022 ```
0023 cutehmi.daemon.3 CuteHMI.Examples.CountDaemon.3 --app
0024 ```
0025 
0026 Default loader picks `Daemon.qml` as default QML component to load. Component can be specified with second positional argument.
0027 
0028 One can also use `--init` option to replace default loader with custom one.
0029 
0030 You can use `--help` command line argument to see the list of all possible command line options.
0031 
0032 Setting empty path for PID file option (`--pidfile=`) disables creation of PID file.
0033 
0034 Fore debug builds use `cutehmi.daemon.3.debug` instead of `cutehmi.daemon.3`.
0035 
0036 ## Linux
0037 
0038 ### Signals
0039 
0040 Under Unix daemon will respond to signals in a following way.
0041 
0042 - SIGTERM tells daemon to gracefully quit with exit code set to EXIT_SUCCESS (0 on almost all systems).
0043 - SIGINT tells daemon to gracefully quit, but exit code will be set to 128 + signal code.
0044 - SIGQUIT causes violent termination and exits via abort.
0045 - SIGHUP attempts to reload the project as if daemon has been restarted.
0046 
0047 ### Forking
0048 
0049 Daemon is a program, which is supposed to work as a background process. This could be achieved in many ways, but lots of traditional
0050 UNIX daemons have been accomplishing it through techniques, which involve forking. Unfortunately this spots a controversy. It comes
0051 out there are three conflicting schools on how many forks daemon should perform. According to these daemon should fork once, twice
0052 or not fork at all... Detailed description on the topic is out of the scope of this document, but here are some major points.
0053 
0054 #### Single forking daemon
0055 
0056 Single fork is performed to exit the parent process and get rid of controlling terminal. After forking, daemon can become a new
0057 session leader, so that when user logs out from the session, process is not killed along with processes associated with that
0058 session. This is how [daemon()][3] function has been implemented.
0059 
0060 #### Double forking daemon
0061 
0062 After first fork process can still acquire controlling terminal if it opens tty according to System V rules [[1]]. Second fork
0063 prevents this [[2]]. Unfortunately this approach heavily obscures process hierarchy, because daemon continues as orphaned process.
0064 
0065 Notably *systemd* has troubles handling double-forking daemons as service units.
0066 [Documentation](https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=) of *systemd* is explicit about expected
0067 behaviour of daemon for forking service type.
0068 
0069 ```
0070 If set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up. The parent
0071 process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the
0072 main service process, and the service manager will consider the unit started when the parent process exits.
0073 ```
0074 
0075 In double-forking approach the child won't continue as main service process, but rather its own child will do. This can deceive
0076 *systemd* to think that the daemon has finished.
0077 
0078 #### No forking
0079 
0080 Many people advice to not fork at all, as it is a domain of the user to detrmine the way how the program shall be run. By this view
0081 program should not daemonize itself. Instead it can be run as a background process (by putting `&` after the command) and tools such
0082 as *screen* and *nohup* can be used to prevent process from being killed when user logs out [[4]].
0083 
0084 ---
0085 
0086 There are some valid points in each of these views, so *cutehmi.daemon.3* allows you to pick number of forks by setting
0087 `--forks={number}` parameter. For *systemd* service pick `--forks=1` and use `Type=forking` or `--forks=0` with `Type=simple`.
0088 Note that `--forks=0` is not the same as application mode (`--app` option) - daemon uses system logging facility instead of
0089 standard output, responds to signals, unlocks former working directory, creates PID file, closes file descriptors and resets its
0090 umask.
0091 
0092 ## Changes
0093 
0094 Compared to previous major version following changes were made.
0095 
0096 - Daemon looks for `Daemon` QML component instead of `Main`.
0097 - Extension is specified with first positional argument instead of `--extension` argument.
0098 - Component is specified with second positional argument instead of `--component` argument.
0099 - Option `--nforks` has been renamed to `--forks`.
0100 
0101 ## References
0102 
0103 1. [Stephen A. Rago, W. Richard Stevens, "Advanced Programming in the UNIX® Environment: Second Edition", Chapter 13. Daemon Processes.][1]
0104 2. [Andries Brouwer, "The Linux kernel".][2]
0105 3. [Linux Programmer's Manual, DAEMON(3)][3]
0106 4. [Department of Health Technology, "Processes; foreground and background, ps, top, kill, screen, nohup and daemons".][4]
0107 
0108 [1]: https://learning.oreilly.com/library/view/advanced-programming-in/0201433079/
0109 [2]: https://www.win.tue.nl/~aeb/linux/lk/lk-10.html
0110 [3]: http://man7.org/linux/man-pages/man3/daemon.3.html
0111 [4]: http://teaching.healthtech.dtu.dk/unix/index.php/Processes;_foreground_and_background,_ps,_top,_kill,_screen,_nohup_and_daemons