10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*471Shg115875 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * lsvcrun - run an rc?.d script, modifying appropriate data in the 310Sstevel@tonic-gate * repository to reflect legacy behavior. 320Sstevel@tonic-gate * 330Sstevel@tonic-gate * We try to keep track of what we can for the legacy scripts via 340Sstevel@tonic-gate * property groups under the smf/legacy_run service. Each property 350Sstevel@tonic-gate * group identifies a service, named in the form 'rc2_d_S10foo'. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * Each group has the following properties: name, the script name 380Sstevel@tonic-gate * displayed by svcs(1m); state_timestamp; contract, contract ID; 390Sstevel@tonic-gate * inode, the inode of the script; and suffix, the suffix of the 400Sstevel@tonic-gate * script name, e.g. 'foo'. 410Sstevel@tonic-gate * 420Sstevel@tonic-gate * When we run a K script, we try to identify and remove the 430Sstevel@tonic-gate * property group by means of examining the inode and script 440Sstevel@tonic-gate * suffix. The inode check means more than one script with the 450Sstevel@tonic-gate * same suffix will still work as intended in the common case. 460Sstevel@tonic-gate * 470Sstevel@tonic-gate * If we cannot find a property group, or one already exists 480Sstevel@tonic-gate * when we try to add one, then we print a suitable warning. These 490Sstevel@tonic-gate * are warnings because there was no strict requirement that K 500Sstevel@tonic-gate * and S scripts be matched up. 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * In the face of these assumptions being proved wrong, we always 530Sstevel@tonic-gate * make sure to execute the script anyway in an attempt to keep 540Sstevel@tonic-gate * things working as they used to. If we can't execute the script, 550Sstevel@tonic-gate * we try to leave the repository in the state it was before. 560Sstevel@tonic-gate */ 570Sstevel@tonic-gate 580Sstevel@tonic-gate #include <sys/ctfs.h> 590Sstevel@tonic-gate #include <sys/types.h> 600Sstevel@tonic-gate #include <sys/wait.h> 610Sstevel@tonic-gate #include <sys/stat.h> 620Sstevel@tonic-gate #include <assert.h> 630Sstevel@tonic-gate #include <ctype.h> 640Sstevel@tonic-gate #include <errno.h> 650Sstevel@tonic-gate #include <fcntl.h> 660Sstevel@tonic-gate #include <fnmatch.h> 670Sstevel@tonic-gate #include <libcontract.h> 680Sstevel@tonic-gate #include <libcontract_priv.h> 690Sstevel@tonic-gate #include <libintl.h> 700Sstevel@tonic-gate #include <libscf.h> 710Sstevel@tonic-gate #include <libscf_priv.h> 720Sstevel@tonic-gate #include <libuutil.h> 730Sstevel@tonic-gate #include <signal.h> 740Sstevel@tonic-gate #include <stdio.h> 750Sstevel@tonic-gate #include <stdlib.h> 760Sstevel@tonic-gate #include <string.h> 770Sstevel@tonic-gate #include <strings.h> 780Sstevel@tonic-gate #include <time.h> 790Sstevel@tonic-gate #include <unistd.h> 800Sstevel@tonic-gate #include <limits.h> 810Sstevel@tonic-gate 820Sstevel@tonic-gate 830Sstevel@tonic-gate /* Environment variables to pass on. See clean_environment(). */ 840Sstevel@tonic-gate static char *evars_to_pass[] = { "LANG", "LC_ALL", "LC_COLLATE", "LC_CTYPE", 850Sstevel@tonic-gate "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "PATH", "TZ" 860Sstevel@tonic-gate }; 870Sstevel@tonic-gate 880Sstevel@tonic-gate #define EVARS_TO_PASS_NUM \ 890Sstevel@tonic-gate (sizeof (evars_to_pass) / sizeof (*evars_to_pass)) 900Sstevel@tonic-gate 910Sstevel@tonic-gate 920Sstevel@tonic-gate static void 930Sstevel@tonic-gate usage() 940Sstevel@tonic-gate { 950Sstevel@tonic-gate (void) fprintf(stderr, 960Sstevel@tonic-gate gettext("Usage: %s [-s] script {start | stop}\n"), uu_getpname()); 970Sstevel@tonic-gate exit(UU_EXIT_USAGE); 980Sstevel@tonic-gate } 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* 1010Sstevel@tonic-gate * Pick out the script name and convert it for use as an SMF property 1020Sstevel@tonic-gate * group name. 1030Sstevel@tonic-gate */ 1040Sstevel@tonic-gate static char * 1050Sstevel@tonic-gate start_pg_name(const char *path) 1060Sstevel@tonic-gate { 1070Sstevel@tonic-gate char *out, *cp; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate if (fnmatch("/etc/rc[0-6S].d/S*", path, FNM_PATHNAME) != 0) { 1100Sstevel@tonic-gate uu_warn(gettext("couldn't parse name %s.\n"), path); 1110Sstevel@tonic-gate return (NULL); 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate out = strdup(path + sizeof ("/etc/") - 1); 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate if (out == NULL) { 1170Sstevel@tonic-gate uu_warn(gettext("strdup() failed (%s).\n"), strerror(errno)); 1180Sstevel@tonic-gate return (NULL); 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* Convert illegal characters to _. */ 1220Sstevel@tonic-gate for (cp = out; *cp != '\0'; ++cp) { 1230Sstevel@tonic-gate /* locale problem? */ 1240Sstevel@tonic-gate if (!isalnum(*cp) && *cp != '-') 1250Sstevel@tonic-gate *cp = '_'; 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate return (out); 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate static char * 1320Sstevel@tonic-gate script_suffix(const char *path) 1330Sstevel@tonic-gate { 1340Sstevel@tonic-gate const char *cp; 1350Sstevel@tonic-gate char *out; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate if (fnmatch("/etc/rc[0-6S].d/[SK]*", path, FNM_PATHNAME) != 0) { 1380Sstevel@tonic-gate uu_warn(gettext("couldn't parse name %s.\n"), path); 1390Sstevel@tonic-gate return (NULL); 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate cp = path + sizeof ("/etc/rc0.d/S") - 1; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate while (isdigit(*cp)) 1450Sstevel@tonic-gate cp++; 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate if (*cp == '\0') { 1480Sstevel@tonic-gate uu_warn(gettext("couldn't parse name %s.\n"), path); 1490Sstevel@tonic-gate return (NULL); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate out = strdup(cp); 1530Sstevel@tonic-gate if (out == NULL) 1540Sstevel@tonic-gate uu_warn(gettext("strdup() failed (%s).\n"), strerror(errno)); 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate return (out); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate /* 1600Sstevel@tonic-gate * Convert a path to an acceptable SMF (service) name. 1610Sstevel@tonic-gate */ 1620Sstevel@tonic-gate static char * 1630Sstevel@tonic-gate path_to_svc_name(const char *path) 1640Sstevel@tonic-gate { 1650Sstevel@tonic-gate char *out, *cp; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate out = strdup(path); 1680Sstevel@tonic-gate if (out == NULL) { 1690Sstevel@tonic-gate uu_warn(gettext("strdup() failed (%s).\n"), strerror(errno)); 1700Sstevel@tonic-gate return (NULL); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate /* Convert illegal characters to _. */ 1740Sstevel@tonic-gate for (cp = out; *cp != '\0'; ++cp) { 1750Sstevel@tonic-gate /* locale problem? */ 1760Sstevel@tonic-gate if (!isalnum(*cp) && *cp != '-' && *cp != '/') 1770Sstevel@tonic-gate *cp = '_'; 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* If the first character is _, use a instead. */ 1810Sstevel@tonic-gate if (*out == '_') 1820Sstevel@tonic-gate *out = 'a'; 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate return (out); 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate static void 1880Sstevel@tonic-gate scferr(const char *func) 1890Sstevel@tonic-gate { 1900Sstevel@tonic-gate uu_warn(gettext("%s failed (%s). Repository will not be modified.\n"), 1910Sstevel@tonic-gate func, scf_strerror(scf_error())); 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate static scf_propertygroup_t * 1950Sstevel@tonic-gate get_start_pg(const char *script, scf_handle_t *h, scf_service_t *svc, 1960Sstevel@tonic-gate boolean_t *ok) 1970Sstevel@tonic-gate { 1980Sstevel@tonic-gate char *pg_name = NULL; 1990Sstevel@tonic-gate scf_propertygroup_t *pg = NULL; 2000Sstevel@tonic-gate scf_property_t *prop = NULL; 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate if ((pg_name = start_pg_name(script)) == NULL) 2030Sstevel@tonic-gate return (NULL); 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate if ((pg = scf_pg_create(h)) == NULL) { 2060Sstevel@tonic-gate scferr("scf_pg_create()"); 2070Sstevel@tonic-gate goto out; 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate add: 2110Sstevel@tonic-gate if (scf_service_add_pg(svc, pg_name, SCF_GROUP_FRAMEWORK, 2120Sstevel@tonic-gate SCF_PG_FLAG_NONPERSISTENT, pg) == 0) { 2130Sstevel@tonic-gate *ok = 1; 2140Sstevel@tonic-gate free(pg_name); 2150Sstevel@tonic-gate return (pg); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate switch (scf_error()) { 2190Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 2200Sstevel@tonic-gate assert(0); 2210Sstevel@tonic-gate abort(); 2220Sstevel@tonic-gate /* NOTREACHED */ 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate case SCF_ERROR_EXISTS: 2250Sstevel@tonic-gate break; 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate case SCF_ERROR_PERMISSION_DENIED: 2280Sstevel@tonic-gate uu_die(gettext( 2290Sstevel@tonic-gate "Insufficient privilege to add repository properties; " 2300Sstevel@tonic-gate "not launching \"%s\".\n"), script); 2310Sstevel@tonic-gate /* NOTREACHED */ 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate default: 2340Sstevel@tonic-gate scferr("scf_service_add_pg()"); 2350Sstevel@tonic-gate scf_pg_destroy(pg); 2360Sstevel@tonic-gate pg = NULL; 2370Sstevel@tonic-gate goto out; 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate if (scf_service_get_pg(svc, pg_name, pg) != 0) { 2410Sstevel@tonic-gate switch (scf_error()) { 2420Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 2430Sstevel@tonic-gate assert(0); 2440Sstevel@tonic-gate abort(); 2450Sstevel@tonic-gate /* NOTREACHED */ 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 2480Sstevel@tonic-gate goto add; 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate default: 2510Sstevel@tonic-gate scferr("scf_service_get_pg()"); 2520Sstevel@tonic-gate scf_pg_destroy(pg); 2530Sstevel@tonic-gate pg = NULL; 2540Sstevel@tonic-gate goto out; 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate if ((prop = scf_property_create(h)) == NULL) { 2590Sstevel@tonic-gate scferr("scf_property_create()"); 2600Sstevel@tonic-gate scf_pg_destroy(pg); 2610Sstevel@tonic-gate pg = NULL; 2620Sstevel@tonic-gate goto out; 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate /* 2660Sstevel@tonic-gate * See if the pg has the name property. If it has, that 2670Sstevel@tonic-gate * implies we successfully ran the same script before. We 2680Sstevel@tonic-gate * should re-run it anyway, but not modify the existing pg; 2690Sstevel@tonic-gate * this might lose contract-control but there's not much we 2700Sstevel@tonic-gate * can do. 2710Sstevel@tonic-gate * 2720Sstevel@tonic-gate * If there's no name property, then we probably couldn't 2730Sstevel@tonic-gate * remove the pg fully after a script failed to run. 2740Sstevel@tonic-gate */ 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate if (scf_pg_get_property(pg, SCF_LEGACY_PROPERTY_NAME, prop) == 0) { 2770Sstevel@tonic-gate uu_warn(gettext("Service matching \"%s\" " 2780Sstevel@tonic-gate "seems to be running.\n"), script); 2790Sstevel@tonic-gate scf_pg_destroy(pg); 2800Sstevel@tonic-gate pg = NULL; 2810Sstevel@tonic-gate } else if (scf_error() != SCF_ERROR_NOT_FOUND) { 2820Sstevel@tonic-gate scferr("scf_pg_get_property()"); 2830Sstevel@tonic-gate scf_pg_destroy(pg); 2840Sstevel@tonic-gate pg = NULL; 2850Sstevel@tonic-gate } else { 2860Sstevel@tonic-gate uu_warn(gettext("Service \"%s\" has an invalid property " 2870Sstevel@tonic-gate "group.\n"), script); 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate out: 2910Sstevel@tonic-gate free(pg_name); 2920Sstevel@tonic-gate scf_property_destroy(prop); 2930Sstevel@tonic-gate return (pg); 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate static scf_propertygroup_t * 2970Sstevel@tonic-gate pg_match(scf_handle_t *h, scf_service_t *svc, ino_t ino, const char *suffix) 2980Sstevel@tonic-gate { 2990Sstevel@tonic-gate char buf[PATH_MAX]; 3000Sstevel@tonic-gate scf_iter_t *iter = NULL; 3010Sstevel@tonic-gate scf_propertygroup_t *pg = NULL; 3020Sstevel@tonic-gate scf_property_t *prop = NULL; 3030Sstevel@tonic-gate scf_value_t *val = NULL; 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate if ((pg = scf_pg_create(h)) == NULL) { 3060Sstevel@tonic-gate scferr("scf_pg_create()"); 3070Sstevel@tonic-gate goto err; 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate if ((iter = scf_iter_create(h)) == NULL) { 3110Sstevel@tonic-gate scferr("scf_iter_create()"); 3120Sstevel@tonic-gate goto err; 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate if ((prop = scf_property_create(h)) == NULL) { 3160Sstevel@tonic-gate scferr("scf_property_create()"); 3170Sstevel@tonic-gate goto err; 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate if ((val = scf_value_create(h)) == NULL) { 3210Sstevel@tonic-gate scferr("scf_value_create()"); 3220Sstevel@tonic-gate goto err; 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate if (scf_iter_service_pgs_typed(iter, svc, SCF_GROUP_FRAMEWORK) != 3260Sstevel@tonic-gate 0) { 3270Sstevel@tonic-gate scferr("scf_iter_service_pgs_typed()"); 3280Sstevel@tonic-gate goto err; 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate while (scf_iter_next_pg(iter, pg) > 0) { 3320Sstevel@tonic-gate int match = 1; 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate if (suffix != NULL) { 3350Sstevel@tonic-gate ssize_t len; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate if (scf_pg_get_property(pg, SCF_LEGACY_PROPERTY_SUFFIX, 3380Sstevel@tonic-gate prop) != 0) 3390Sstevel@tonic-gate continue; 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate if (scf_property_get_value(prop, val) != 0) 3420Sstevel@tonic-gate continue; 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate len = scf_value_get_astring(val, buf, sizeof (buf)); 3450Sstevel@tonic-gate if (len < 0) { 3460Sstevel@tonic-gate scferr("scf_value_get_astring()"); 3470Sstevel@tonic-gate goto err; 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate if (len >= sizeof (buf)) 3500Sstevel@tonic-gate continue; 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate match = (strcmp(buf, suffix) == 0); 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate if (ino != 0) { 3560Sstevel@tonic-gate uint64_t pval; 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate if (scf_pg_get_property(pg, SCF_LEGACY_PROPERTY_INODE, 3590Sstevel@tonic-gate prop) != 0) 3600Sstevel@tonic-gate continue; 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate if (scf_property_get_value(prop, val) != 0) 3630Sstevel@tonic-gate continue; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate if (scf_value_get_count(val, &pval) != 0) 3660Sstevel@tonic-gate continue; 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate match = (ino == pval) && match; 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate if (match) 3720Sstevel@tonic-gate goto out; 3730Sstevel@tonic-gate } 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate err: 3760Sstevel@tonic-gate scf_pg_destroy(pg); 3770Sstevel@tonic-gate pg = NULL; 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate out: 3800Sstevel@tonic-gate scf_value_destroy(val); 3810Sstevel@tonic-gate scf_iter_destroy(iter); 3820Sstevel@tonic-gate scf_property_destroy(prop); 3830Sstevel@tonic-gate return (pg); 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate /* 3870Sstevel@tonic-gate * Try and find the property group matching the service this script 3880Sstevel@tonic-gate * stops. First we look for a matching inode plus a matching suffix. 3890Sstevel@tonic-gate * This commonly succeeds, but if not, we just search for inode. 3900Sstevel@tonic-gate * Finally, we try for just the script suffix. 3910Sstevel@tonic-gate */ 3920Sstevel@tonic-gate static scf_propertygroup_t * 3930Sstevel@tonic-gate get_stop_pg(const char *script, scf_handle_t *h, scf_service_t *svc, 3940Sstevel@tonic-gate boolean_t *ok) 3950Sstevel@tonic-gate { 3960Sstevel@tonic-gate struct stat st; 3970Sstevel@tonic-gate char *suffix; 3980Sstevel@tonic-gate scf_propertygroup_t *pg; 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate if (stat(script, &st) != 0) { 4010Sstevel@tonic-gate uu_warn(gettext("Couldn't stat %s (%s).\n"), script, 4020Sstevel@tonic-gate strerror(errno)); 4030Sstevel@tonic-gate return (NULL); 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate if ((suffix = script_suffix(script)) == NULL) { 4070Sstevel@tonic-gate pg = pg_match(h, svc, st.st_ino, NULL); 4080Sstevel@tonic-gate if (pg != NULL) 4090Sstevel@tonic-gate goto out; 4100Sstevel@tonic-gate return (NULL); 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate if ((pg = pg_match(h, svc, st.st_ino, suffix)) != NULL) 4140Sstevel@tonic-gate goto out; 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate if ((pg = pg_match(h, svc, st.st_ino, NULL)) != NULL) 4170Sstevel@tonic-gate goto out; 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate if ((pg = pg_match(h, svc, 0, suffix)) == NULL) { 4200Sstevel@tonic-gate uu_warn(gettext("Service matching \"%s\" " 4210Sstevel@tonic-gate "doesn't seem to be running.\n"), script); 4220Sstevel@tonic-gate free(suffix); 4230Sstevel@tonic-gate return (NULL); 4240Sstevel@tonic-gate } 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate out: 4270Sstevel@tonic-gate *ok = 1; 4280Sstevel@tonic-gate free(suffix); 4290Sstevel@tonic-gate return (pg); 4300Sstevel@tonic-gate } 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate static scf_propertygroup_t * 4330Sstevel@tonic-gate get_script_pg(const char *script, boolean_t start_flag, boolean_t *ok) 4340Sstevel@tonic-gate { 4350Sstevel@tonic-gate scf_handle_t *h = NULL; 4360Sstevel@tonic-gate scf_scope_t *scope = NULL; 4370Sstevel@tonic-gate scf_service_t *svc = NULL; 4380Sstevel@tonic-gate scf_propertygroup_t *pg = NULL; 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate *ok = 0; 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate h = scf_handle_create(SCF_VERSION); 4430Sstevel@tonic-gate if (h == NULL) { 4440Sstevel@tonic-gate scferr("scf_handle_create()"); 4450Sstevel@tonic-gate goto out; 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate if (scf_handle_bind(h) != 0) { 4490Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NO_SERVER) { 4500Sstevel@tonic-gate scferr("scf_handle_bind()"); 4510Sstevel@tonic-gate } else { 4520Sstevel@tonic-gate uu_warn(gettext( 4530Sstevel@tonic-gate "Could not connect to svc.configd.\n")); 4540Sstevel@tonic-gate } 4550Sstevel@tonic-gate goto out; 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate if ((scope = scf_scope_create(h)) == NULL) { 4590Sstevel@tonic-gate scferr("scf_scope_create()"); 4600Sstevel@tonic-gate goto out; 4610Sstevel@tonic-gate } 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate if ((svc = scf_service_create(h)) == NULL) { 4640Sstevel@tonic-gate scferr("scf_service_create()"); 4650Sstevel@tonic-gate goto out; 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, scope) != 0) { 4690Sstevel@tonic-gate scferr("scf_handle_get_local_scope()"); 4700Sstevel@tonic-gate goto out; 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate if (scf_scope_get_service(scope, SCF_LEGACY_SERVICE, svc) != 0) { 4740Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) { 4750Sstevel@tonic-gate scferr("scf_scope_get_service()"); 4760Sstevel@tonic-gate goto out; 4770Sstevel@tonic-gate } 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate if (scf_scope_add_service(scope, SCF_LEGACY_SERVICE, svc) != 4800Sstevel@tonic-gate 0) { 4810Sstevel@tonic-gate scferr("scf_scope_add_service()"); 4820Sstevel@tonic-gate goto out; 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate if (start_flag) 4870Sstevel@tonic-gate pg = get_start_pg(script, h, svc, ok); 4880Sstevel@tonic-gate else 4890Sstevel@tonic-gate pg = get_stop_pg(script, h, svc, ok); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate out: 4920Sstevel@tonic-gate scf_service_destroy(svc); 4930Sstevel@tonic-gate scf_scope_destroy(scope); 4940Sstevel@tonic-gate return (pg); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate static int 4980Sstevel@tonic-gate prepare_contract() 4990Sstevel@tonic-gate { 5000Sstevel@tonic-gate int fd; 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate do 5030Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR); 5040Sstevel@tonic-gate while (fd < 0 && errno == EINTR); 5050Sstevel@tonic-gate if (fd < 0) { 5060Sstevel@tonic-gate uu_warn(gettext("Can not create contract")); 5070Sstevel@tonic-gate return (-1); 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* Leave HWERR in fatal set. */ 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate errno = ct_tmpl_activate(fd); 5130Sstevel@tonic-gate if (errno != 0) { 5140Sstevel@tonic-gate assert(errno == EPERM); 5150Sstevel@tonic-gate uu_warn(gettext("Can not activate contract template")); 5160Sstevel@tonic-gate (void) close(fd); 5170Sstevel@tonic-gate return (-1); 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate (void) close(fd); 5210Sstevel@tonic-gate return (0); 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate static void 5250Sstevel@tonic-gate cleanup_pg(scf_propertygroup_t *pg) 5260Sstevel@tonic-gate { 5270Sstevel@tonic-gate scf_error_t err; 5280Sstevel@tonic-gate char buf[80]; 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate if (scf_pg_delete(pg) == 0) 5310Sstevel@tonic-gate return; 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate err = scf_error(); 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate if (scf_pg_to_fmri(pg, buf, sizeof (buf)) != 0) 5360Sstevel@tonic-gate (void) strcpy(buf, "?"); 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate uu_warn(gettext("Could not remove property group %s: %s.\n"), buf, 5390Sstevel@tonic-gate scf_strerror(err)); 5400Sstevel@tonic-gate } 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate /* 5430Sstevel@tonic-gate * Create a duplicate environment which only contains approved 5440Sstevel@tonic-gate * variables---those in evars_to_pass and those beginning with "_INIT_". 5450Sstevel@tonic-gate */ 5460Sstevel@tonic-gate static char ** 5470Sstevel@tonic-gate approved_env(char **env) 5480Sstevel@tonic-gate { 5490Sstevel@tonic-gate char **newenv; 5500Sstevel@tonic-gate int i, i_new, j; 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate for (i = 0; env[i] != NULL; ++i) 5530Sstevel@tonic-gate ; 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate newenv = malloc(sizeof (*newenv) * (i + 1)); 5560Sstevel@tonic-gate if (newenv == NULL) 5570Sstevel@tonic-gate return (NULL); 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate i_new = 0; 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate for (i = 0; env[i] != NULL; ++i) { 5620Sstevel@tonic-gate if (strncmp(env[i], "_INIT_", sizeof ("_INIT_") - 1) == 0) { 5630Sstevel@tonic-gate newenv[i_new++] = env[i]; 5640Sstevel@tonic-gate continue; 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate for (j = 0; j < EVARS_TO_PASS_NUM; ++j) { 5680Sstevel@tonic-gate size_t l = strlen(evars_to_pass[j]); 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate if (env[i][l] == '=' && 5710Sstevel@tonic-gate strncmp(env[i], evars_to_pass[j], l) == 0) 5720Sstevel@tonic-gate newenv[i_new++] = env[i]; 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate newenv[i_new] = NULL; 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate return (newenv); 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate /* 5820Sstevel@tonic-gate * Create a duplicate environment which does not contain any SMF_ variables. 5830Sstevel@tonic-gate */ 5840Sstevel@tonic-gate static char ** 5850Sstevel@tonic-gate env_without_smf(char **env) 5860Sstevel@tonic-gate { 5870Sstevel@tonic-gate char **newenv; 5880Sstevel@tonic-gate int i, i_new; 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate for (i = 0; env[i] != NULL; ++i) 5910Sstevel@tonic-gate ; 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate newenv = malloc(sizeof (*newenv) * (i + 1)); 5940Sstevel@tonic-gate if (newenv == NULL) 5950Sstevel@tonic-gate return (NULL); 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate i_new = 0; 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate for (i = 0; env[i] != NULL; ++i) { 6000Sstevel@tonic-gate if (strncmp(env[i], "SMF_", sizeof ("SMF_") - 1) == 0) 6010Sstevel@tonic-gate continue; 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate newenv[i_new++] = env[i]; 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate newenv[i_new] = NULL; 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate return (newenv); 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate static int 6120Sstevel@tonic-gate add_new_property(scf_handle_t *h, scf_transaction_t *tx, const char *name, 6130Sstevel@tonic-gate scf_type_t ty, const void *val) 6140Sstevel@tonic-gate { 6150Sstevel@tonic-gate scf_transaction_entry_t *e; 6160Sstevel@tonic-gate scf_value_t *v; 6170Sstevel@tonic-gate const char *func; 6180Sstevel@tonic-gate const struct timeval *t; 6190Sstevel@tonic-gate int r; 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate if ((e = scf_entry_create(h)) == NULL) { 6220Sstevel@tonic-gate func = "scf_entry_create()"; 6230Sstevel@tonic-gate goto err; 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate if ((v = scf_value_create(h)) == NULL) { 6270Sstevel@tonic-gate func = "scf_value_create()"; 6280Sstevel@tonic-gate goto err; 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate r = scf_transaction_property_new(tx, e, name, ty); 6320Sstevel@tonic-gate if (r != 0) { 6330Sstevel@tonic-gate func = "scf_transaction_property_new()"; 6340Sstevel@tonic-gate goto err; 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate switch (ty) { 6380Sstevel@tonic-gate case SCF_TYPE_COUNT: 639*471Shg115875 scf_value_set_count(v, (uint64_t)(uintptr_t)val); 6400Sstevel@tonic-gate break; 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate case SCF_TYPE_TIME: 6430Sstevel@tonic-gate t = val; 6440Sstevel@tonic-gate r = scf_value_set_time(v, t->tv_sec, 1000 * t->tv_usec); 6450Sstevel@tonic-gate assert(r == 0); 6460Sstevel@tonic-gate break; 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate case SCF_TYPE_ASTRING: 6490Sstevel@tonic-gate r = scf_value_set_astring(v, val); 6500Sstevel@tonic-gate assert(r == 0); 6510Sstevel@tonic-gate break; 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate default: 6540Sstevel@tonic-gate assert(0); 6550Sstevel@tonic-gate abort(); 6560Sstevel@tonic-gate } 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate if (scf_entry_add_value(e, v) == 0) 6590Sstevel@tonic-gate return (0); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate func = "scf_entry_add_value()"; 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate err: 6640Sstevel@tonic-gate uu_warn(gettext("%s failed (%s).\n"), func, scf_strerror(scf_error())); 6650Sstevel@tonic-gate return (-1); 6660Sstevel@tonic-gate } 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate static void 6690Sstevel@tonic-gate set_legacy_service(scf_propertygroup_t *pg, const char *script) 6700Sstevel@tonic-gate { 6710Sstevel@tonic-gate scf_handle_t *h; 6720Sstevel@tonic-gate const char *func; 6730Sstevel@tonic-gate char *suffix; 6740Sstevel@tonic-gate scf_transaction_t *tx; 6750Sstevel@tonic-gate struct timeval tstamp; 6760Sstevel@tonic-gate struct stat st; 6770Sstevel@tonic-gate ctid_t ctid; 6780Sstevel@tonic-gate char *svc_name = NULL; 6790Sstevel@tonic-gate int ret; 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate h = scf_pg_handle(pg); 6820Sstevel@tonic-gate if (h == NULL) { 6830Sstevel@tonic-gate func = "scf_pg_handle()"; 6840Sstevel@tonic-gate goto scferr; 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate ret = gettimeofday(&tstamp, NULL); 6880Sstevel@tonic-gate assert(ret == 0); 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate if (stat(script, &st) != 0) { 6910Sstevel@tonic-gate uu_warn(gettext("Couldn't stat %s (%s).\n"), script, 6920Sstevel@tonic-gate strerror(errno)); 6930Sstevel@tonic-gate goto err; 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate if (errno = contract_latest(&ctid)) { 6970Sstevel@tonic-gate uu_warn(gettext("Could not get contract")); 6980Sstevel@tonic-gate goto err; 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate tx = scf_transaction_create(h); 7020Sstevel@tonic-gate if (tx == NULL) { 7030Sstevel@tonic-gate func = "scf_transaction_create()"; 7040Sstevel@tonic-gate goto scferr; 7050Sstevel@tonic-gate } 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate if (scf_transaction_start(tx, pg) != 0) { 7080Sstevel@tonic-gate func = "scf_transaction_start()"; 7090Sstevel@tonic-gate goto scferr; 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate 7120Sstevel@tonic-gate /* 7130Sstevel@tonic-gate * We'd like to use the prettier svc_name, but if path_to_svc_name() 7140Sstevel@tonic-gate * fails, we can use the script name anyway. 7150Sstevel@tonic-gate */ 7160Sstevel@tonic-gate svc_name = path_to_svc_name(script); 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate if (add_new_property(h, tx, SCF_LEGACY_PROPERTY_NAME, SCF_TYPE_ASTRING, 7190Sstevel@tonic-gate (void *)(svc_name ? svc_name : script)) != 0) 7200Sstevel@tonic-gate goto err; 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate if (add_new_property(h, tx, SCF_PROPERTY_STATE_TIMESTAMP, 7230Sstevel@tonic-gate SCF_TYPE_TIME, &tstamp) != 0) 7240Sstevel@tonic-gate goto err; 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate if (add_new_property(h, tx, SCF_LEGACY_PROPERTY_INODE, 7270Sstevel@tonic-gate SCF_TYPE_COUNT, (void *)st.st_ino) != 0) 7280Sstevel@tonic-gate goto err; 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate if ((suffix = script_suffix(script)) != NULL) { 7310Sstevel@tonic-gate if (add_new_property(h, tx, SCF_LEGACY_PROPERTY_SUFFIX, 7320Sstevel@tonic-gate SCF_TYPE_ASTRING, (void *)suffix) != 0) 7330Sstevel@tonic-gate goto err; 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate free(suffix); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate if (add_new_property(h, tx, SCF_PROPERTY_CONTRACT, SCF_TYPE_COUNT, 7390Sstevel@tonic-gate (void *)ctid) != 0) 7400Sstevel@tonic-gate goto err; 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate for (;;) { 7430Sstevel@tonic-gate switch (scf_transaction_commit(tx)) { 7440Sstevel@tonic-gate case 1: 7450Sstevel@tonic-gate free(svc_name); 7460Sstevel@tonic-gate return; 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate case 0: 7490Sstevel@tonic-gate if (scf_pg_update(pg) == -1) { 7500Sstevel@tonic-gate func = "scf_pg_update()"; 7510Sstevel@tonic-gate goto scferr; 7520Sstevel@tonic-gate } 7530Sstevel@tonic-gate continue; 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate case -1: 7560Sstevel@tonic-gate func = "scf_transaction_commit()"; 7570Sstevel@tonic-gate goto scferr; 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate default: 7600Sstevel@tonic-gate assert(0); 7610Sstevel@tonic-gate abort(); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate scferr: 7660Sstevel@tonic-gate uu_warn(gettext("%s failed (%s).\n"), func, scf_strerror(scf_error())); 7670Sstevel@tonic-gate err: 7680Sstevel@tonic-gate uu_die(gettext("Could not commit property values to repository.\n")); 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate int 7720Sstevel@tonic-gate main(int argc, char *argv[], char *envp[]) 7730Sstevel@tonic-gate { 7740Sstevel@tonic-gate const char *restarter, *script, *action; 7750Sstevel@tonic-gate boolean_t source = 0; 7760Sstevel@tonic-gate int o; 7770Sstevel@tonic-gate boolean_t start_flag; 7780Sstevel@tonic-gate char **newenv; 7790Sstevel@tonic-gate pid_t pid; 7800Sstevel@tonic-gate int pipefds[2]; 7810Sstevel@tonic-gate char c; 7820Sstevel@tonic-gate int exitstatus; 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate scf_propertygroup_t *pg; 7850Sstevel@tonic-gate boolean_t pg_ok; 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate (void) uu_setpname(argv[0]); 7880Sstevel@tonic-gate uu_alt_exit(UU_PROFILE_LAUNCHER); 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate /* Make sure we were run by svc.startd. */ 7910Sstevel@tonic-gate if ((restarter = getenv("SMF_RESTARTER")) == NULL || 7920Sstevel@tonic-gate strcmp(restarter, SCF_SERVICE_STARTD) != 0) 7930Sstevel@tonic-gate uu_die(gettext("invocation outside smf(5) inappropriate\n")); 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate while ((o = getopt(argc, argv, "s")) != -1) { 7960Sstevel@tonic-gate switch (o) { 7970Sstevel@tonic-gate case 's': 7980Sstevel@tonic-gate source = 1; 7990Sstevel@tonic-gate break; 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate default: 8020Sstevel@tonic-gate usage(); 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate if (argc - optind != 2) 8070Sstevel@tonic-gate usage(); 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate script = argv[optind]; 8100Sstevel@tonic-gate action = argv[optind + 1]; 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate if (strcmp(action, "start") == 0) 8130Sstevel@tonic-gate start_flag = 1; 8140Sstevel@tonic-gate else if (strcmp(action, "stop") == 0) 8150Sstevel@tonic-gate start_flag = 0; 8160Sstevel@tonic-gate else 8170Sstevel@tonic-gate usage(); 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate /* 8200Sstevel@tonic-gate * Look for the pg & exit if appropriate. Also, if we're starting, 8210Sstevel@tonic-gate * add the pg now so we can exit before launching the script if we 8220Sstevel@tonic-gate * have insufficient repository privilege. 8230Sstevel@tonic-gate * 8240Sstevel@tonic-gate * If any other problem occurs, we carry on anyway. 8250Sstevel@tonic-gate */ 8260Sstevel@tonic-gate pg = get_script_pg(script, start_flag, &pg_ok); 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate /* Clean the environment. Now so we can fail early. */ 8290Sstevel@tonic-gate if (!source) 8300Sstevel@tonic-gate newenv = approved_env(envp); 8310Sstevel@tonic-gate else 8320Sstevel@tonic-gate newenv = env_without_smf(envp); 8330Sstevel@tonic-gate if (newenv == NULL) 8340Sstevel@tonic-gate uu_die(gettext( 8350Sstevel@tonic-gate "Could not create new environment: out of memory.\n")); 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate if (prepare_contract() == -1) { 8380Sstevel@tonic-gate if (start_flag && pg != NULL) 8390Sstevel@tonic-gate cleanup_pg(pg); 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate exit(UU_EXIT_FATAL); 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate /* pipe to communicate exec success or failure */ 8450Sstevel@tonic-gate if (pipe(pipefds) != 0) { 8460Sstevel@tonic-gate uu_warn(gettext("Could not create pipe")); 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate if (start_flag && pg != NULL) 8490Sstevel@tonic-gate cleanup_pg(pg); 8500Sstevel@tonic-gate 8510Sstevel@tonic-gate exit(UU_EXIT_FATAL); 8520Sstevel@tonic-gate } 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate if (!pg_ok) 8550Sstevel@tonic-gate (void) printf(gettext("Executing legacy init script \"%s\" " 8560Sstevel@tonic-gate "despite previous errors.\n"), script); 8570Sstevel@tonic-gate else 8580Sstevel@tonic-gate (void) printf(gettext("Executing legacy init script \"%s\".\n"), 8590Sstevel@tonic-gate script); 8600Sstevel@tonic-gate (void) fflush(stdout); 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate pid = fork(); 8630Sstevel@tonic-gate if (pid < 0) { 8640Sstevel@tonic-gate uu_warn(gettext("Could not fork")); 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate if (start_flag && pg != NULL) 8670Sstevel@tonic-gate cleanup_pg(pg); 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate exit(UU_EXIT_FATAL); 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate 8720Sstevel@tonic-gate if (pid == 0) { 8730Sstevel@tonic-gate /* child */ 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate const char *arg1, *arg2, *arg3; 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate (void) close(pipefds[0]); 8780Sstevel@tonic-gate (void) fcntl(pipefds[1], F_SETFD, FD_CLOEXEC); 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate if (!source) { 8810Sstevel@tonic-gate arg1 = "/bin/sh"; 8820Sstevel@tonic-gate arg2 = script; 8830Sstevel@tonic-gate arg3 = action; 8840Sstevel@tonic-gate } else { 8850Sstevel@tonic-gate arg1 = "/bin/sh"; 8860Sstevel@tonic-gate arg2 = "-c"; 8870Sstevel@tonic-gate arg3 = script; 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate (void) execle(arg1, arg1, arg2, arg3, NULL, newenv); 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate uu_warn(gettext("Could not exec \"%s %s %s\""), arg1, 8930Sstevel@tonic-gate arg2, arg3); 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate /* Notify parent of the failure. */ 8970Sstevel@tonic-gate while (write(pipefds[1], &c, 1) != 1) { 8980Sstevel@tonic-gate switch (errno) { 8990Sstevel@tonic-gate case EAGAIN: 9000Sstevel@tonic-gate (void) sleep(1); 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate /* FALLTHROUGH */ 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate case EINTR: 9050Sstevel@tonic-gate continue; 9060Sstevel@tonic-gate } 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate uu_warn(gettext("Could not inform parent of error")); 9090Sstevel@tonic-gate break; 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate exit(UU_EXIT_FATAL); 9130Sstevel@tonic-gate } 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate (void) close(pipefds[1]); 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate if (read(pipefds[0], &c, sizeof (c)) > 0) { 9180Sstevel@tonic-gate if (!start_flag) 9190Sstevel@tonic-gate uu_die(gettext("exec() failed; leaving properties.\n")); 9200Sstevel@tonic-gate else { 9210Sstevel@tonic-gate uu_warn(gettext("exec() failed.\n")); 9220Sstevel@tonic-gate if (pg != NULL) 9230Sstevel@tonic-gate cleanup_pg(pg); 9240Sstevel@tonic-gate exit(UU_EXIT_FATAL); 9250Sstevel@tonic-gate } 9260Sstevel@tonic-gate } 9270Sstevel@tonic-gate 9280Sstevel@tonic-gate while (waitpid(pid, &exitstatus, 0) == -1) { 9290Sstevel@tonic-gate assert(errno == EINTR); 9300Sstevel@tonic-gate } 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate if (WIFSIGNALED(exitstatus)) { 9330Sstevel@tonic-gate char buf[SIG2STR_MAX]; 9340Sstevel@tonic-gate (void) sig2str(WTERMSIG(exitstatus), buf); 9350Sstevel@tonic-gate (void) printf(gettext("Legacy init script \"%s\" failed due " 9360Sstevel@tonic-gate "to signal %s.\n"), script, buf); 9370Sstevel@tonic-gate } else { 9380Sstevel@tonic-gate (void) printf(gettext("Legacy init script \"%s\" exited with " 9390Sstevel@tonic-gate "return code %d.\n"), script, WEXITSTATUS(exitstatus)); 9400Sstevel@tonic-gate } 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate if (pg != NULL) { 9430Sstevel@tonic-gate if (start_flag) 9440Sstevel@tonic-gate set_legacy_service(pg, script); 9450Sstevel@tonic-gate else 9460Sstevel@tonic-gate cleanup_pg(pg); 9470Sstevel@tonic-gate scf_pg_destroy(pg); 9480Sstevel@tonic-gate } 9490Sstevel@tonic-gate 9500Sstevel@tonic-gate return (UU_EXIT_OK); 9510Sstevel@tonic-gate } 952