196efe3b3SAugustin Cavalier/* 296efe3b3SAugustin Cavalier * Copyright 2017 Haiku, Inc. All rights reserved. 396efe3b3SAugustin Cavalier * Distributed under the terms of the MIT License. 496efe3b3SAugustin Cavalier * 596efe3b3SAugustin Cavalier * Authors: 696efe3b3SAugustin Cavalier * Axel Dörfler, axeld@pinc-software.de 796efe3b3SAugustin Cavalier */ 896efe3b3SAugustin Cavalier 996efe3b3SAugustin Cavalier 1096efe3b3SAugustin Cavalier/*! 1196efe3b3SAugustin Cavalier \page launch_intro Introduction to the Launch Daemon 1296efe3b3SAugustin Cavalier 1396efe3b3SAugustin CavalierIn general, you should name your jobs/services after the application signature 1496efe3b3SAugustin Cavalier(if possible), and name the configuration files accordingly (in each case 1596efe3b3SAugustin Cavalierwithout the "application/" prefix). Alternatively, you may use the name of your 1696efe3b3SAugustin Cavalierpackage as file name. If you do so, you may want to include the version, too, 1796efe3b3SAugustin Cavalierbut do not add the version to the name of a job. 1896efe3b3SAugustin Cavalier 1996efe3b3SAugustin CavalierA "service" is re-started automatically by the launch_daemon as soon as it's 2096efe3b3SAugustin Cavalierquit (or has crashed). Use a "job" instead, if you don't want this behavior. 2196efe3b3SAugustin Cavalier 2296efe3b3SAugustin CavalierLet's start with a simple example: 2396efe3b3SAugustin Cavalier<code> 2496efe3b3SAugustin Cavalierservice x-vnd.MyGreatServer { 2596efe3b3SAugustin Cavalier} 2696efe3b3SAugustin Cavalier</code> 2796efe3b3SAugustin CavalierThis will register a service named MyGreatServer, and assumes it uses a BServer 2896efe3b3SAugustin Cavalierbased application object. It will automatically launch the server either 2996efe3b3SAugustin Cavalierduring system boot (when you place your configuration file in 3096efe3b3SAugustin Cavalier<code>/system/data/launch/</code>), or after user login (when it's in 3196efe3b3SAugustin Cavalier<code>~/config/data/launch/</code>) via its signature (which it constructs 3296efe3b3SAugustin Cavalierusing the job's name). Furthermore, it will create a port for the server, so 3396efe3b3SAugustin Cavalierthat it can be immediately be talked to. 3496efe3b3SAugustin Cavalier 3596efe3b3SAugustin CavalierUnfortunately, BServer is private as of now, and you didn't want to make a 3696efe3b3SAugustin Cavaliergreat effort to subclass it. In this case, you have to notify the launch_daemon 3796efe3b3SAugustin Cavalierof this fact by adding a "legacy" to that configuration: 3896efe3b3SAugustin Cavalier\code 3996efe3b3SAugustin Cavalierservice x-vnd.MyGreatServer { 4096efe3b3SAugustin Cavalier legacy 4196efe3b3SAugustin Cavalier} 4296efe3b3SAugustin Cavalier\endcode 4396efe3b3SAugustin CavalierIf you want to save the cycles for querying for your server, you can also 4496efe3b3SAugustin Cavalierdirectly specify the file that should be launched; in this case, the job name 4596efe3b3SAugustin Cavalieris just a name. This could look like this: 4696efe3b3SAugustin Cavalier\code 4796efe3b3SAugustin Cavalierservice x-vnd.MyGreatServer { 4896efe3b3SAugustin Cavalier launch /path/to/my/great/server 4996efe3b3SAugustin Cavalier legacy 5096efe3b3SAugustin Cavalier} 5196efe3b3SAugustin Cavalier\endcode 5296efe3b3SAugustin CavalierThis method also allows you to add additional launch arguments like this: 5396efe3b3SAugustin Cavalier\code 5496efe3b3SAugustin Cavalierservice x-vnd.MyGreatServer { 5596efe3b3SAugustin Cavalier launch /path/to/my/great/server --debug-mode 5696efe3b3SAugustin Cavalier legacy 5796efe3b3SAugustin Cavalier} 5896efe3b3SAugustin Cavalier\endcode 5996efe3b3SAugustin CavalierIf you do not want to enable the service by default, but only provide a 6096efe3b3SAugustin Cavaliertemplate or basic configuration for the user, you can disable it like this: 6196efe3b3SAugustin Cavalier\code 6296efe3b3SAugustin Cavalierservice x-vnd.MyGreatServer { 6396efe3b3SAugustin Cavalier launch /path/to/my/great/server --debug-mode 6496efe3b3SAugustin Cavalier legacy 6596efe3b3SAugustin Cavalier disabled 6696efe3b3SAugustin Cavalier} 6796efe3b3SAugustin Cavalier\endcode 6896efe3b3SAugustin CavalierYou can then override this in the settings by redefining the service, and 6996efe3b3SAugustin Cavalieroverwrite the parts you want to change. In this case, it might just be: 7096efe3b3SAugustin Cavalier\code 7196efe3b3SAugustin Cavalierservice x-vnd.MyGreatServer { 7296efe3b3SAugustin Cavalier disabled false 7396efe3b3SAugustin Cavalier} 7496efe3b3SAugustin Cavalier\endcode 7596efe3b3SAugustin CavalierThe rest of the configuration will stay intact. 7696efe3b3SAugustin Cavalier 7796efe3b3SAugustin CavalierIf you only want to launch your application depending on the current 7896efe3b3SAugustin Cavalierenvironment, you can define conditions which must be met to launch your 7996efe3b3SAugustin Cavalierapplication at all, and events which will trigger launching your application. 8096efe3b3SAugustin Cavalier 8196efe3b3SAugustin CavalierIn the configuration file, this could look like this: 8296efe3b3SAugustin Cavalier\code 8396efe3b3SAugustin Cavalierservice x-vnd.MyGreatServer { 8496efe3b3SAugustin Cavalier launch /path/to/my/great/server 8596efe3b3SAugustin Cavalier if { 8696efe3b3SAugustin Cavalier not safemode 8796efe3b3SAugustin Cavalier file_exists /path/to/license/file 8896efe3b3SAugustin Cavalier } 8996efe3b3SAugustin Cavalier on { 9096efe3b3SAugustin Cavalier demand 9196efe3b3SAugustin Cavalier } 9296efe3b3SAugustin Cavalier} 9396efe3b3SAugustin Cavalier\endcode 9496efe3b3SAugustin CavalierAlternatively, there are shortcuts for two of the above items, and you could 9596efe3b3SAugustin Cavalieralso write it like this: 9696efe3b3SAugustin Cavalier\code 9796efe3b3SAugustin Cavalierservice x-vnd.MyGreatServer { 9896efe3b3SAugustin Cavalier launch /path/to/my/great/server 9996efe3b3SAugustin Cavalier if { 10096efe3b3SAugustin Cavalier file_exists /path/to/license/file 10196efe3b3SAugustin Cavalier } 10296efe3b3SAugustin Cavalier not_safemode 10396efe3b3SAugustin Cavalier on_demand 10496efe3b3SAugustin Cavalier} 10596efe3b3SAugustin Cavalier\endcode 10696efe3b3SAugustin CavalierIf you have only single line conditions/events, you may also omit the curly 10796efe3b3SAugustin Cavalierbraces completely: 10896efe3b3SAugustin Cavalier\code 10996efe3b3SAugustin Cavalierservice x-vnd.MyGreatServer { 11096efe3b3SAugustin Cavalier launch /path/to/my/great/server 11196efe3b3SAugustin Cavalier if file_exists /path/to/license/file 11296efe3b3SAugustin Cavalier not_safemode 11396efe3b3SAugustin Cavalier on demand 11496efe3b3SAugustin Cavalier} 11596efe3b3SAugustin Cavalier\endcode 11696efe3b3SAugustin CavalierNote, the "on demand" does not use the "on_demand" shortcut, but just saves the 11796efe3b3SAugustin Cavaliercurly braces of "on { demand }". 11896efe3b3SAugustin Cavalier 11996efe3b3SAugustin CavalierYou can also use operators like "and", "or", and "not" in conditions. If you 12096efe3b3SAugustin Cavalierput more than one condition into an "if" they must all be true in order to meet 12196efe3b3SAugustin Cavalierthe condition. Conditions will be evaluated every time the launch_daemon has a 12296efe3b3SAugustin Cavalierreason to start your application -- the outcome of the condition may change 12396efe3b3SAugustin Cavalierover time. 12496efe3b3SAugustin Cavalier 12596efe3b3SAugustin CavalierLikewise, if you put more than one event into an "on" only one of them needs to 12696efe3b3SAugustin Cavalierbe triggered in order to launch your job. While the event processing is already 12796efe3b3SAugustin Cavalierprepared to allow for an "and" operator, this is not yet available. 12896efe3b3SAugustin Cavalier 12996efe3b3SAugustin CavalierYou can also specify the environment variables for your application. They can 13096efe3b3SAugustin Cavalierbe either specified directly, or you can let a shell script define them for you: 13196efe3b3SAugustin Cavalier\code 13296efe3b3SAugustin Cavalierservice x-vnd.MyGreatServer { 13396efe3b3SAugustin Cavalier env { 13496efe3b3SAugustin Cavalier from_script /path/to/my/script.sh 135*ba0223daSAugustin Cavalier LC_CTYPE C.UTF-8 13696efe3b3SAugustin Cavalier } 13796efe3b3SAugustin Cavalier launch /path/to/my/great/server 13896efe3b3SAugustin Cavalier} 13996efe3b3SAugustin Cavalier\endcode 14096efe3b3SAugustin CavalierIf you want to move the job into a specific target, you can write it like this: 14196efe3b3SAugustin Cavalier\code 14296efe3b3SAugustin Cavaliertarget my-target { 14396efe3b3SAugustin Cavalier service x-vnd.MyGreatServer { 14496efe3b3SAugustin Cavalier launch /path/to/my/great/server 14596efe3b3SAugustin Cavalier } 14696efe3b3SAugustin Cavalier} 14796efe3b3SAugustin Cavalier\endcode 14896efe3b3SAugustin CavalierYou will be able to move jobs into targets around in the configuration files, 14996efe3b3SAugustin Cavalierbut this has not been implemented at the time of this writing. 15096efe3b3SAugustin Cavalier 15196efe3b3SAugustin CavalierLike jobs, and services, targets can use conditions, events, and environment 15296efe3b3SAugustin Cavaliervariables. If you do not specify an event for your target, it will not launch 15396efe3b3SAugustin Cavalierautomatically unless it's requested by name. Furthermore, jobs within your 15496efe3b3SAugustin Cavaliertarget will not be available unless the target has been launched either 15596efe3b3SAugustin Cavaliermanually or by event. 15696efe3b3SAugustin Cavalier 15796efe3b3SAugustin CavalierYou can also let the launch_daemon create resources for your application, like 15896efe3b3SAugustin Cavalieradditional named ports. These ports will be available to other applications 15996efe3b3SAugustin Cavalierwithout having to launch your application, and will belong to the launch_daemon 16096efe3b3SAugustin Cavalierthroughout their lifetime. 16196efe3b3SAugustin Cavalier 16296efe3b3SAugustin CavalierFinally, there is a "run" directive that you can use to launch targets 16396efe3b3SAugustin Cavalierunconditionally. The "run" directive also allows to use conditions, but adds 16496efe3b3SAugustin Cavalierthe additional keywords "then", and "else" like this: 16596efe3b3SAugustin Cavalier\code 16696efe3b3SAugustin Cavalierrun { 16796efe3b3SAugustin Cavalier if read_only 16896efe3b3SAugustin Cavalier then installer 16996efe3b3SAugustin Cavalier else desktop 17096efe3b3SAugustin Cavalier} 17196efe3b3SAugustin Cavalier\endcode 17296efe3b3SAugustin CavalierYou can also use curly braces if you like or want to run more than one job. 17396efe3b3SAugustin Cavalier 17496efe3b3SAugustin CavalierIt's a good idea to look at the existing configuration files to get an idea how 17596efe3b3SAugustin Cavalierthis is all supposed to work. 17696efe3b3SAugustin Cavalier*/ 177