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*762Sdhain * 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 * efdaemon - Emebbed Fcode Interpreter daemon.
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * Opens /dev/fcode, detaches from tty and reads a request. Upon successful
330Sstevel@tonic-gate * return, invokes the Fcode interpreter via the shell script:
340Sstevel@tonic-gate * /usr/lib/efcode/efcode.sh Waits for completion of the interpreter.
350Sstevel@tonic-gate */
360Sstevel@tonic-gate
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <fcntl.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <strings.h>
420Sstevel@tonic-gate #include <syslog.h>
430Sstevel@tonic-gate #include <errno.h>
440Sstevel@tonic-gate #include <sys/wait.h>
45*762Sdhain #include <sys/fcode.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate char efcode_sh_file[] = "/usr/lib/efcode/efcode.sh";
480Sstevel@tonic-gate char dev_fcode_file[] = "/dev/fcode";
490Sstevel@tonic-gate
500Sstevel@tonic-gate int debug = 0;
510Sstevel@tonic-gate
520Sstevel@tonic-gate int
main(int argc,char ** argv)530Sstevel@tonic-gate main(int argc, char **argv)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate extern char *optarg;
560Sstevel@tonic-gate extern int optind, opterr, optopt;
570Sstevel@tonic-gate int c, fd, nbytes, status;
580Sstevel@tonic-gate char tc;
590Sstevel@tonic-gate pid_t pid, tpid;
600Sstevel@tonic-gate long nerr = 0;
61*762Sdhain int error;
620Sstevel@tonic-gate
630Sstevel@tonic-gate openlog("efdaemon", LOG_PID|LOG_CONS, LOG_DAEMON);
640Sstevel@tonic-gate
650Sstevel@tonic-gate while ((c = getopt(argc, argv, "d")) != EOF) {
660Sstevel@tonic-gate switch (c) {
670Sstevel@tonic-gate
680Sstevel@tonic-gate case 'd':
690Sstevel@tonic-gate debug++;
700Sstevel@tonic-gate break;
710Sstevel@tonic-gate
720Sstevel@tonic-gate case '?':
730Sstevel@tonic-gate syslog(LOG_ERR, "Usage: efdaemon [ -d ]\n");
740Sstevel@tonic-gate exit(1);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate }
770Sstevel@tonic-gate
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate * Ensure we can open /dev/fcode
800Sstevel@tonic-gate */
810Sstevel@tonic-gate if ((fd = open(dev_fcode_file, O_RDONLY)) < 0) {
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate * Only output message if debug is on. On most systems,
840Sstevel@tonic-gate * /dev/fcode will not exist, so this message would pollute the
850Sstevel@tonic-gate * console.
860Sstevel@tonic-gate */
870Sstevel@tonic-gate if (debug)
880Sstevel@tonic-gate syslog(LOG_ERR, "Can't open %s: %s\n", dev_fcode_file,
890Sstevel@tonic-gate strerror(errno));
900Sstevel@tonic-gate exit(1);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate close(fd);
930Sstevel@tonic-gate
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate * Ensure that /usr/lib/efcode/efcode.sh exists and is executable.
960Sstevel@tonic-gate */
970Sstevel@tonic-gate if (access(efcode_sh_file, X_OK | R_OK)) {
980Sstevel@tonic-gate syslog(LOG_ERR, "%s: %s\n", efcode_sh_file, strerror(errno));
990Sstevel@tonic-gate exit(1);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate * Fork a child then parent exits so we're a child of initd.
1040Sstevel@tonic-gate */
1050Sstevel@tonic-gate if ((pid = fork()) < 0) {
1060Sstevel@tonic-gate syslog(LOG_ERR, "Fork failed: %s\n", strerror(errno));
1070Sstevel@tonic-gate exit(1);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate if (pid)
1100Sstevel@tonic-gate exit(0);
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate * detach from tty here.
1150Sstevel@tonic-gate */
1160Sstevel@tonic-gate setpgrp();
1170Sstevel@tonic-gate close(0);
1180Sstevel@tonic-gate close(1);
1190Sstevel@tonic-gate close(2);
1200Sstevel@tonic-gate (void) open("/dev/null", O_RDWR);
1210Sstevel@tonic-gate (void) dup(0);
1220Sstevel@tonic-gate (void) dup(0);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate for (;;) {
1250Sstevel@tonic-gate while ((fd = open(dev_fcode_file, O_RDONLY)) < 0) {
1260Sstevel@tonic-gate nerr++;
1270Sstevel@tonic-gate if (nerr == 1)
1280Sstevel@tonic-gate syslog(LOG_ERR, "Can't open %s: %s\n",
1290Sstevel@tonic-gate dev_fcode_file, strerror(errno));
1300Sstevel@tonic-gate sleep(1);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate if (nerr > 1) {
1330Sstevel@tonic-gate syslog(LOG_ERR, "Open on %s failed %d times\n",
1340Sstevel@tonic-gate dev_fcode_file, nerr);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate nerr = 0;
1370Sstevel@tonic-gate nbytes = read(fd, &tc, sizeof (tc));
1380Sstevel@tonic-gate if (nbytes < 0) {
1390Sstevel@tonic-gate syslog(LOG_ERR, "Read of %s: %s\n", dev_fcode_file,
1400Sstevel@tonic-gate strerror(errno));
1410Sstevel@tonic-gate close(fd);
1420Sstevel@tonic-gate continue;
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate if (debug)
1450Sstevel@tonic-gate syslog(LOG_DEBUG, "Got request\n");
1460Sstevel@tonic-gate while ((pid = fork()) < 0) {
1470Sstevel@tonic-gate nerr++;
1480Sstevel@tonic-gate if (nerr == 1)
1490Sstevel@tonic-gate syslog(LOG_ERR, "Fork failed: %s\n",
1500Sstevel@tonic-gate strerror(errno));
1510Sstevel@tonic-gate sleep(1);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate if ((nerr > 1) && pid) {
1540Sstevel@tonic-gate syslog(LOG_ERR, "Fork failed %d times\n", nerr);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate nerr = 0;
1570Sstevel@tonic-gate if (pid) {
1580Sstevel@tonic-gate tpid = wait(&status);
1590Sstevel@tonic-gate if (tpid < 0)
1600Sstevel@tonic-gate syslog(LOG_ERR, "Wait error: %s\n",
1610Sstevel@tonic-gate strerror(errno));
1620Sstevel@tonic-gate else if (pid != tpid)
1630Sstevel@tonic-gate syslog(LOG_ERR, "Wait error, expect pid: %d"
1640Sstevel@tonic-gate " got %d, status: %x\n", pid, tpid, status);
165*762Sdhain else if (status) {
1660Sstevel@tonic-gate syslog(LOG_ERR, "Wait pid: %d status: %x\n",
1670Sstevel@tonic-gate pid, status);
168*762Sdhain if (WIFEXITED(status) &&
169*762Sdhain (WEXITSTATUS(status) == 1)) {
170*762Sdhain error = FC_FCODE_ABORT;
171*762Sdhain } else {
172*762Sdhain error = FC_EXEC_FAILED;
173*762Sdhain }
174*762Sdhain if (ioctl(fd, FC_SET_FCODE_ERROR, &error) < 0) {
175*762Sdhain syslog(LOG_ERR,
176*762Sdhain "ioctl(FC_SET_FCODE_ERROR)"
177*762Sdhain " failed\n");
178*762Sdhain }
179*762Sdhain } else if (debug)
1800Sstevel@tonic-gate syslog(LOG_DEBUG, "Wait: pid: %d\n", pid);
1810Sstevel@tonic-gate close(fd);
1820Sstevel@tonic-gate continue;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate if (debug)
1850Sstevel@tonic-gate syslog(LOG_DEBUG, "Child: %d processing request\n",
1860Sstevel@tonic-gate getpid());
1870Sstevel@tonic-gate fcntl(fd, F_DUP2FD, 0);
1880Sstevel@tonic-gate while (execl("/bin/sh", "sh", efcode_sh_file, NULL)) {
1890Sstevel@tonic-gate nerr++;
1900Sstevel@tonic-gate if (nerr == 1)
1910Sstevel@tonic-gate syslog(LOG_ERR, "execl(/bin/sh) failed: %s\n",
1920Sstevel@tonic-gate strerror(errno));
1930Sstevel@tonic-gate sleep(1);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate return (0);
1980Sstevel@tonic-gate }
199