'\" te .\" Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. .TH smf_method 5 "30 Jan 2015" "SunOS 5.11" "Standards, Environments, and Macros" .SH NAME smf_method \- service management framework conventions for methods .SH DESCRIPTION .sp .LP The class of services managed by \fBsvc.startd\fR(1M) in the service management framework, \fBsmf\fR(5), consists of applications that fit a simple \fBfork\fR(2)-\fBexec\fR(2) model. The \fBsvc.startd\fR(1M) master daemon and other restarters support the \fBfork\fR(2)-\fBexec\fR(2) model, potentially with additional capabilities. The \fBsvc.startd\fR(1M) daemon and other restarters require that the methods which activate, manipulate, or examine a service instance follow the conventions described in this manual page. .SS "Invocation form" .sp .LP The form of a method invocation is not dictated by convention. In some cases, a method invocation might consist of the direct invocation of the daemon or other binary executable that provides the service. For cases in which an executable script or other mediating executable is used, the convention recommends the form: .sp .in +2 .nf /path/to/method_executable abbr_method_name .fi .in -2 .sp .LP The \fIabbr_method_name\fR used for the recommended form is a supported method such as \fBstart\fR or \fBstop\fR. The set of methods supported by a restarter is given on the related restarter page. The \fBsvc.startd\fR(1M) daemon supports \fBstart\fR, \fBstop\fR, and \fBrefresh\fR methods. .sp .LP A restarter might define other kinds of methods beyond those referenced in this page. The conventions surrounding such extensions are defined by the restarter and might not be identical to those given here. .SS "Environment Variables" .sp .LP The restarter provides four environment variables to the method that determine the context in which the method is invoked. .sp .ne 2 .mk .na \fB\fBSMF_FMRI\fR\fR .ad .sp .6 .RS 4n The service fault management resource identifier (FMRI) of the instance for which the method is invoked. .RE .sp .ne 2 .mk .na \fB\fBSMF_METHOD\fR\fR .ad .sp .6 .RS 4n The full name of the method being invoked, such as \fBstart\fR or \fBstop\fR. .RE .sp .ne 2 .mk .na \fB\fBSMF_RESTARTER\fR\fR .ad .sp .6 .RS 4n The service FMRI of the restarter that invokes the method. .RE .sp .ne 2 .mk .na \fB\fBSMF_ZONENAME\fR\fR .ad .sp .6 .RS 4n The name of the zone in which the method is running. This can also be obtained by using the \fBzonename\fR(1) command. .RE .sp .LP These variables should be removed from the environment prior to the invocation of any persistent process by the method. A convenience shell function, \fBsmf_clear_env\fR, is given for service authors who use Bourne-compatible shell scripting to compose service methods in the include file described below. .sp .LP The method context can cause other environment variables to be set as described below. .SS "Method Definition" .sp .LP A method is defined minimally by three properties in a propertygroup of type \fBmethod\fR. .sp .LP These properties are: .sp .ne 2 .mk .na \fBexec (\fIastring\fR)\fR .ad .RS 27n .rt Method executable string. .RE .sp .ne 2 .mk .na \fBtimeout_seconds (\fIcount\fR)\fR .ad .RS 27n .rt Number of seconds before method times out. See the \fBTimeouts\fR section for more detail. .RE .sp .ne 2 .mk .na \fBtype (\fIastring\fR)\fR .ad .RS 27n .rt Method type. Currently always set to \fBmethod\fR. .RE .sp .LP A Method Context can be defined to further refine the execution environment of the method. See the \fBMethod Context\fR section for more information. .SS "Method Tokens" .sp .LP When defined in the \fBexec\fR string of the method by the restarter \fBsvc.startd\fR, a set of tokens are parsed and expanded with appropriate value. Other restarters might not support method tokens. The delegated restarter for inet services, \fBinetd\fR(1M), does not support the following method expansions. .sp .ne 2 .mk .na \fB\fB%%\fR\fR .ad .sp .6 .RS 4n % .RE .sp .ne 2 .mk .na \fB\fB%r\fR\fR .ad .sp .6 .RS 4n Name of the restarter, such as \fBsvc.startd\fR .RE .sp .ne 2 .mk .na \fB\fB%m\fR\fR .ad .sp .6 .RS 4n The full name of the method being invoked, such as \fBstart\fR or \fBstop\fR. .RE .sp .ne 2 .mk .na \fB\fB%s\fR\fR .ad .sp .6 .RS 4n Name of the service .RE .sp .ne 2 .mk .na \fB\fB%i\fR\fR .ad .sp .6 .RS 4n Name of the instance .RE .sp .ne 2 .mk .na \fB\fB%f\fR\fR .ad .sp .6 .RS 4n FMRI of the instance .RE .sp .ne 2 .mk .na \fB\fB%{prop[:,]}\fR\fR .ad .sp .6 .RS 4n Value(s) of a property. The \fBprop\fR might be a property FMRI, a property group name and a property name separated by a \fB/\fR, or a property name in the \fBapplication\fR property group. These values can be followed by a \fB,\fR (comma) or \fB:\fR (colon). If present, the separators are used to separate multiple values. If absent, a space is used. The following shell metacharacters encountered in string values are quoted with a \e (backslash): .sp .in +2 .nf ; & ( ) | ^ < > newline space tab \e " ' .fi .in -2 An invalid expansion constitutes method failure. .RE .sp .LP Two explicit tokens can be used in the place of method commands. .sp .ne 2 .mk .na \fB\fB:kill [-signal]\fR\fR .ad .sp .6 .RS 4n Sends the specified signal, which is \fBSIGTERM\fR by default, to all processes in the primary instance contract. Always returns \fBSMF_EXIT_OK\fR. This token should be used to replace common \fBpkill\fR invocations. .RE .sp .ne 2 .mk .na \fB\fB:true\fR\fR .ad .sp .6 .RS 4n Always returns \fBSMF_EXIT_OK\fR. This token should be used for methods that are required by the restarter but which are unnecessary for the particular service implementation. .RE .SS "Exiting and Exit Status" .sp .LP The required behavior of a start method is to delay exiting until the service instance is ready to answer requests or is otherwise functional. .sp .LP The following exit status codes are defined in \fB\fR and in the shell support file. .sp .sp .TS tab(); lw(1.74i) lw(.9i) lw(2.86i) lw(1.74i) lw(.9i) lw(2.86i) . \fBSMF_EXIT_OK\fR\fB0\fRT{ Method exited, performing its operation successfully. T} \fBSMF_EXIT_ERR_FATAL\fR\fB95\fRT{ Method failed fatally and is unrecoverable without administrative intervention. T} \fBSMF_EXIT_ERR_CONFIG\fR\fB96\fRT{ Unrecoverable configuration error. A common condition that returns this exit status is the absence of required configuration files for an enabled service instance. T} \fBSMF_EXIT_ERR_NOSMF\fR\fB99\fRT{ Method has been mistakenly invoked outside the \fBsmf\fR(5) facility. Services that depend on \fBsmf\fR(5) capabilities should exit with this status value. T} \fBSMF_EXIT_ERR_PERM\fR\fB100\fRT{ Method requires a form of permission such as file access, privilege, authorization, or other credential that is not available when invoked. T} \fBSMF_EXIT_ERR_OTHER\fR\fBnon-zero\fRT{ Any non-zero exit status from a method is treated as an unknown error. A series of unknown errors can be diagnosed as a fault by the restarter or on behalf of the restarter. T} .TE .sp .LP In addition to the exit codes described above, a method may use the following exit codes in conjunction with \fBsmf_method_exit()\fR, available via \fBsmf_method_exit\fR(3SCF), \fBsmf_include.sh\fR, and the \fBsmf_include.py\fR Python module: .sp .sp .TS tab(); lw(1.74i) lw(.9i) lw(2.86i) lw(1.74i) lw(.9i) lw(2.86i) . \fBSMF_EXIT_TEMP_DISABLE\fR\fB101\fRT{ Method exits successfully and requests a temporary disable. T} \fBSMF_EXIT_TEMP_TRANSIENT\fR\fB102\fRT{ Method exits successfully and requests that it be treated as if its service model was "transient". T} .TE .sp .LP Use of a precise exit code allows the responsible restarter to categorize an error response as likely to be intermittent and worth pursuing restart or permanent and request administrative intervention. .SS "Timeouts" .sp .LP Each method can have an independent timeout, specified in seconds. The method timeout is specified by the \fBtimeout_seconds\fR property. .sp .LP A timeout is used as a last resort for the service's restarter to determine that a method has hung or is not making progress. If a timeout elapsed, many restarters place the service into the maintenance state. See \fBsvc.startd\fR(1M). A significant margin of error is recommended when specifying a timeout in order to avoid premature failures when the method is making progress, but the system is temporarily responding very slowly due to memory, CPU, or I/O load. .sp .LP \fB60\fR seconds is a good starting value for methods expected to take only a second or two. \fB300\fR seconds (5 minutes) is appropriate for a method which commonly takes \fB30\fR seconds. Scale up as appropriate for methods which routinely take longer. .sp .LP Shorter timeouts can be used if fast failure is desired in order to prompt administrative intervention. If administrative intervention is likely to only be to clear the service and start the method again, consider a longer timeout. .sp .LP If \fBtimeout_seconds\fR is set to \fB0\fR, there is no timeout for the service. This setting is not preferred, but is available for services which absolutely require it. \fB-1\fR is also accepted to specify no timeout, but is deprecated. .SS "Shell Programming Support" .sp .LP A set of environment variables that define the above exit status values is provided with convenience shell functions in the file \fB/lib/svc/share/smf_include.sh\fR. This file is a Bourne shell script suitable for inclusion via the source operator in any Bourne-compatible shell. .sp .LP To assist in the composition of scripts that can serve as SMF methods as well as \fB/etc/init.d\fR scripts, the \fBsmf_present()\fR shell function is provided. If the \fBsmf\fR(5) facility is not available, \fBsmf_present()\fR returns a non-zero exit status. .sp .LP One possible structure for such a script follows: .sp .in +2 .nf if smf_present; then # Shell code to run application as managed service .... smf_clear_env else # Shell code to run application as /etc/init.d script .... fi .fi .in -2 .sp .LP This example shows the use of both convenience functions that are provided. .SS "Python Programming Support" .sp .LP The same set of exit statuses provided by \fB/lib/svc/share/smf_include.sh\fR are available in the \fBsmf_include\fR module. .SS "Method Context" .sp .LP The service management facility offers a common mechanism to set the context in which the \fBfork\fR(2)-\fBexec\fR(2) model services execute. .sp .LP The desired method context should be provided by the service developer. All service instances should run with the lowest level of privileges possible to limit potential security compromises. .sp .LP A method context can contain the following properties: .sp .ne 2 .mk .na \fBenvironment\fR .ad .sp .6 .RS 4n Environment variables to insert into the environment of the method, in the form of a number of \fBNAME=value\fR strings. .RE .sp .ne 2 .mk .na \fB\fBprofile\fR\fR .ad .sp .6 .RS 4n The name of an RBAC (role-based access control) profile which, along with the method executable, identifies an entry in \fBexec_attr\fR(4). .RE .sp .ne 2 .mk .na \fB\fBuser\fR\fR .ad .sp .6 .RS 4n The user ID in numeric or text form. .RE .sp .ne 2 .mk .na \fB\fBgroup\fR\fR .ad .sp .6 .RS 4n The group ID in numeric or text form. .RE .sp .ne 2 .mk .na \fB\fBsupp_groups\fR\fR .ad .sp .6 .RS 4n An optional string that specifies the supplemental group memberships by ID, in numeric or text form. .RE .sp .ne 2 .mk .na \fB\fBprivileges\fR\fR .ad .sp .6 .RS 4n An optional string specifying the privilege set as defined in \fBprivileges\fR(5). An Extended Policy can be specified here. .RE .sp .ne 2 .mk .na \fB\fBlimit_privileges\fR\fR .ad .sp .6 .RS 4n An optional string specifying the limit privilege set as defined in \fBprivileges\fR(5). .RE .sp .ne 2 .mk .na \fB\fBworking_directory\fR\fR .ad .sp .6 .RS 4n The home directory from which to launch the method. \fB:home\fR can be used as a token to indicate the home directory of the user whose \fBuid\fR is used to launch the method. If the property is unset, \fB:home\fR is used. .RE .sp .ne 2 .mk .na \fB\fBproject\fR\fR .ad .sp .6 .RS 4n The project ID in numeric or text form. \fB:default\fR can be used as a token to indicate a project identified by \fBgetdefaultproj\fR(3PROJECT) for the user whose \fBuid\fR is used to launch the method. .RE .sp .ne 2 .mk .na \fB\fBresource_pool\fR\fR .ad .sp .6 .RS 4n The resource pool name on which to launch the method. \fB:default\fR can be used as a token to indicate the pool specified in the \fBproject\fR(4) entry given in the \fBproject\fR attribute above. .RE .sp .LP The method context can be set for the entire service instance by specifying a \fBmethod_context\fR property group for the service or instance. A method might override the instance method context by providing the method context properties on the method property group. .sp .LP Invalid method context settings always lead to failure of the method, with the exception of invalid environment variables that issue warnings. .sp .LP In addition to the context defined above, many \fBfork\fR(2)-\fBexec\fR(2) model restarters also use the following conventions when invoking executables as methods: .sp .ne 2 .mk .na \fBArgument array\fR .ad .sp .6 .RS 4n The arguments in \fBargv[]\fR are set consistently with the result of \fB/bin/sh -c\fR of the \fBexec\fR string. .RE .sp .ne 2 .mk .na \fBFile descriptors\fR .ad .sp .6 .RS 4n File descriptor \fB0\fR is \fB/dev/null\fR. File descriptors \fB1\fR and \fB2\fR are recommended to be a per-service log file. .RE .SH FILES .sp .ne 2 .mk .na \fB\fB/lib/svc/share/smf_include.sh\fR\fR .ad .br .na \fB\fB/lib/svc/share/smf_exit_codes.sh\fR\fR .ad .br .na \fB\fB/usr/lib/\fIpython-version\fR/vendor-packages/smf_include.py\fR\fR .ad .sp .6 .RS 4n Definitions of exit status values. .RE .sp .ne 2 .mk .na \fB\fB/usr/include/libscf.h\fR\fR .ad .sp .6 .RS 4n Definitions of exit status codes. .RE .SH EXAMPLES .LP \fBExample 1 \fRReport a service-specific configuration error message in the service log. .sp .LP A start method might want to use \fBsmf_method_exit()\fR to report a service-specific configuration error message in the service log. .sp .in +2 .nf if [ ! -s "$my_config_file" ]; then smf_method_exit $SMF_EXIT_ERR_CONFIG \e missing_or_empty_config_file \e "$my_config_file is missing or empty" fi .fi .in -2 .LP \fBExample 2 \fRDisable a service that should only run in the global zone when started in a non-global zone. .sp .LP A service that should only run in the global zone might want to disable itself when started in a non-global zone. .sp .in +2 .nf if smf_is_nonglobalzone; then smf_method_exit $SMF_EXIT_TEMP_DISABLE global_zone_only \e "$SMF_FMRI is not supported in a local zone" SUNW_OST_OSCMD fi .fi .in -2 .SH SEE ALSO .sp .LP \fBzonename\fR(1), \fBcoreadm\fR(1M), \fBinetd\fR(1M), \fBsvccfg\fR(1M), \fBsvc.startd\fR(1M), \fBexec\fR(2), \fBfork\fR(2), \fBgetdefaultproj\fR(3PROJECT), \fBsmf_method_exit\fR(3SCF), \fBexec_attr\fR(4), \fBproject\fR(4), \fBservice_bundle\fR(4), \fBattributes\fR(5), \fBprivileges\fR(5), \fBrbac\fR(5), \fBsmf\fR(5), \fBsmf_bootstrap\fR(5), \fBzones\fR(5) .SH NOTES .sp .LP The present version of \fBsmf\fR(5) does not support multiple repositories. .sp .LP When a service is configured to be started as root but with privileges different from \fBlimit_privileges\fR, the resulting process is privilege aware. This can be surprising to developers who expect \fBseteuid()\fR to reduce privileges to basic or less.