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 /*
236d22b733Sdhain  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * efdaemon - Emebbed Fcode Interpreter daemon.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * Opens /dev/fcode, detaches from tty and reads a request.  Upon successful
317c478bd9Sstevel@tonic-gate  * return, invokes the Fcode interpreter via the shell script:
327c478bd9Sstevel@tonic-gate  * /usr/lib/efcode/efcode.sh  Waits for completion of the interpreter.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <syslog.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/wait.h>
436d22b733Sdhain #include <sys/fcode.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate char efcode_sh_file[] = "/usr/lib/efcode/efcode.sh";
467c478bd9Sstevel@tonic-gate char dev_fcode_file[] = "/dev/fcode";
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int debug = 0;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)517c478bd9Sstevel@tonic-gate main(int argc, char **argv)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	extern char *optarg;
547c478bd9Sstevel@tonic-gate 	extern int optind, opterr, optopt;
557c478bd9Sstevel@tonic-gate 	int c, fd, nbytes, status;
567c478bd9Sstevel@tonic-gate 	char tc;
577c478bd9Sstevel@tonic-gate 	pid_t pid, tpid;
587c478bd9Sstevel@tonic-gate 	long nerr = 0;
596d22b733Sdhain 	int error;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	openlog("efdaemon", LOG_PID|LOG_CONS, LOG_DAEMON);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d")) != EOF) {
647c478bd9Sstevel@tonic-gate 		switch (c) {
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 		case 'd':
677c478bd9Sstevel@tonic-gate 			debug++;
687c478bd9Sstevel@tonic-gate 			break;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 		case '?':
717c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Usage: efdaemon [ -d ]\n");
727c478bd9Sstevel@tonic-gate 			exit(1);
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	 * Ensure we can open /dev/fcode
787c478bd9Sstevel@tonic-gate 	 */
797c478bd9Sstevel@tonic-gate 	if ((fd = open(dev_fcode_file, O_RDONLY)) < 0) {
807c478bd9Sstevel@tonic-gate 		/*
817c478bd9Sstevel@tonic-gate 		 * Only output message if debug is on.  On most systems,
827c478bd9Sstevel@tonic-gate 		 * /dev/fcode will not exist, so this message would pollute the
837c478bd9Sstevel@tonic-gate 		 * console.
847c478bd9Sstevel@tonic-gate 		 */
857c478bd9Sstevel@tonic-gate 		if (debug)
867c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Can't open %s: %s\n", dev_fcode_file,
877c478bd9Sstevel@tonic-gate 			    strerror(errno));
887c478bd9Sstevel@tonic-gate 		exit(1);
897c478bd9Sstevel@tonic-gate 	}
90*09e6639bSToomas Soome 	(void) close(fd);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * Ensure that /usr/lib/efcode/efcode.sh exists and is executable.
947c478bd9Sstevel@tonic-gate 	 */
957c478bd9Sstevel@tonic-gate 	if (access(efcode_sh_file, X_OK | R_OK)) {
967c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: %s\n", efcode_sh_file, strerror(errno));
977c478bd9Sstevel@tonic-gate 		exit(1);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * Fork a child then parent exits so we're a child of initd.
1027c478bd9Sstevel@tonic-gate 	 */
1037c478bd9Sstevel@tonic-gate 	if ((pid = fork()) < 0) {
1047c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Fork failed: %s\n", strerror(errno));
1057c478bd9Sstevel@tonic-gate 		exit(1);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	if (pid)
1087c478bd9Sstevel@tonic-gate 		exit(0);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	/*
1127c478bd9Sstevel@tonic-gate 	 * detach from tty here.
1137c478bd9Sstevel@tonic-gate 	 */
114*09e6639bSToomas Soome 	(void) setpgrp();
115*09e6639bSToomas Soome 	(void) close(0);
116*09e6639bSToomas Soome 	(void) close(1);
117*09e6639bSToomas Soome 	(void) close(2);
1187c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR);
1197c478bd9Sstevel@tonic-gate 	(void) dup(0);
1207c478bd9Sstevel@tonic-gate 	(void) dup(0);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	for (;;) {
1237c478bd9Sstevel@tonic-gate 		while ((fd = open(dev_fcode_file, O_RDONLY)) < 0) {
1247c478bd9Sstevel@tonic-gate 			nerr++;
1257c478bd9Sstevel@tonic-gate 			if (nerr == 1)
1267c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Can't open %s: %s\n",
1277c478bd9Sstevel@tonic-gate 				    dev_fcode_file, strerror(errno));
128*09e6639bSToomas Soome 			(void) sleep(1);
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 		if (nerr > 1) {
1317c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Open on %s failed %d times\n",
1327c478bd9Sstevel@tonic-gate 			    dev_fcode_file, nerr);
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		nerr = 0;
1357c478bd9Sstevel@tonic-gate 		nbytes = read(fd, &tc, sizeof (tc));
1367c478bd9Sstevel@tonic-gate 		if (nbytes < 0) {
1377c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Read of %s: %s\n", dev_fcode_file,
1387c478bd9Sstevel@tonic-gate 			    strerror(errno));
139*09e6639bSToomas Soome 			(void) close(fd);
1407c478bd9Sstevel@tonic-gate 			continue;
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 		if (debug)
1437c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG, "Got request\n");
1447c478bd9Sstevel@tonic-gate 		while ((pid = fork()) < 0) {
1457c478bd9Sstevel@tonic-gate 			nerr++;
1467c478bd9Sstevel@tonic-gate 			if (nerr == 1)
1477c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Fork failed: %s\n",
1487c478bd9Sstevel@tonic-gate 				    strerror(errno));
149*09e6639bSToomas Soome 			(void) sleep(1);
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 		if ((nerr > 1) && pid) {
1527c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Fork failed %d times\n", nerr);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 		nerr = 0;
1557c478bd9Sstevel@tonic-gate 		if (pid) {
1567c478bd9Sstevel@tonic-gate 			tpid = wait(&status);
1577c478bd9Sstevel@tonic-gate 			if (tpid < 0)
1587c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait error: %s\n",
1597c478bd9Sstevel@tonic-gate 				    strerror(errno));
1607c478bd9Sstevel@tonic-gate 			else if (pid != tpid)
1617c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait error, expect pid: %d"
1627c478bd9Sstevel@tonic-gate 				    " got %d, status: %x\n", pid, tpid, status);
1636d22b733Sdhain 			else if (status) {
1647c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait pid: %d status: %x\n",
1657c478bd9Sstevel@tonic-gate 				    pid, status);
1666d22b733Sdhain 				if (WIFEXITED(status) &&
1676d22b733Sdhain 				    (WEXITSTATUS(status) == 1)) {
1686d22b733Sdhain 					error = FC_FCODE_ABORT;
1696d22b733Sdhain 				} else {
1706d22b733Sdhain 					error = FC_EXEC_FAILED;
1716d22b733Sdhain 				}
1726d22b733Sdhain 				if (ioctl(fd, FC_SET_FCODE_ERROR, &error) < 0) {
1736d22b733Sdhain 					syslog(LOG_ERR,
1746d22b733Sdhain 					    "ioctl(FC_SET_FCODE_ERROR)"
1756d22b733Sdhain 					    " failed\n");
1766d22b733Sdhain 				}
1776d22b733Sdhain 			} else if (debug)
1787c478bd9Sstevel@tonic-gate 				syslog(LOG_DEBUG, "Wait: pid: %d\n", pid);
179*09e6639bSToomas Soome 			(void) close(fd);
1807c478bd9Sstevel@tonic-gate 			continue;
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 		if (debug)
1837c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG, "Child: %d processing request\n",
1847c478bd9Sstevel@tonic-gate 			    getpid());
185*09e6639bSToomas Soome 		(void) fcntl(fd, F_DUP2FD, 0);
1867c478bd9Sstevel@tonic-gate 		while (execl("/bin/sh", "sh", efcode_sh_file, NULL)) {
1877c478bd9Sstevel@tonic-gate 			nerr++;
1887c478bd9Sstevel@tonic-gate 			if (nerr == 1)
1897c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "execl(/bin/sh) failed: %s\n",
1907c478bd9Sstevel@tonic-gate 				    strerror(errno));
191*09e6639bSToomas Soome 			(void) sleep(1);
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	return (0);
1967c478bd9Sstevel@tonic-gate }
197