17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*48bbca81SDaniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
297c478bd9Sstevel@tonic-gate /* All Rights Reserved */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include "stdio.h"
347c478bd9Sstevel@tonic-gate #include "string.h"
357c478bd9Sstevel@tonic-gate #include "errno.h"
367c478bd9Sstevel@tonic-gate #include "limits.h"
377c478bd9Sstevel@tonic-gate #include "unistd.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include "lp.h"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate extern char **environ;
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate static void envlist(int, char **);
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate * We recognize the following key phrases in the alert prototype
477c478bd9Sstevel@tonic-gate * file, and replace them with appropriate values.
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate #define NALRT_KEYS 7
507c478bd9Sstevel@tonic-gate # define ALRT_ENV 0
517c478bd9Sstevel@tonic-gate # define ALRT_PWD 1
527c478bd9Sstevel@tonic-gate # define ALRT_ULIMIT 2
537c478bd9Sstevel@tonic-gate # define ALRT_UMASK 3
547c478bd9Sstevel@tonic-gate # define ALRT_INTERVAL 4
557c478bd9Sstevel@tonic-gate # define ALRT_CMD 5
567c478bd9Sstevel@tonic-gate # define ALRT_USER 6
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate static struct {
597c478bd9Sstevel@tonic-gate char *v;
607c478bd9Sstevel@tonic-gate short len;
617c478bd9Sstevel@tonic-gate } shell_keys[NALRT_KEYS] = {
627c478bd9Sstevel@tonic-gate #define ENTRY(X) X, sizeof(X)-1
637c478bd9Sstevel@tonic-gate ENTRY("-ENVIRONMENT-"),
647c478bd9Sstevel@tonic-gate ENTRY("-PWD-"),
657c478bd9Sstevel@tonic-gate ENTRY("-ULIMIT-"),
667c478bd9Sstevel@tonic-gate ENTRY("-UMASK-"),
677c478bd9Sstevel@tonic-gate ENTRY("-INTERVAL-"),
687c478bd9Sstevel@tonic-gate ENTRY("-CMD-"),
697c478bd9Sstevel@tonic-gate ENTRY("-USER-"),
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate * These are used to bracket the administrator's command, so that
747c478bd9Sstevel@tonic-gate * we can find it easily. We're out of luck if the administrator
75*48bbca81SDaniel Hoffman * includes an identical phrase in their command.
767c478bd9Sstevel@tonic-gate */
777c478bd9Sstevel@tonic-gate #define ALRT_CMDSTART "## YOUR COMMAND STARTS HERE -- DON'T TOUCH ABOVE!!"
787c478bd9Sstevel@tonic-gate #define ALRT_CMDEND "## YOUR COMMAND ENDS HERE -- DON'T TOUCH BELOW!!"
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate /**
817c478bd9Sstevel@tonic-gate ** putalert() - WRITE ALERT TO FILES
827c478bd9Sstevel@tonic-gate **/
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate int
putalert(char * parent,char * name,FALERT * alertp)857c478bd9Sstevel@tonic-gate putalert(char *parent, char *name, FALERT *alertp)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate char *path,
887c478bd9Sstevel@tonic-gate cur_dir[PATH_MAX + 1],
897c478bd9Sstevel@tonic-gate buf[BUFSIZ];
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate int cur_umask;
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate int fdout, fdin;
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate if (!parent || !*parent || !name || !*name) {
977c478bd9Sstevel@tonic-gate errno = EINVAL;
987c478bd9Sstevel@tonic-gate return (-1);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate if (!alertp->shcmd) {
1027c478bd9Sstevel@tonic-gate errno = EINVAL;
1037c478bd9Sstevel@tonic-gate return (-1);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate if (STREQU(alertp->shcmd, NAME_NONE))
1077c478bd9Sstevel@tonic-gate return (delalert(parent, name));
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * See if the form/printer/print-wheel exists.
1117c478bd9Sstevel@tonic-gate */
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate if (!(path = makepath(parent, name, (char *)0)))
1147c478bd9Sstevel@tonic-gate return (-1);
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate if (Access(path, F_OK) == -1) {
1177c478bd9Sstevel@tonic-gate if (errno == ENOENT)
1187c478bd9Sstevel@tonic-gate errno = ENOTDIR; /* not quite, but what else? */
1197c478bd9Sstevel@tonic-gate Free (path);
1207c478bd9Sstevel@tonic-gate return (-1);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate Free (path);
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate * First, the shell command file.
1267c478bd9Sstevel@tonic-gate */
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate if (!(path = makepath(parent, name, ALERTSHFILE, (char *)0)))
1297c478bd9Sstevel@tonic-gate return (-1);
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate if ((fdout = open_locked(path, "w", MODE_NOEXEC)) < 0) {
1327c478bd9Sstevel@tonic-gate Free (path);
1337c478bd9Sstevel@tonic-gate return (-1);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate Free (path);
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate * We use a prototype file to build the shell command,
1397c478bd9Sstevel@tonic-gate * so that the alerts are easily customized. The shell
1407c478bd9Sstevel@tonic-gate * is expected to handle repeat alerts and failed alerts,
1417c478bd9Sstevel@tonic-gate * because the Spooler doesn't. Also, the Spooler runs
1427c478bd9Sstevel@tonic-gate * each alert with the UID and GID of the administrator
1437c478bd9Sstevel@tonic-gate * who defined the alert. Otherwise, anything goes.
1447c478bd9Sstevel@tonic-gate */
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate if (!Lp_Bin) {
1477c478bd9Sstevel@tonic-gate getpaths ();
1487c478bd9Sstevel@tonic-gate if (!Lp_Bin)
1497c478bd9Sstevel@tonic-gate return (-1);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate if (!(path = makepath(Lp_Bin, ALERTPROTOFILE, (char *)0)))
1527c478bd9Sstevel@tonic-gate return (-1);
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate if ((fdin = open_locked(path, "r", 0)) < 0) {
1557c478bd9Sstevel@tonic-gate Free (path);
1567c478bd9Sstevel@tonic-gate return (-1);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate Free (path);
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate errno = 0;
1617c478bd9Sstevel@tonic-gate while (fdgets(buf, BUFSIZ, fdin)) {
1627c478bd9Sstevel@tonic-gate int key;
1637c478bd9Sstevel@tonic-gate char *cp,
1647c478bd9Sstevel@tonic-gate *dash;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate cp = buf;
1677c478bd9Sstevel@tonic-gate while ((dash = strchr(cp, '-'))) {
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate *dash = 0;
1707c478bd9Sstevel@tonic-gate fdputs (cp, fdout);
1717c478bd9Sstevel@tonic-gate *(cp = dash) = '-';
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate for (key = 0; key < NALRT_KEYS; key++)
1747c478bd9Sstevel@tonic-gate if (STRNEQU(
1757c478bd9Sstevel@tonic-gate cp,
1767c478bd9Sstevel@tonic-gate shell_keys[key].v,
1777c478bd9Sstevel@tonic-gate shell_keys[key].len
1787c478bd9Sstevel@tonic-gate )) {
1797c478bd9Sstevel@tonic-gate register char *newline =
1807c478bd9Sstevel@tonic-gate (cp != buf)? "\n" : "";
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate cp += shell_keys[key].len;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate switch (key) {
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate case ALRT_ENV:
1877c478bd9Sstevel@tonic-gate fdprintf(fdout, newline);
1887c478bd9Sstevel@tonic-gate envlist(fdout, environ);
1897c478bd9Sstevel@tonic-gate break;
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate case ALRT_PWD:
1927c478bd9Sstevel@tonic-gate getcwd (cur_dir, PATH_MAX);
1937c478bd9Sstevel@tonic-gate fdprintf (fdout, "%s", cur_dir);
1947c478bd9Sstevel@tonic-gate break;
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate case ALRT_ULIMIT:
1977c478bd9Sstevel@tonic-gate fdprintf (fdout, "%ld", ulimit(1, (long)0));
1987c478bd9Sstevel@tonic-gate break;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate case ALRT_UMASK:
2017c478bd9Sstevel@tonic-gate umask (cur_umask = umask(0));
2027c478bd9Sstevel@tonic-gate fdprintf (fdout, "%03o", cur_umask);
2037c478bd9Sstevel@tonic-gate break;
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate case ALRT_INTERVAL:
2067c478bd9Sstevel@tonic-gate fdprintf(fdout, "%ld", (long)alertp->W);
2077c478bd9Sstevel@tonic-gate break;
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate case ALRT_CMD:
2107c478bd9Sstevel@tonic-gate fdprintf(fdout, newline);
2117c478bd9Sstevel@tonic-gate fdprintf(fdout, "%s\n", ALRT_CMDSTART);
2127c478bd9Sstevel@tonic-gate fdprintf(fdout, "%s\n", alertp->shcmd);
2137c478bd9Sstevel@tonic-gate fdprintf(fdout, "%s\n", ALRT_CMDEND);
2147c478bd9Sstevel@tonic-gate break;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate case ALRT_USER:
2177c478bd9Sstevel@tonic-gate fdprintf(fdout, "%s", getname());
2187c478bd9Sstevel@tonic-gate break;
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate break;
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate if (key >= NALRT_KEYS)
2257c478bd9Sstevel@tonic-gate fdputc(*cp++, fdout);
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate fdputs(cp, fdout);
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate if (errno != 0) {
2327c478bd9Sstevel@tonic-gate int save_errno = errno;
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate close(fdin);
2357c478bd9Sstevel@tonic-gate close(fdout);
2367c478bd9Sstevel@tonic-gate errno = save_errno;
2377c478bd9Sstevel@tonic-gate return (-1);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate close(fdin);
2407c478bd9Sstevel@tonic-gate close(fdout);
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate * Next, the variables file.
2447c478bd9Sstevel@tonic-gate */
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate if (!(path = makepath(parent, name, ALERTVARSFILE, (char *)0)))
2477c478bd9Sstevel@tonic-gate return (-1);
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate if ((fdout = open_locked(path, "w", MODE_NOREAD)) < 0) {
2507c478bd9Sstevel@tonic-gate Free (path);
2517c478bd9Sstevel@tonic-gate return (-1);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate Free (path);
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate fdprintf(fdout, "%d\n", alertp->Q > 0? alertp->Q : 1);
2567c478bd9Sstevel@tonic-gate fdprintf(fdout, "%d\n", alertp->W >= 0? alertp->W : 0);
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate close(fdout);
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate return (0);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate /**
2647c478bd9Sstevel@tonic-gate ** getalert() - EXTRACT ALERT FROM FILES
2657c478bd9Sstevel@tonic-gate **/
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate FALERT *
getalert(char * parent,char * name)2687c478bd9Sstevel@tonic-gate getalert(char *parent, char *name)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate int fd;
2717c478bd9Sstevel@tonic-gate char *tmp;
2727c478bd9Sstevel@tonic-gate static FALERT alert;
2737c478bd9Sstevel@tonic-gate register char *path;
2747c478bd9Sstevel@tonic-gate char buf[BUFSIZ];
2757c478bd9Sstevel@tonic-gate int len;
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate if (!parent || !*parent || !name || !*name) {
2787c478bd9Sstevel@tonic-gate errno = EINVAL;
2797c478bd9Sstevel@tonic-gate return (0);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate * See if the form/printer/print-wheel exists.
2847c478bd9Sstevel@tonic-gate */
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate if (!(path = makepath(parent, name, (char *)0)))
2877c478bd9Sstevel@tonic-gate return (0);
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate if (Access(path, F_OK) == -1) {
2907c478bd9Sstevel@tonic-gate if (errno == ENOENT)
2917c478bd9Sstevel@tonic-gate errno = ENOTDIR; /* not quite, but what else? */
2927c478bd9Sstevel@tonic-gate Free (path);
2937c478bd9Sstevel@tonic-gate return (0);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate Free (path);
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate * First, the shell command file.
2997c478bd9Sstevel@tonic-gate */
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate if (!(path = makepath(parent, name, ALERTSHFILE, (char *)0)))
3027c478bd9Sstevel@tonic-gate return (0);
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate if ((fd = open_locked(path, "r", 0)) < 0) {
3057c478bd9Sstevel@tonic-gate Free (path);
3067c478bd9Sstevel@tonic-gate return (0);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate Free (path);
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate * Skip over environment setting stuff, while loop, etc.,
3127c478bd9Sstevel@tonic-gate * to find the beginning of the command.
3137c478bd9Sstevel@tonic-gate */
3147c478bd9Sstevel@tonic-gate errno = 0;
3157c478bd9Sstevel@tonic-gate while ((tmp = fdgets(buf, BUFSIZ, fd)) &&
3167c478bd9Sstevel@tonic-gate !STRNEQU(buf, ALRT_CMDSTART, sizeof(ALRT_CMDSTART)-1))
3177c478bd9Sstevel@tonic-gate ;
3187c478bd9Sstevel@tonic-gate if ((tmp == NULL) || (errno != 0)) {
3197c478bd9Sstevel@tonic-gate int save_errno = errno;
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate close(fd);
3227c478bd9Sstevel@tonic-gate errno = save_errno;
3237c478bd9Sstevel@tonic-gate return (0);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate alert.shcmd = sop_up_rest(fd, ALRT_CMDEND);
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate close(fd);
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate if (!alert.shcmd)
3317c478bd9Sstevel@tonic-gate return (0);
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate * Drop terminating newline.
3357c478bd9Sstevel@tonic-gate */
3367c478bd9Sstevel@tonic-gate if (alert.shcmd[(len = strlen(alert.shcmd)) - 1] == '\n')
3377c478bd9Sstevel@tonic-gate alert.shcmd[len - 1] = 0;
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate * Next, the variables file.
3427c478bd9Sstevel@tonic-gate */
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate if (!(path = makepath(parent, name, ALERTVARSFILE, (char *)0)))
3457c478bd9Sstevel@tonic-gate return (0);
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate if ((fd = open_locked(path, "r", 0)) < 0) {
3487c478bd9Sstevel@tonic-gate Free (path);
3497c478bd9Sstevel@tonic-gate return (0);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate Free (path);
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate errno = 0;
3547c478bd9Sstevel@tonic-gate (void)fdgets (buf, BUFSIZ, fd);
3557c478bd9Sstevel@tonic-gate if (errno != 0) {
3567c478bd9Sstevel@tonic-gate int save_errno = errno;
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate close(fd);
3597c478bd9Sstevel@tonic-gate errno = save_errno;
3607c478bd9Sstevel@tonic-gate return (0);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate alert.Q = atoi(buf);
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate (void)fdgets (buf, BUFSIZ, fd);
3657c478bd9Sstevel@tonic-gate if (errno != 0) {
3667c478bd9Sstevel@tonic-gate int save_errno = errno;
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate close(fd);
3697c478bd9Sstevel@tonic-gate errno = save_errno;
3707c478bd9Sstevel@tonic-gate return (0);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate alert.W = atoi(buf);
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate close(fd);
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate return (&alert);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate /**
3807c478bd9Sstevel@tonic-gate ** delalert() - DELETE ALERT FILES
3817c478bd9Sstevel@tonic-gate **/
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate int
delalert(char * parent,char * name)3847c478bd9Sstevel@tonic-gate delalert(char *parent, char *name)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate char *path;
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate if (!parent || !*parent || !name || !*name) {
3907c478bd9Sstevel@tonic-gate errno = EINVAL;
3917c478bd9Sstevel@tonic-gate return (-1);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate /*
3957c478bd9Sstevel@tonic-gate * See if the form/printer/print-wheel exists.
3967c478bd9Sstevel@tonic-gate */
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate if (!(path = makepath(parent, name, (char *)0)))
3997c478bd9Sstevel@tonic-gate return (-1);
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gate if (Access(path, F_OK) == -1) {
4027c478bd9Sstevel@tonic-gate if (errno == ENOENT)
4037c478bd9Sstevel@tonic-gate errno = ENOTDIR; /* not quite, but what else? */
4047c478bd9Sstevel@tonic-gate Free (path);
4057c478bd9Sstevel@tonic-gate return (-1);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate Free (path);
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate * Remove the two files.
4117c478bd9Sstevel@tonic-gate */
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate if (!(path = makepath(parent, name, ALERTSHFILE, (char *)0)))
4147c478bd9Sstevel@tonic-gate return (-1);
4157c478bd9Sstevel@tonic-gate if (rmfile(path) == -1) {
4167c478bd9Sstevel@tonic-gate Free (path);
4177c478bd9Sstevel@tonic-gate return (-1);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate Free (path);
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate if (!(path = makepath(parent, name, ALERTVARSFILE, (char *)0)))
4227c478bd9Sstevel@tonic-gate return (-1);
4237c478bd9Sstevel@tonic-gate if (rmfile(path) == -1) {
4247c478bd9Sstevel@tonic-gate Free (path);
4257c478bd9Sstevel@tonic-gate return (-1);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate Free (path);
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate return (0);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate /**
4337c478bd9Sstevel@tonic-gate ** envlist() - PRINT OUT ENVIRONMENT LIST SAFELY
4347c478bd9Sstevel@tonic-gate **/
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate static void
envlist(int fd,char ** list)4377c478bd9Sstevel@tonic-gate envlist(int fd, char **list)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate register char *env,
4407c478bd9Sstevel@tonic-gate *value;
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate if (!list || !*list)
4437c478bd9Sstevel@tonic-gate return;
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate while ((env = *list++)) {
4467c478bd9Sstevel@tonic-gate if (!(value = strchr(env, '=')))
4477c478bd9Sstevel@tonic-gate continue;
4487c478bd9Sstevel@tonic-gate *value++ = 0;
4497c478bd9Sstevel@tonic-gate if (!strchr(value, '\''))
4507c478bd9Sstevel@tonic-gate fdprintf(fd, (char *)gettext("export %s; %s='%s'\n"),
4517c478bd9Sstevel@tonic-gate env, env, value);
4527c478bd9Sstevel@tonic-gate *--value = '=';
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate * printalert() - PRINT ALERT DESCRIPTION
4587c478bd9Sstevel@tonic-gate *
4597c478bd9Sstevel@tonic-gate * This is not used in the scheduler, so we don't need to switch to using
4607c478bd9Sstevel@tonic-gate * file descriptors for scalability.
4617c478bd9Sstevel@tonic-gate */
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate void
printalert(FILE * fp,FALERT * alertp,int isfault)4647c478bd9Sstevel@tonic-gate printalert(FILE *fp, FALERT *alertp, int isfault)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate if (!alertp->shcmd) {
4677c478bd9Sstevel@tonic-gate if (isfault)
4687c478bd9Sstevel@tonic-gate (void)fprintf (fp, (char *)gettext("On fault: no alert\n"));
4697c478bd9Sstevel@tonic-gate else
4707c478bd9Sstevel@tonic-gate (void)fprintf (fp, (char *)gettext("No alert\n"));
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate } else {
4737c478bd9Sstevel@tonic-gate register char *copy = Strdup(alertp->shcmd),
4747c478bd9Sstevel@tonic-gate *cp;
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate if (isfault)
4777c478bd9Sstevel@tonic-gate (void)fprintf (fp, (char *)gettext("On fault: "));
4787c478bd9Sstevel@tonic-gate else
4797c478bd9Sstevel@tonic-gate if (alertp->Q > 1)
4807c478bd9Sstevel@tonic-gate (void)fprintf (
4817c478bd9Sstevel@tonic-gate fp,
4827c478bd9Sstevel@tonic-gate (char *)gettext("When %d are queued: "),
4837c478bd9Sstevel@tonic-gate alertp->Q
4847c478bd9Sstevel@tonic-gate );
4857c478bd9Sstevel@tonic-gate else
4867c478bd9Sstevel@tonic-gate (void)fprintf (fp, (char *)gettext("Upon any being queued: "));
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate if (copy && (cp = strchr(copy, ' ')))
4897c478bd9Sstevel@tonic-gate while (*cp == ' ')
4907c478bd9Sstevel@tonic-gate *cp++ = 0;
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate if (
4937c478bd9Sstevel@tonic-gate copy
4947c478bd9Sstevel@tonic-gate && syn_name(cp)
4957c478bd9Sstevel@tonic-gate && (
4967c478bd9Sstevel@tonic-gate STREQU(copy, NAME_WRITE)
4977c478bd9Sstevel@tonic-gate || STREQU(copy, NAME_MAIL)
4987c478bd9Sstevel@tonic-gate )
4997c478bd9Sstevel@tonic-gate )
5007c478bd9Sstevel@tonic-gate (void)fprintf (fp, "%s to %s ", copy, cp);
5017c478bd9Sstevel@tonic-gate else
5027c478bd9Sstevel@tonic-gate (void)fprintf (fp, (char *)gettext("alert with \"%s\" "), alertp->shcmd);
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate if (alertp->W > 0)
5057c478bd9Sstevel@tonic-gate (void)fprintf (fp, (char *)gettext("every %d minutes\n"), alertp->W);
5067c478bd9Sstevel@tonic-gate else
5077c478bd9Sstevel@tonic-gate (void)fprintf (fp, (char *)gettext("once\n"));
5087c478bd9Sstevel@tonic-gate
5097c478bd9Sstevel@tonic-gate Free (copy);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate return;
5127c478bd9Sstevel@tonic-gate }
513