xref: /titanic_52/usr/src/lib/efcode/efdaemon/efdaemon.c (revision 6d22b73346a02763769401e9f28b596670cc3d16)
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 /*
23*6d22b733Sdhain  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * efdaemon - Emebbed Fcode Interpreter daemon.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * Opens /dev/fcode, detaches from tty and reads a request.  Upon successful
337c478bd9Sstevel@tonic-gate  * return, invokes the Fcode interpreter via the shell script:
347c478bd9Sstevel@tonic-gate  * /usr/lib/efcode/efcode.sh  Waits for completion of the interpreter.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <strings.h>
427c478bd9Sstevel@tonic-gate #include <syslog.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <sys/wait.h>
45*6d22b733Sdhain #include <sys/fcode.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate char efcode_sh_file[] = "/usr/lib/efcode/efcode.sh";
487c478bd9Sstevel@tonic-gate char dev_fcode_file[] = "/dev/fcode";
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate int debug = 0;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate int
537c478bd9Sstevel@tonic-gate main(int argc, char **argv)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	extern char *optarg;
567c478bd9Sstevel@tonic-gate 	extern int optind, opterr, optopt;
577c478bd9Sstevel@tonic-gate 	int c, fd, nbytes, status;
587c478bd9Sstevel@tonic-gate 	char tc;
597c478bd9Sstevel@tonic-gate 	pid_t pid, tpid;
607c478bd9Sstevel@tonic-gate 	long nerr = 0;
61*6d22b733Sdhain 	int error;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	openlog("efdaemon", LOG_PID|LOG_CONS, LOG_DAEMON);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d")) != EOF) {
667c478bd9Sstevel@tonic-gate 		switch (c) {
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 		case 'd':
697c478bd9Sstevel@tonic-gate 			debug++;
707c478bd9Sstevel@tonic-gate 			break;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 		case '?':
737c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Usage: efdaemon [ -d ]\n");
747c478bd9Sstevel@tonic-gate 			exit(1);
757c478bd9Sstevel@tonic-gate 		}
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	/*
797c478bd9Sstevel@tonic-gate 	 * Ensure we can open /dev/fcode
807c478bd9Sstevel@tonic-gate 	 */
817c478bd9Sstevel@tonic-gate 	if ((fd = open(dev_fcode_file, O_RDONLY)) < 0) {
827c478bd9Sstevel@tonic-gate 		/*
837c478bd9Sstevel@tonic-gate 		 * Only output message if debug is on.  On most systems,
847c478bd9Sstevel@tonic-gate 		 * /dev/fcode will not exist, so this message would pollute the
857c478bd9Sstevel@tonic-gate 		 * console.
867c478bd9Sstevel@tonic-gate 		 */
877c478bd9Sstevel@tonic-gate 		if (debug)
887c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Can't open %s: %s\n", dev_fcode_file,
897c478bd9Sstevel@tonic-gate 			    strerror(errno));
907c478bd9Sstevel@tonic-gate 		exit(1);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 	close(fd);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	/*
957c478bd9Sstevel@tonic-gate 	 * Ensure that /usr/lib/efcode/efcode.sh exists and is executable.
967c478bd9Sstevel@tonic-gate 	 */
977c478bd9Sstevel@tonic-gate 	if (access(efcode_sh_file, X_OK | R_OK)) {
987c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: %s\n", efcode_sh_file, strerror(errno));
997c478bd9Sstevel@tonic-gate 		exit(1);
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/*
1037c478bd9Sstevel@tonic-gate 	 * Fork a child then parent exits so we're a child of initd.
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate 	if ((pid = fork()) < 0) {
1067c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Fork failed: %s\n", strerror(errno));
1077c478bd9Sstevel@tonic-gate 		exit(1);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 	if (pid)
1107c478bd9Sstevel@tonic-gate 		exit(0);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/*
1147c478bd9Sstevel@tonic-gate 	 * detach from tty here.
1157c478bd9Sstevel@tonic-gate 	 */
1167c478bd9Sstevel@tonic-gate 	setpgrp();
1177c478bd9Sstevel@tonic-gate 	close(0);
1187c478bd9Sstevel@tonic-gate 	close(1);
1197c478bd9Sstevel@tonic-gate 	close(2);
1207c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR);
1217c478bd9Sstevel@tonic-gate 	(void) dup(0);
1227c478bd9Sstevel@tonic-gate 	(void) dup(0);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	for (;;) {
1257c478bd9Sstevel@tonic-gate 		while ((fd = open(dev_fcode_file, O_RDONLY)) < 0) {
1267c478bd9Sstevel@tonic-gate 			nerr++;
1277c478bd9Sstevel@tonic-gate 			if (nerr == 1)
1287c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Can't open %s: %s\n",
1297c478bd9Sstevel@tonic-gate 				    dev_fcode_file, strerror(errno));
1307c478bd9Sstevel@tonic-gate 			sleep(1);
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 		if (nerr > 1) {
1337c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Open on %s failed %d times\n",
1347c478bd9Sstevel@tonic-gate 			    dev_fcode_file, nerr);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		nerr = 0;
1377c478bd9Sstevel@tonic-gate 		nbytes = read(fd, &tc, sizeof (tc));
1387c478bd9Sstevel@tonic-gate 		if (nbytes < 0) {
1397c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Read of %s: %s\n", dev_fcode_file,
1407c478bd9Sstevel@tonic-gate 			    strerror(errno));
1417c478bd9Sstevel@tonic-gate 			close(fd);
1427c478bd9Sstevel@tonic-gate 			continue;
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 		if (debug)
1457c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG, "Got request\n");
1467c478bd9Sstevel@tonic-gate 		while ((pid = fork()) < 0) {
1477c478bd9Sstevel@tonic-gate 			nerr++;
1487c478bd9Sstevel@tonic-gate 			if (nerr == 1)
1497c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Fork failed: %s\n",
1507c478bd9Sstevel@tonic-gate 				    strerror(errno));
1517c478bd9Sstevel@tonic-gate 			sleep(1);
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 		if ((nerr > 1) && pid) {
1547c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Fork failed %d times\n", nerr);
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 		nerr = 0;
1577c478bd9Sstevel@tonic-gate 		if (pid) {
1587c478bd9Sstevel@tonic-gate 			tpid = wait(&status);
1597c478bd9Sstevel@tonic-gate 			if (tpid < 0)
1607c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait error: %s\n",
1617c478bd9Sstevel@tonic-gate 				    strerror(errno));
1627c478bd9Sstevel@tonic-gate 			else if (pid != tpid)
1637c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait error, expect pid: %d"
1647c478bd9Sstevel@tonic-gate 				    " got %d, status: %x\n", pid, tpid, status);
165*6d22b733Sdhain 			else if (status) {
1667c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait pid: %d status: %x\n",
1677c478bd9Sstevel@tonic-gate 				    pid, status);
168*6d22b733Sdhain 				if (WIFEXITED(status) &&
169*6d22b733Sdhain 				    (WEXITSTATUS(status) == 1)) {
170*6d22b733Sdhain 					error = FC_FCODE_ABORT;
171*6d22b733Sdhain 				} else {
172*6d22b733Sdhain 					error = FC_EXEC_FAILED;
173*6d22b733Sdhain 				}
174*6d22b733Sdhain 				if (ioctl(fd, FC_SET_FCODE_ERROR, &error) < 0) {
175*6d22b733Sdhain 					syslog(LOG_ERR,
176*6d22b733Sdhain 					    "ioctl(FC_SET_FCODE_ERROR)"
177*6d22b733Sdhain 					    " failed\n");
178*6d22b733Sdhain 				}
179*6d22b733Sdhain 			} else if (debug)
1807c478bd9Sstevel@tonic-gate 				syslog(LOG_DEBUG, "Wait: pid: %d\n", pid);
1817c478bd9Sstevel@tonic-gate 			close(fd);
1827c478bd9Sstevel@tonic-gate 			continue;
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 		if (debug)
1857c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG, "Child: %d processing request\n",
1867c478bd9Sstevel@tonic-gate 			    getpid());
1877c478bd9Sstevel@tonic-gate 		fcntl(fd, F_DUP2FD, 0);
1887c478bd9Sstevel@tonic-gate 		while (execl("/bin/sh", "sh", efcode_sh_file, NULL)) {
1897c478bd9Sstevel@tonic-gate 			nerr++;
1907c478bd9Sstevel@tonic-gate 			if (nerr == 1)
1917c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "execl(/bin/sh) failed: %s\n",
1927c478bd9Sstevel@tonic-gate 				    strerror(errno));
1937c478bd9Sstevel@tonic-gate 			sleep(1);
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	return (0);
1987c478bd9Sstevel@tonic-gate }
199