Move everything from /var/adm to /var/log
[unleashed/lotheac.git] / usr / src / cmd / svc / lsvcrun / lsvcrun.c
blob336869a195bb7d47ef930f30ab13a6679744d925
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
26 * lsvcrun - run an rc?.d script, modifying appropriate data in the
27 * repository to reflect legacy behavior.
29 * We try to keep track of what we can for the legacy scripts via
30 * property groups under the smf/legacy_run service. Each property
31 * group identifies a service, named in the form 'rc2_d_S10foo'.
33 * Each group has the following properties: name, the script name
34 * displayed by svcs(1m); state_timestamp; contract, contract ID;
35 * inode, the inode of the script; and suffix, the suffix of the
36 * script name, e.g. 'foo'.
38 * In order to support rc scripts which delete themselves upon invocation we
39 * collect inode before running the script.
41 * When we run a K script, we try to identify and remove the
42 * property group by means of examining the inode and script
43 * suffix. The inode check means more than one script with the
44 * same suffix will still work as intended in the common case.
46 * If we cannot find a property group, or one already exists
47 * when we try to add one, then we print a suitable warning. These
48 * are warnings because there was no strict requirement that K
49 * and S scripts be matched up.
51 * In the face of these assumptions being proved wrong, we always
52 * make sure to execute the script anyway in an attempt to keep
53 * things working as they used to. If we can't execute the script,
54 * we try to leave the repository in the state it was before.
57 #include <sys/ctfs.h>
58 #include <sys/types.h>
59 #include <sys/wait.h>
60 #include <sys/stat.h>
61 #include <assert.h>
62 #include <ctype.h>
63 #include <errno.h>
64 #include <fcntl.h>
65 #include <fnmatch.h>
66 #include <libcontract.h>
67 #include <libcontract_priv.h>
68 #include <libintl.h>
69 #include <libscf.h>
70 #include <libscf_priv.h>
71 #include <libuutil.h>
72 #include <signal.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <strings.h>
77 #include <time.h>
78 #include <unistd.h>
79 #include <limits.h>
82 /* Environment variables to pass on. See clean_environment(). */
83 static char *evars_to_pass[] = { "LANG", "LC_ALL", "LC_COLLATE", "LC_CTYPE",
84 "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "PATH", "TZ"
87 #define EVARS_TO_PASS_NUM \
88 (sizeof (evars_to_pass) / sizeof (*evars_to_pass))
91 static void
92 usage()
94 (void) fprintf(stderr,
95 gettext("Usage: %s [-s] script {start | stop}\n"), uu_getpname());
96 exit(UU_EXIT_USAGE);
100 * Pick out the script name and convert it for use as an SMF property
101 * group name.
103 static char *
104 start_pg_name(const char *path)
106 char *out, *cp;
108 if (fnmatch("/etc/rc[0-6S].d/S*", path, FNM_PATHNAME) != 0) {
109 uu_warn(gettext("couldn't parse name %s.\n"), path);
110 return (NULL);
113 out = strdup(path + sizeof ("/etc/") - 1);
115 if (out == NULL) {
116 uu_warn(gettext("strdup() failed (%s).\n"), strerror(errno));
117 return (NULL);
120 /* Convert illegal characters to _. */
121 for (cp = out; *cp != '\0'; ++cp) {
122 /* locale problem? */
123 if (!isalnum(*cp) && *cp != '-')
124 *cp = '_';
127 return (out);
130 static char *
131 script_suffix(const char *path)
133 const char *cp;
134 char *out;
136 if (fnmatch("/etc/rc[0-6S].d/[SK]*", path, FNM_PATHNAME) != 0) {
137 uu_warn(gettext("couldn't parse name %s.\n"), path);
138 return (NULL);
141 cp = path + sizeof ("/etc/rc0.d/S") - 1;
143 while (isdigit(*cp))
144 cp++;
146 if (*cp == '\0') {
147 uu_warn(gettext("couldn't parse name %s.\n"), path);
148 return (NULL);
151 out = strdup(cp);
152 if (out == NULL)
153 uu_warn(gettext("strdup() failed (%s).\n"), strerror(errno));
155 return (out);
159 * Convert a path to an acceptable SMF (service) name.
161 static char *
162 path_to_svc_name(const char *path)
164 char *out, *cp;
166 out = strdup(path);
167 if (out == NULL) {
168 uu_warn(gettext("strdup() failed (%s).\n"), strerror(errno));
169 return (NULL);
172 /* Convert illegal characters to _. */
173 for (cp = out; *cp != '\0'; ++cp) {
174 /* locale problem? */
175 if (!isalnum(*cp) && *cp != '-' && *cp != '/')
176 *cp = '_';
179 /* If the first character is _, use a instead. */
180 if (*out == '_')
181 *out = 'a';
183 return (out);
186 static void
187 scferr(const char *func)
189 uu_warn(gettext("%s failed (%s). Repository will not be modified.\n"),
190 func, scf_strerror(scf_error()));
193 static scf_propertygroup_t *
194 get_start_pg(const char *script, scf_handle_t *h, scf_service_t *svc,
195 boolean_t *ok)
197 char *pg_name = NULL;
198 scf_propertygroup_t *pg = NULL;
199 scf_property_t *prop = NULL;
201 if ((pg_name = start_pg_name(script)) == NULL)
202 return (NULL);
204 if ((pg = scf_pg_create(h)) == NULL) {
205 scferr("scf_pg_create()");
206 goto out;
209 add:
210 if (scf_service_add_pg(svc, pg_name, SCF_GROUP_FRAMEWORK,
211 SCF_PG_FLAG_NONPERSISTENT, pg) == 0) {
212 *ok = 1;
213 free(pg_name);
214 return (pg);
217 switch (scf_error()) {
218 case SCF_ERROR_INVALID_ARGUMENT:
219 assert(0);
220 abort();
221 /* NOTREACHED */
223 case SCF_ERROR_EXISTS:
224 break;
226 case SCF_ERROR_PERMISSION_DENIED:
227 uu_die(gettext(
228 "Insufficient privilege to add repository properties; "
229 "not launching \"%s\".\n"), script);
230 /* NOTREACHED */
232 default:
233 scferr("scf_service_add_pg()");
234 scf_pg_destroy(pg);
235 pg = NULL;
236 goto out;
239 if (scf_service_get_pg(svc, pg_name, pg) != 0) {
240 switch (scf_error()) {
241 case SCF_ERROR_INVALID_ARGUMENT:
242 assert(0);
243 abort();
244 /* NOTREACHED */
246 case SCF_ERROR_NOT_FOUND:
247 goto add;
249 default:
250 scferr("scf_service_get_pg()");
251 scf_pg_destroy(pg);
252 pg = NULL;
253 goto out;
257 if ((prop = scf_property_create(h)) == NULL) {
258 scferr("scf_property_create()");
259 scf_pg_destroy(pg);
260 pg = NULL;
261 goto out;
265 * See if the pg has the name property. If it has, that
266 * implies we successfully ran the same script before. We
267 * should re-run it anyway, but not modify the existing pg;
268 * this might lose contract-control but there's not much we
269 * can do.
271 * If there's no name property, then we probably couldn't
272 * remove the pg fully after a script failed to run.
275 if (scf_pg_get_property(pg, SCF_LEGACY_PROPERTY_NAME, prop) == 0) {
276 uu_warn(gettext("Service matching \"%s\" "
277 "seems to be running.\n"), script);
278 scf_pg_destroy(pg);
279 pg = NULL;
280 } else if (scf_error() != SCF_ERROR_NOT_FOUND) {
281 scferr("scf_pg_get_property()");
282 scf_pg_destroy(pg);
283 pg = NULL;
284 } else {
285 uu_warn(gettext("Service \"%s\" has an invalid property "
286 "group.\n"), script);
289 out:
290 free(pg_name);
291 scf_property_destroy(prop);
292 return (pg);
295 static scf_propertygroup_t *
296 pg_match(scf_handle_t *h, scf_service_t *svc, ino_t ino, const char *suffix)
298 char buf[PATH_MAX];
299 scf_iter_t *iter = NULL;
300 scf_propertygroup_t *pg = NULL;
301 scf_property_t *prop = NULL;
302 scf_value_t *val = NULL;
304 if ((pg = scf_pg_create(h)) == NULL) {
305 scferr("scf_pg_create()");
306 goto err;
309 if ((iter = scf_iter_create(h)) == NULL) {
310 scferr("scf_iter_create()");
311 goto err;
314 if ((prop = scf_property_create(h)) == NULL) {
315 scferr("scf_property_create()");
316 goto err;
319 if ((val = scf_value_create(h)) == NULL) {
320 scferr("scf_value_create()");
321 goto err;
324 if (scf_iter_service_pgs_typed(iter, svc, SCF_GROUP_FRAMEWORK) !=
325 0) {
326 scferr("scf_iter_service_pgs_typed()");
327 goto err;
330 while (scf_iter_next_pg(iter, pg) > 0) {
331 int match = 1;
333 if (suffix != NULL) {
334 ssize_t len;
336 if (scf_pg_get_property(pg, SCF_LEGACY_PROPERTY_SUFFIX,
337 prop) != 0)
338 continue;
340 if (scf_property_get_value(prop, val) != 0)
341 continue;
343 len = scf_value_get_astring(val, buf, sizeof (buf));
344 if (len < 0) {
345 scferr("scf_value_get_astring()");
346 goto err;
348 if (len >= sizeof (buf))
349 continue;
351 match = (strcmp(buf, suffix) == 0);
354 if (ino != 0) {
355 uint64_t pval;
357 if (scf_pg_get_property(pg, SCF_LEGACY_PROPERTY_INODE,
358 prop) != 0)
359 continue;
361 if (scf_property_get_value(prop, val) != 0)
362 continue;
364 if (scf_value_get_count(val, &pval) != 0)
365 continue;
367 match = (ino == pval) && match;
370 if (match)
371 goto out;
374 err:
375 scf_pg_destroy(pg);
376 pg = NULL;
378 out:
379 scf_value_destroy(val);
380 scf_iter_destroy(iter);
381 scf_property_destroy(prop);
382 return (pg);
386 * Try and find the property group matching the service this script
387 * stops. First we look for a matching inode plus a matching suffix.
388 * This commonly succeeds, but if not, we just search for inode.
389 * Finally, we try for just the script suffix.
391 static scf_propertygroup_t *
392 get_stop_pg(const char *script, scf_handle_t *h, scf_service_t *svc,
393 boolean_t *ok)
395 struct stat st;
396 char *suffix;
397 scf_propertygroup_t *pg;
399 if (stat(script, &st) != 0) {
400 uu_warn(gettext("Couldn't stat %s (%s).\n"), script,
401 strerror(errno));
402 return (NULL);
405 if ((suffix = script_suffix(script)) == NULL) {
406 pg = pg_match(h, svc, st.st_ino, NULL);
407 if (pg != NULL)
408 goto out;
409 return (NULL);
412 if ((pg = pg_match(h, svc, st.st_ino, suffix)) != NULL)
413 goto out;
415 if ((pg = pg_match(h, svc, st.st_ino, NULL)) != NULL)
416 goto out;
418 if ((pg = pg_match(h, svc, 0, suffix)) == NULL) {
419 uu_warn(gettext("Service matching \"%s\" "
420 "doesn't seem to be running.\n"), script);
421 free(suffix);
422 return (NULL);
425 out:
426 *ok = 1;
427 free(suffix);
428 return (pg);
431 static scf_propertygroup_t *
432 get_script_pg(const char *script, boolean_t start_flag, boolean_t *ok)
434 scf_handle_t *h = NULL;
435 scf_scope_t *scope = NULL;
436 scf_service_t *svc = NULL;
437 scf_propertygroup_t *pg = NULL;
439 *ok = 0;
441 h = scf_handle_create(SCF_VERSION);
442 if (h == NULL) {
443 scferr("scf_handle_create()");
444 goto out;
447 if (scf_handle_bind(h) != 0) {
448 if (scf_error() != SCF_ERROR_NO_SERVER) {
449 scferr("scf_handle_bind()");
450 } else {
451 uu_warn(gettext(
452 "Could not connect to svc.configd.\n"));
454 goto out;
457 if ((scope = scf_scope_create(h)) == NULL) {
458 scferr("scf_scope_create()");
459 goto out;
462 if ((svc = scf_service_create(h)) == NULL) {
463 scferr("scf_service_create()");
464 goto out;
467 if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, scope) != 0) {
468 scferr("scf_handle_get_local_scope()");
469 goto out;
472 if (scf_scope_get_service(scope, SCF_LEGACY_SERVICE, svc) != 0) {
473 if (scf_error() != SCF_ERROR_NOT_FOUND) {
474 scferr("scf_scope_get_service()");
475 goto out;
478 if (scf_scope_add_service(scope, SCF_LEGACY_SERVICE, svc) !=
479 0) {
480 scferr("scf_scope_add_service()");
481 goto out;
485 if (start_flag)
486 pg = get_start_pg(script, h, svc, ok);
487 else
488 pg = get_stop_pg(script, h, svc, ok);
490 out:
491 scf_service_destroy(svc);
492 scf_scope_destroy(scope);
493 return (pg);
496 static int
497 prepare_contract(const char *script, const char *action)
499 int fd;
500 char *svc_name;
501 char *svc_strbuf;
502 int err = 0;
504 do {
505 fd = open(CTFS_ROOT "/process/template", O_RDWR);
506 } while (fd < 0 && errno == EINTR);
507 if (fd < 0) {
508 uu_warn(gettext("Can not create contract"));
509 return (-1);
512 svc_strbuf = malloc(CT_PARAM_MAX_SIZE);
513 if (svc_strbuf == NULL) {
514 uu_warn(gettext("Can not allocate memory"));
515 err = -1;
516 goto cleanup;
519 (void) strlcpy(svc_strbuf, SCF_FMRI_LEGACY_PREFIX, CT_PARAM_MAX_SIZE);
520 svc_name = path_to_svc_name(script);
521 (void) strlcat(svc_strbuf, svc_name ? svc_name : script,
522 CT_PARAM_MAX_SIZE);
523 if (svc_name != NULL) {
524 free(svc_name);
527 if ((errno = ct_pr_tmpl_set_svc_fmri(fd, svc_strbuf)) != 0) {
528 uu_warn(gettext("Can not set svc_fmri"));
529 err = -1;
530 goto cleanup;
533 (void) strlcpy(svc_strbuf, action, CT_PARAM_MAX_SIZE);
534 if ((errno = ct_pr_tmpl_set_svc_aux(fd, svc_strbuf)) != 0) {
535 uu_warn(gettext("Can not set svc_aux"));
536 err = -1;
537 goto cleanup;
540 /* Leave HWERR in fatal set. */
542 errno = ct_tmpl_activate(fd);
543 if (errno != 0) {
544 assert(errno == EPERM);
545 uu_warn(gettext("Can not activate contract template"));
546 err = -1;
547 goto cleanup;
550 cleanup:
551 free(svc_strbuf);
552 (void) close(fd);
554 return (err);
557 static void
558 cleanup_pg(scf_propertygroup_t *pg)
560 scf_error_t err;
561 char buf[80];
563 if (scf_pg_delete(pg) == 0)
564 return;
566 err = scf_error();
568 if (scf_pg_to_fmri(pg, buf, sizeof (buf)) != 0)
569 (void) strcpy(buf, "?");
571 uu_warn(gettext("Could not remove property group %s: %s.\n"), buf,
572 scf_strerror(err));
576 * Create a duplicate environment which only contains approved
577 * variables---those in evars_to_pass and those beginning with "_INIT_".
579 static char **
580 approved_env(char **env)
582 char **newenv;
583 int i, i_new, j;
585 for (i = 0; env[i] != NULL; ++i)
588 newenv = malloc(sizeof (*newenv) * (i + 1));
589 if (newenv == NULL)
590 return (NULL);
592 i_new = 0;
594 for (i = 0; env[i] != NULL; ++i) {
595 if (strncmp(env[i], "_INIT_", sizeof ("_INIT_") - 1) == 0) {
596 newenv[i_new++] = env[i];
597 continue;
600 for (j = 0; j < EVARS_TO_PASS_NUM; ++j) {
601 size_t l = strlen(evars_to_pass[j]);
603 if (env[i][l] == '=' &&
604 strncmp(env[i], evars_to_pass[j], l) == 0)
605 newenv[i_new++] = env[i];
609 newenv[i_new] = NULL;
611 return (newenv);
615 * Create a duplicate environment which does not contain any SMF_ variables.
617 static char **
618 env_without_smf(char **env)
620 char **newenv;
621 int i, i_new;
623 for (i = 0; env[i] != NULL; ++i)
626 newenv = malloc(sizeof (*newenv) * (i + 1));
627 if (newenv == NULL)
628 return (NULL);
630 i_new = 0;
632 for (i = 0; env[i] != NULL; ++i) {
633 if (strncmp(env[i], "SMF_", sizeof ("SMF_") - 1) == 0)
634 continue;
636 newenv[i_new++] = env[i];
639 newenv[i_new] = NULL;
641 return (newenv);
644 static int
645 add_new_property(scf_handle_t *h, scf_transaction_t *tx, const char *name,
646 scf_type_t ty, const void *val)
648 scf_transaction_entry_t *e;
649 scf_value_t *v;
650 const char *func;
651 const struct timeval *t;
652 int r;
654 if ((e = scf_entry_create(h)) == NULL) {
655 func = "scf_entry_create()";
656 goto err;
659 if ((v = scf_value_create(h)) == NULL) {
660 func = "scf_value_create()";
661 goto err;
664 r = scf_transaction_property_new(tx, e, name, ty);
665 if (r != 0) {
666 func = "scf_transaction_property_new()";
667 goto err;
670 switch (ty) {
671 case SCF_TYPE_COUNT:
672 scf_value_set_count(v, *(uint64_t *)val);
673 break;
675 case SCF_TYPE_TIME:
676 t = val;
677 r = scf_value_set_time(v, t->tv_sec, 1000 * t->tv_usec);
678 assert(r == 0);
679 break;
681 case SCF_TYPE_ASTRING:
682 r = scf_value_set_astring(v, val);
683 assert(r == 0);
684 break;
686 default:
687 assert(0);
688 abort();
691 if (scf_entry_add_value(e, v) == 0)
692 return (0);
694 func = "scf_entry_add_value()";
696 err:
697 uu_warn(gettext("%s failed (%s).\n"), func, scf_strerror(scf_error()));
698 return (-1);
701 static void
702 set_legacy_service(scf_propertygroup_t *pg, const char *script, ino_t inode)
704 scf_handle_t *h;
705 const char *func;
706 char *suffix;
707 scf_transaction_t *tx;
708 struct timeval tstamp;
709 ctid_t ctid;
710 char *svc_name = NULL;
711 int ret;
712 uint64_t count;
714 h = scf_pg_handle(pg);
715 if (h == NULL) {
716 func = "scf_pg_handle()";
717 goto scferr;
720 ret = gettimeofday(&tstamp, NULL);
721 assert(ret == 0);
723 if (errno = contract_latest(&ctid)) {
724 uu_warn(gettext("Could not get contract"));
725 goto err;
728 tx = scf_transaction_create(h);
729 if (tx == NULL) {
730 func = "scf_transaction_create()";
731 goto scferr;
734 if (scf_transaction_start(tx, pg) != 0) {
735 func = "scf_transaction_start()";
736 goto scferr;
740 * We'd like to use the prettier svc_name, but if path_to_svc_name()
741 * fails, we can use the script name anyway.
743 svc_name = path_to_svc_name(script);
745 if (add_new_property(h, tx, SCF_LEGACY_PROPERTY_NAME, SCF_TYPE_ASTRING,
746 (void *)(svc_name ? svc_name : script)) != 0)
747 goto err;
749 if (add_new_property(h, tx, SCF_PROPERTY_STATE_TIMESTAMP,
750 SCF_TYPE_TIME, &tstamp) != 0)
751 goto err;
753 count = inode;
754 if (add_new_property(h, tx, SCF_LEGACY_PROPERTY_INODE,
755 SCF_TYPE_COUNT, &count) != 0)
756 goto err;
758 if ((suffix = script_suffix(script)) != NULL) {
759 if (add_new_property(h, tx, SCF_LEGACY_PROPERTY_SUFFIX,
760 SCF_TYPE_ASTRING, (void *)suffix) != 0)
761 goto err;
763 free(suffix);
766 count = ctid;
767 if (add_new_property(h, tx, SCF_PROPERTY_CONTRACT, SCF_TYPE_COUNT,
768 &count) != 0)
769 goto err;
771 for (;;) {
772 switch (scf_transaction_commit(tx)) {
773 case 1:
774 free(svc_name);
775 return;
777 case 0:
778 if (scf_pg_update(pg) == -1) {
779 func = "scf_pg_update()";
780 goto scferr;
782 continue;
784 case -1:
785 func = "scf_transaction_commit()";
786 goto scferr;
788 default:
789 assert(0);
790 abort();
794 scferr:
795 uu_warn(gettext("%s failed (%s).\n"), func, scf_strerror(scf_error()));
796 err:
797 uu_die(gettext("Could not commit property values to repository.\n"));
801 main(int argc, char *argv[], char *envp[])
803 const char *restarter, *script, *action;
804 boolean_t source = 0;
805 int o;
806 boolean_t start_flag;
807 char **newenv;
808 pid_t pid;
809 int pipefds[2];
810 char c;
811 int exitstatus;
812 struct stat st;
814 scf_propertygroup_t *pg;
815 boolean_t pg_ok;
817 (void) uu_setpname(argv[0]);
818 uu_alt_exit(UU_PROFILE_LAUNCHER);
820 /* Make sure we were run by svc.startd. */
821 if ((restarter = getenv("SMF_RESTARTER")) == NULL ||
822 strcmp(restarter, SCF_SERVICE_STARTD) != 0)
823 uu_die(gettext("invocation outside smf(5) inappropriate\n"));
825 while ((o = getopt(argc, argv, "s")) != -1) {
826 switch (o) {
827 case 's':
828 source = 1;
829 break;
831 default:
832 usage();
836 if (argc - optind != 2)
837 usage();
839 script = argv[optind];
840 action = argv[optind + 1];
842 if (strcmp(action, "start") == 0)
843 start_flag = 1;
844 else if (strcmp(action, "stop") == 0)
845 start_flag = 0;
846 else
847 usage();
850 * Look for the pg & exit if appropriate. Also, if we're starting,
851 * add the pg now so we can exit before launching the script if we
852 * have insufficient repository privilege.
854 * If any other problem occurs, we carry on anyway.
856 pg = get_script_pg(script, start_flag, &pg_ok);
858 /* Clean the environment. Now so we can fail early. */
859 if (!source)
860 newenv = approved_env(envp);
861 else
862 newenv = env_without_smf(envp);
863 if (newenv == NULL)
864 uu_die(gettext(
865 "Could not create new environment: out of memory.\n"));
867 if (prepare_contract(script, action) == -1) {
868 if (start_flag && pg != NULL)
869 cleanup_pg(pg);
871 exit(UU_EXIT_FATAL);
874 /* pipe to communicate exec success or failure */
875 if (pipe(pipefds) != 0) {
876 uu_warn(gettext("Could not create pipe"));
878 if (start_flag && pg != NULL)
879 cleanup_pg(pg);
881 exit(UU_EXIT_FATAL);
884 if (!pg_ok)
885 (void) printf(gettext("Executing legacy init script \"%s\" "
886 "despite previous errors.\n"), script);
887 else
888 (void) printf(gettext("Executing legacy init script \"%s\".\n"),
889 script);
890 (void) fflush(stdout);
892 if (stat(script, &st) != 0) {
893 uu_warn(gettext("Couldn't stat %s (%s).\n"), script,
894 strerror(errno));
895 st.st_ino = (ino_t)0;
898 pid = fork();
899 if (pid < 0) {
900 uu_warn(gettext("Could not fork"));
902 if (start_flag && pg != NULL)
903 cleanup_pg(pg);
905 exit(UU_EXIT_FATAL);
908 if (pid == 0) {
909 /* child */
911 const char *arg1, *arg2, *arg3;
913 (void) close(pipefds[0]);
914 (void) fcntl(pipefds[1], F_SETFD, FD_CLOEXEC);
916 if (!source) {
917 arg1 = "/bin/sh";
918 arg2 = script;
919 arg3 = action;
920 } else {
921 arg1 = "/bin/sh";
922 arg2 = "-c";
923 arg3 = script;
926 (void) execle(arg1, arg1, arg2, arg3, NULL, newenv);
928 uu_warn(gettext("Could not exec \"%s %s %s\""), arg1,
929 arg2, arg3);
932 /* Notify parent of the failure. */
933 while (write(pipefds[1], &c, 1) != 1) {
934 switch (errno) {
935 case EAGAIN:
936 (void) sleep(1);
938 /* FALLTHROUGH */
940 case EINTR:
941 continue;
944 uu_warn(gettext("Could not inform parent of error"));
945 break;
948 exit(UU_EXIT_FATAL);
951 (void) close(pipefds[1]);
953 if (read(pipefds[0], &c, sizeof (c)) > 0) {
954 if (!start_flag)
955 uu_die(gettext("exec() failed; leaving properties.\n"));
956 else {
957 uu_warn(gettext("exec() failed.\n"));
958 if (pg != NULL)
959 cleanup_pg(pg);
960 exit(UU_EXIT_FATAL);
964 while (waitpid(pid, &exitstatus, 0) == -1) {
965 assert(errno == EINTR);
968 if (WIFSIGNALED(exitstatus)) {
969 char buf[SIG2STR_MAX];
970 (void) sig2str(WTERMSIG(exitstatus), buf);
971 (void) printf(gettext("Legacy init script \"%s\" failed due "
972 "to signal %s.\n"), script, buf);
973 } else {
974 (void) printf(gettext("Legacy init script \"%s\" exited with "
975 "return code %d.\n"), script, WEXITSTATUS(exitstatus));
978 if (pg != NULL) {
979 if (start_flag)
980 set_legacy_service(pg, script, st.st_ino);
981 else
982 cleanup_pg(pg);
983 scf_pg_destroy(pg);
986 return (UU_EXIT_OK);