1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2000-2002 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * efdaemon - Emebbed Fcode Interpreter daemon.
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * Opens /dev/fcode, detaches from tty and reads a request.  Upon successful
33*0Sstevel@tonic-gate  * return, invokes the Fcode interpreter via the shell script:
34*0Sstevel@tonic-gate  * /usr/lib/efcode/efcode.sh  Waits for completion of the interpreter.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <stdio.h>
38*0Sstevel@tonic-gate #include <fcntl.h>
39*0Sstevel@tonic-gate #include <unistd.h>
40*0Sstevel@tonic-gate #include <stdlib.h>
41*0Sstevel@tonic-gate #include <strings.h>
42*0Sstevel@tonic-gate #include <syslog.h>
43*0Sstevel@tonic-gate #include <errno.h>
44*0Sstevel@tonic-gate #include <sys/wait.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate char efcode_sh_file[] = "/usr/lib/efcode/efcode.sh";
47*0Sstevel@tonic-gate char dev_fcode_file[] = "/dev/fcode";
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate int debug = 0;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate int
52*0Sstevel@tonic-gate main(int argc, char **argv)
53*0Sstevel@tonic-gate {
54*0Sstevel@tonic-gate 	extern char *optarg;
55*0Sstevel@tonic-gate 	extern int optind, opterr, optopt;
56*0Sstevel@tonic-gate 	int c, fd, nbytes, status;
57*0Sstevel@tonic-gate 	char tc;
58*0Sstevel@tonic-gate 	pid_t pid, tpid;
59*0Sstevel@tonic-gate 	long nerr = 0;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	openlog("efdaemon", LOG_PID|LOG_CONS, LOG_DAEMON);
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d")) != EOF) {
64*0Sstevel@tonic-gate 		switch (c) {
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 		case 'd':
67*0Sstevel@tonic-gate 			debug++;
68*0Sstevel@tonic-gate 			break;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 		case '?':
71*0Sstevel@tonic-gate 			syslog(LOG_ERR, "Usage: efdaemon [ -d ]\n");
72*0Sstevel@tonic-gate 			exit(1);
73*0Sstevel@tonic-gate 		}
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	/*
77*0Sstevel@tonic-gate 	 * Ensure we can open /dev/fcode
78*0Sstevel@tonic-gate 	 */
79*0Sstevel@tonic-gate 	if ((fd = open(dev_fcode_file, O_RDONLY)) < 0) {
80*0Sstevel@tonic-gate 		/*
81*0Sstevel@tonic-gate 		 * Only output message if debug is on.  On most systems,
82*0Sstevel@tonic-gate 		 * /dev/fcode will not exist, so this message would pollute the
83*0Sstevel@tonic-gate 		 * console.
84*0Sstevel@tonic-gate 		 */
85*0Sstevel@tonic-gate 		if (debug)
86*0Sstevel@tonic-gate 			syslog(LOG_ERR, "Can't open %s: %s\n", dev_fcode_file,
87*0Sstevel@tonic-gate 			    strerror(errno));
88*0Sstevel@tonic-gate 		exit(1);
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 	close(fd);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	/*
93*0Sstevel@tonic-gate 	 * Ensure that /usr/lib/efcode/efcode.sh exists and is executable.
94*0Sstevel@tonic-gate 	 */
95*0Sstevel@tonic-gate 	if (access(efcode_sh_file, X_OK | R_OK)) {
96*0Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: %s\n", efcode_sh_file, strerror(errno));
97*0Sstevel@tonic-gate 		exit(1);
98*0Sstevel@tonic-gate 	}
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	/*
101*0Sstevel@tonic-gate 	 * Fork a child then parent exits so we're a child of initd.
102*0Sstevel@tonic-gate 	 */
103*0Sstevel@tonic-gate 	if ((pid = fork()) < 0) {
104*0Sstevel@tonic-gate 		syslog(LOG_ERR, "Fork failed: %s\n", strerror(errno));
105*0Sstevel@tonic-gate 		exit(1);
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 	if (pid)
108*0Sstevel@tonic-gate 		exit(0);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	/*
112*0Sstevel@tonic-gate 	 * detach from tty here.
113*0Sstevel@tonic-gate 	 */
114*0Sstevel@tonic-gate 	setpgrp();
115*0Sstevel@tonic-gate 	close(0);
116*0Sstevel@tonic-gate 	close(1);
117*0Sstevel@tonic-gate 	close(2);
118*0Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR);
119*0Sstevel@tonic-gate 	(void) dup(0);
120*0Sstevel@tonic-gate 	(void) dup(0);
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	for (;;) {
123*0Sstevel@tonic-gate 		while ((fd = open(dev_fcode_file, O_RDONLY)) < 0) {
124*0Sstevel@tonic-gate 			nerr++;
125*0Sstevel@tonic-gate 			if (nerr == 1)
126*0Sstevel@tonic-gate 				syslog(LOG_ERR, "Can't open %s: %s\n",
127*0Sstevel@tonic-gate 				    dev_fcode_file, strerror(errno));
128*0Sstevel@tonic-gate 			sleep(1);
129*0Sstevel@tonic-gate 		}
130*0Sstevel@tonic-gate 		if (nerr > 1) {
131*0Sstevel@tonic-gate 			syslog(LOG_ERR, "Open on %s failed %d times\n",
132*0Sstevel@tonic-gate 			    dev_fcode_file, nerr);
133*0Sstevel@tonic-gate 		}
134*0Sstevel@tonic-gate 		nerr = 0;
135*0Sstevel@tonic-gate 		nbytes = read(fd, &tc, sizeof (tc));
136*0Sstevel@tonic-gate 		if (nbytes < 0) {
137*0Sstevel@tonic-gate 			syslog(LOG_ERR, "Read of %s: %s\n", dev_fcode_file,
138*0Sstevel@tonic-gate 			    strerror(errno));
139*0Sstevel@tonic-gate 			close(fd);
140*0Sstevel@tonic-gate 			continue;
141*0Sstevel@tonic-gate 		}
142*0Sstevel@tonic-gate 		if (debug)
143*0Sstevel@tonic-gate 			syslog(LOG_DEBUG, "Got request\n");
144*0Sstevel@tonic-gate 		while ((pid = fork()) < 0) {
145*0Sstevel@tonic-gate 			nerr++;
146*0Sstevel@tonic-gate 			if (nerr == 1)
147*0Sstevel@tonic-gate 				syslog(LOG_ERR, "Fork failed: %s\n",
148*0Sstevel@tonic-gate 				    strerror(errno));
149*0Sstevel@tonic-gate 			sleep(1);
150*0Sstevel@tonic-gate 		}
151*0Sstevel@tonic-gate 		if ((nerr > 1) && pid) {
152*0Sstevel@tonic-gate 			syslog(LOG_ERR, "Fork failed %d times\n", nerr);
153*0Sstevel@tonic-gate 		}
154*0Sstevel@tonic-gate 		nerr = 0;
155*0Sstevel@tonic-gate 		if (pid) {
156*0Sstevel@tonic-gate 			tpid = wait(&status);
157*0Sstevel@tonic-gate 			if (tpid < 0)
158*0Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait error: %s\n",
159*0Sstevel@tonic-gate 				    strerror(errno));
160*0Sstevel@tonic-gate 			else if (pid != tpid)
161*0Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait error, expect pid: %d"
162*0Sstevel@tonic-gate 				    " got %d, status: %x\n", pid, tpid, status);
163*0Sstevel@tonic-gate 			else if (status)
164*0Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait pid: %d status: %x\n",
165*0Sstevel@tonic-gate 				    pid, status);
166*0Sstevel@tonic-gate 			else if (debug)
167*0Sstevel@tonic-gate 				syslog(LOG_DEBUG, "Wait: pid: %d\n", pid);
168*0Sstevel@tonic-gate 			close(fd);
169*0Sstevel@tonic-gate 			continue;
170*0Sstevel@tonic-gate 		}
171*0Sstevel@tonic-gate 		if (debug)
172*0Sstevel@tonic-gate 			syslog(LOG_DEBUG, "Child: %d processing request\n",
173*0Sstevel@tonic-gate 			    getpid());
174*0Sstevel@tonic-gate 		fcntl(fd, F_DUP2FD, 0);
175*0Sstevel@tonic-gate 		while (execl("/bin/sh", "sh", efcode_sh_file, NULL)) {
176*0Sstevel@tonic-gate 			nerr++;
177*0Sstevel@tonic-gate 			if (nerr == 1)
178*0Sstevel@tonic-gate 				syslog(LOG_ERR, "execl(/bin/sh) failed: %s\n",
179*0Sstevel@tonic-gate 				    strerror(errno));
180*0Sstevel@tonic-gate 			sleep(1);
181*0Sstevel@tonic-gate 		}
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	return (0);
185*0Sstevel@tonic-gate }
186