xref: /illumos-gate/usr/src/man/man7/smf_method.7 (revision e8d3efa1c56e5f2b5368600a2baeb7b1d54a07f8)
1.\" The contents of this file are subject to the terms of the Common
2.\" Development and Distribution License (the "License").  You may not use
3.\" this file except in compliance with the License. You can obtain a copy
4.\" of the license at usr/src/OPENSOLARIS.LICENSE or
5.\" http://www.opensolaris.org/os/licensing.
6.\"
7.\"  See the License for the specific language governing permissions and
8.\" limitations under the License. When distributing Covered Code, include
9.\" this CDDL HEADER in each file and include the License file at
10.\" usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below
11.\" this CDDL HEADER, with
12.\"
13.\" the fields enclosed by brackets "[]" replaced with your own identifying
14.\" information: Portions Copyright [yyyy] [name of copyright owner]
15.\"
16.\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
17.\" Copyright 2017 RackTop Systems.
18.\" Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
19.\" Copyright 2026 Oxide Computer Company
20.\"
21.Dd July 18, 2026
22.Dt SMF_METHOD 7
23.Os
24.Sh NAME
25.Nm smf_method
26.Nd service management framework conventions for methods
27.Sh DESCRIPTION
28The class of services managed by
29.Xr svc.startd 8
30in the service management framework,
31.Xr smf 7 ,
32consists of applications that fit a simple
33.Sm off
34.Xr fork 2
35-
36.Xr exec 2
37.Sm on
38model.
39The
40.Xr svc.startd 8
41master daemon and other restarters support the
42.Sm off
43.Xr fork 2
44-
45.Xr exec 2
46.Sm on
47model, potentially with additional capabilities.
48The
49.Xr svc.startd 8
50daemon and other restarters require that the methods which activate,
51manipulate, or examine a service instance follow the conventions described in
52this manual page.
53.Ss Invocation Form
54The form of a method invocation is not dictated by convention.
55In some cases, a method invocation might consist of the direct invocation of
56the daemon or other binary executable that provides the service.
57For cases in which an executable script or other mediating executable is used,
58the convention recommends the form:
59.Pp
60.Dl /path/to/method_executable abbr_method_name
61.Pp
62The
63.Ar abbr_method_name
64used for the recommended form is a supported method such as
65.Cm start
66or
67.Cm stop .
68The set of methods supported by a restarter is given on the related restarter
69page.
70The
71.Xr svc.startd 8
72daemon supports
73.Cm start ,
74.Cm stop ,
75and
76.Cm refresh
77methods.
78.Pp
79A restarter might define other kinds of methods beyond those referenced in this
80page.
81The conventions surrounding such extensions are defined by the restarter and
82might not be identical to those given here.
83.Ss Environment Variables
84The restarter provides four environment variables to the method that determine
85the context in which the method is invoked.
86.Bl -tag -width Ev
87.It Sy SMF_FMRI
88The service fault management resource identifier
89.Pq FMRI
90of the instance for which the method is invoked.
91.It Sy SMF_METHOD
92The full name of the method being invoked, such as
93.Cm start
94or
95.Cm stop .
96.It Sy SMF_RESTARTER
97The service FMRI of the restarter that invokes the method
98.It Sy SMF_ZONENAME
99The name of the zone in which the method is running.
100This can also be obtained by using the
101.Xr zonename 1
102command.
103.El
104.Pp
105These variables should be removed from the environment prior to the invocation
106of any persistent process by the method.
107A convenience shell function,
108.Fn smf_clear_env ,
109is given for service authors who use Bourne-compatible shell scripting to
110compose service methods in the include file described below.
111.Pp
112The method context can cause other environment variables to be set as described
113below.
114.Ss Method Definition
115A method is defined minimally by three properties in a property group of type
116.Sy method .
117.Pp
118These properties are:
119.Bl -tag -width timeout_seconds_(count)
120.It Sy exec Pq Ar astring
121Method executable string.
122.It Sy timeout_seconds Pq Ar count
123Number of seconds before method times out.
124See the
125.Sy Timeouts
126section for more detail.
127.It Sy type Pq Ar astring
128Method type.
129Currently always set to
130.Sy method .
131.El
132.Pp
133A Method Context can be defined to further refine the execution environment of
134the method.
135See the
136.Sx Method Context
137section for more information.
138If no Method Context is present then the service is started as root, with user
139ID and group ID set to 0 and full privileges in effect, and in the system root
140directory,
141.Pa / .
142However, if a Method Context is used then the defaults described below in
143.Sx Method Context
144apply.
145Note that in particular, for services started as root, this may cause a change
146of working directory to
147.Pa /root .
148.Ss Method Tokens
149When defined in the
150.Sy exec
151string of the method by the restarter
152.Xr svc.startd 8 ,
153a set of tokens are parsed and expanded with appropriate value.
154Other restarters might not support method tokens.
155For example, the delegated restarter for inet services,
156.Xr inetd 8 ,
157does
158.Em not
159support the following method expansions.
160.Bl -tag -width xxxxxxxxxxx
161.It Sy %%
162%
163.It Sy %r
164Name of the restarter, such as
165.Sy svc.startd
166.It Sy %m
167The full name of the method being invoked, such as
168.Cm start
169or
170.Cm stop .
171.It Sy %s
172Name of the service
173.It Sy %i
174Name of the instance
175.It Sy %f
176FMRI of the instance
177.It Sy %{prop[:,]}
178Value(s) of a property.
179The
180.Sy prop
181might be a property FMRI, a property group name and a property name separated
182by a /, or a property name in the
183.Sy application
184property group.
185These values can be followed by a ,
186.Pq comma
187or :
188.Pq colon .
189If present, the separators are used to separate multiple values.
190If absent, a space is used.
191The following shell metacharacters encountered in string values are quoted with
192a \e
193.Pq backslash :
194.Pp
195.D1 \&; & \&( \&) \&| ^ < > newline space tab \e \&" ' \(ga
196.Pp
197An invalid expansion constitutes method failure.
198.El
199.Pp
200Two explicit tokens can be used in the place of method commands.
201.Bl -tag -width :kill_[-signal]
202.It Ic :kill Op Fl signal
203Sends the specified signal, which is
204.Dv SIGTERM
205by default, to all processes in the primary instance contract.
206Always returns
207.Sy SMF_EXIT_OK .
208This token should be used to replace common
209.Xr pkill 1
210invocations.
211.It Ic :true
212Always returns
213.Sy SMF_EXIT_OK .
214This token should be used for methods that are required by the restarter but
215which are unnecessary for the particular service implementation.
216.El
217.Ss Exiting and Exit Status
218The required behavior of a start method is to delay exiting until the service
219instance is ready to answer requests or is otherwise functional.
220.Pp
221The following exit status codes are defined in
222.In libscf.h
223and in the shell support file.
224.Bl -column "SMF_EXIT_TEMP_DISABLE" "non-zero"
225.It Sy SMF_EXIT_OK Ta Sy 0 Ta
226Method exited, performing its operation successfully.
227.It Sy SMF_EXIT_NODAEMON Ta Sy 94 Ta
228Method exited successfully but purposefully leaves no processes remaining in
229the contract; it should be treated as if it had a transient service model.
230.It Sy SMF_EXIT_ERR_FATAL Ta Sy 95 Ta
231Method failed fatally and is irrecoverable without administrative
232intervention.
233.It Sy SMF_EXIT_ERR_CONFIG Ta Sy 96 Ta
234Unrecoverable configuration error.
235A common condition that returns this exit status is the absence of required
236configuration files for an enabled service instance.
237.It Sy SMF_EXIT_MON_DEGRADE Ta Sy 97 Ta
238Method encountered some problems; the service may not be fully functional.
239.It Sy SMF_EXIT_ERR_NOSMF Ta Sy 99 Ta
240Method has been mistakenly invoked outside the
241.Xr smf 7
242facility.
243Services that depend on
244.Xr smf 7
245capabilities should exit with this status value in that situation.
246.It Sy SMF_EXIT_ERR_PERM Ta Sy 100 Ta
247Method requires a form of permission such as file access, privilege,
248authorization, or other credential that is not available when invoked.
249.It Sy SMF_EXIT_TEMP_DISABLE Ta Sy 101 Ta
250Method requests that the service instance be temporarily disabled.
251This is honoured only for start methods.
252A method may use this when it determines that the service should not run in the
253current environment.
254The disable does not persist across a reboot, so the method runs again on the
255next boot in case the condition that prompted it has since cleared.
256The instance can also be re-enabled at any time using
257.Xr svcadm 8 .
258This is equivalent to the service running
259.Ql svcadm disable -t
260on itself, but is performed atomically by the restarter.
261See
262.Fn smf_method_exit
263below for a convenient way to record the reason for the disable.
264.It Sy SMF_EXIT_ERR_OTHER Ta non-zero Ta
265Any non-zero exit status from a method is treated as an unknown error.
266A series of unknown errors can be diagnosed as a fault by the restarter or on
267behalf of the restarter.
268.El
269.Pp
270Use of a precise exit code allows the responsible restarter to categorize an
271error response as likely to be intermittent and worth pursuing restart or
272permanent and request administrative intervention.
273.Ss Timeouts
274Each method can have an independent timeout, given in seconds.
275The choice of a particular timeout should be based on site expectations for
276detecting a method failure due to non-responsiveness.
277Sites with replicated filesystems or other failover resources can elect to
278lengthen method timeouts from the default.
279Sites with no remote resources can elect to shorten the timeouts.
280Method timeout is specified by the
281.Sy timeout_seconds
282property.
283.Pp
284If you specify a
285.Sy timeout_seconds
286of 0 for a method, it declares to the restarter that there is no timeout for
287the service.
288This setting is not preferred, but is available for services that absolutely
289require it.
290.Pp
291-1 is also accepted, but is a deprecated specification.
292.Ss Shell Programming Support
293A set of environment variables that define the above exit status values is
294provided with convenience shell functions in the file
295.Pa /lib/svc/share/smf_include.sh .
296This file is a Bourne shell script suitable for inclusion via the source
297operator in any Bourne-compatible shell.
298.Pp
299To assist in the composition of scripts that can serve as SMF methods as well
300as
301.Pa /etc/init.d
302scripts, the
303.Fn smf_present
304shell function is provided.
305If the
306.Xr smf 7
307facility is not available,
308.Fn smf_present
309returns a non-zero exit status.
310.Pp
311One possible structure for such a script follows:
312.Bd -literal -offset indent
313if smf_present; then
314	# Shell code to run application as managed service
315	...
316	smf_clear_env
317else
318	# Shell code to run application as /etc/init.d script
319	...
320fi
321.Ed
322.Pp
323This example shows the use of both convenience functions that are provided.
324.Pp
325The
326.Fn smf_method_exit
327function records a human-readable
328.Ar reason
329for the action a method is taking and then exits with the provided
330.Ar exit_status :
331.Bd -literal -offset indent
332smf_method_exit exit_status reason
333.Ed
334.Pp
335It is intended for use with
336.Sy SMF_EXIT_TEMP_DISABLE .
337The
338.Ar reason
339is stored in the
340.Sy general_ovr/comment
341property of the instance, the same property used by the
342.Fl c
343option of
344.Xr svcadm 8 ,
345and is displayed by
346.Xr svcs 1
347in the
348.Fl x
349and
350.Fl l
351output while the instance remains disabled.
352Like the temporary disable itself, this property does not persist across a
353reboot and is removed automatically when the instance is re-enabled.
354.Ss Method Context
355The service management facility offers a common mechanism to set the context in
356which the
357.Sm off
358.Xr fork 2
359-
360.Xr exec 2
361.Sm on
362model services execute.
363.Pp
364The desired method context should be provided by the service developer.
365All service instances should run with the lowest level of privileges possible
366to limit potential security compromises.
367.Pp
368A method context can contain the following properties:
369.Bl -tag -width working_directory
370.It Sy use_profile
371A boolean that specifies whether the profile should be used instead of the
372.Sy user ,
373.Sy group ,
374.Sy privileges ,
375and
376.Sy limit_privileges
377properties.
378.It Sy environment
379Environment variables to insert into the environment of the method, in the form
380of a number of NAME=value strings.
381.It Sy profile
382The name of an RBAC
383.Pq role-based access control
384profile which, along with the method executable, identifies an entry in
385.Xr exec_attr 5 .
386.It Sy user
387The user ID in numeric or text form.
388.It Sy group
389The group ID in numeric or text form.
390.It Sy supp_groups
391An optional string that specifies the supplemental group memberships by ID, in
392numeric or text form.
393.It Sy privileges
394An optional string specifying the privilege set as defined in
395.Xr privileges 7 .
396.It Sy limit_privileges
397An optional string specifying the limit privilege set as defined in
398.Xr privileges 7 .
399.It Sy working_directory
400The home directory from which to launch the method.
401.Cm :home
402can be used as a token to indicate the home directory of the user whose
403.Sy uid
404is used to launch the method.
405If the property is unset, the default depends on the presence of the
406.Sy use_profile ,
407.Sy profile ,
408.Sy user ,
409and
410.Sy group
411properties.
412If any of these are set then
413.Cm :home
414is used, otherwise the method starts in the root directory,
415.Pa / .
416.It Sy security_flags
417The security flags to apply when launching the method.
418See
419.Xr security-flags 7 .
420.Pp
421The
422.Dq default
423keyword specifies those flags specified in svc:/system/process-security.
424The
425.Dq all
426keyword enables all flags, the
427.Dq none
428keyword enables no flags.
429The
430.Dq current
431keyword specifies the current flags.
432Flags may be added by specifying their name
433.Pq optionally preceded by Sq \&+ ,
434and removed by preceding their name with
435.Sq \&- .
436.Pp
437Use of
438.Dq all
439has associated risks, as future versions of the system may include further
440flags which may harm poorly implemented software.
441.It Sy corefile_pattern
442An optional string that specifies the corefile pattern to use for the service,
443as per
444.Xr coreadm 8 .
445Most restarters supply a default.
446Setting this property overrides local customizations to the global core pattern.
447.It Sy project
448The project ID in numeric or text form.
449.Cm :default
450can be used as a token to indicate a project identified by
451.Xr getdefaultproj 3PROJECT
452for the user whose
453.Sy uid
454is used to launch the method.
455.It Sy resource_pool
456The resource pool name on which to launch the method.
457.Cm :default
458can be used as a token to indicate the pool specified in the
459.Xr project 5
460entry given in the
461.Sy project
462attribute above.
463.El
464.Pp
465The method context can be set for the entire service instance by specifying a
466.Sy method_context
467property group for the service or instance.
468A method might override the instance method context by providing the method
469context properties on the method property group.
470.Pp
471Invalid method context settings always lead to failure of the method, with the
472exception of invalid environment variables that issue warnings.
473.Pp
474In addition to the context defined above, many
475.Sm off
476.Xr fork 2
477-
478.Xr exec 2
479.Sm on
480model restarters also use the following conventions when invoking executables
481as methods:
482.Bl -tag -width Ds
483.It Sy Argument array
484The arguments in
485.Sy argv[]
486are set consistently with the result of
487.Sy /bin/sh -c
488of the
489.Sy exec
490string.
491.It Sy File descriptors
492File descriptor
493.Sy 0
494is
495.Pa /dev/null .
496File descriptors
497.Sy 1
498and
499.Sy 2
500are recommended to be a per-service log file.
501.El
502.Sh FILES
503.Bl -tag -width Ds
504.It Pa /lib/svc/share/smf_include.sh
505Definitions of exit status values.
506.It Pa /usr/include/libscf.h
507Definitions of exit status codes.
508.El
509.Sh SEE ALSO
510.Xr svcs 1 ,
511.Xr zonename 1 ,
512.Xr exec 2 ,
513.Xr fork 2 ,
514.Xr getdefaultproj 3PROJECT ,
515.Xr exec_attr 5 ,
516.Xr project 5 ,
517.Xr service_bundle 5 ,
518.Xr attributes 7 ,
519.Xr privileges 7 ,
520.Xr rbac 7 ,
521.Xr security-flags 7 ,
522.Xr smf 7 ,
523.Xr smf_bootstrap 7 ,
524.Xr zones 7 ,
525.Xr coreadm 8 ,
526.Xr inetd 8 ,
527.Xr svc.startd 8 ,
528.Xr svcadm 8 ,
529.Xr svccfg 8
530.Sh NOTES
531The present version of
532.Xr smf 7
533does not support multiple repositories.
534.Pp
535When a service is configured to be started as root but with privileges
536different from
537.Sy limit_privileges ,
538the resulting process is privilege aware.
539This can be surprising to developers who expect
540.No seteuid Ns Pq <non-zero UID>
541to reduce privileges to basic or less.
542