4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * efdaemon - Emebbed Fcode Interpreter daemon.
32 * Opens /dev/fcode, detaches from tty and reads a request. Upon successful
33 * return, invokes the Fcode interpreter via the shell script:
34 * /usr/lib/efcode/efcode.sh Waits for completion of the interpreter.
45 #include <sys/fcode.h>
47 char efcode_sh_file
[] = "/usr/lib/efcode/efcode.sh";
48 char dev_fcode_file
[] = "/dev/fcode";
53 main(int argc
, char **argv
)
56 extern int optind
, opterr
, optopt
;
57 int c
, fd
, nbytes
, status
;
63 openlog("efdaemon", LOG_PID
|LOG_CONS
, LOG_DAEMON
);
65 while ((c
= getopt(argc
, argv
, "d")) != EOF
) {
73 syslog(LOG_ERR
, "Usage: efdaemon [ -d ]\n");
79 * Ensure we can open /dev/fcode
81 if ((fd
= open(dev_fcode_file
, O_RDONLY
)) < 0) {
83 * Only output message if debug is on. On most systems,
84 * /dev/fcode will not exist, so this message would pollute the
88 syslog(LOG_ERR
, "Can't open %s: %s\n", dev_fcode_file
,
95 * Ensure that /usr/lib/efcode/efcode.sh exists and is executable.
97 if (access(efcode_sh_file
, X_OK
| R_OK
)) {
98 syslog(LOG_ERR
, "%s: %s\n", efcode_sh_file
, strerror(errno
));
103 * Fork a child then parent exits so we're a child of initd.
105 if ((pid
= fork()) < 0) {
106 syslog(LOG_ERR
, "Fork failed: %s\n", strerror(errno
));
114 * detach from tty here.
120 (void) open("/dev/null", O_RDWR
);
125 while ((fd
= open(dev_fcode_file
, O_RDONLY
)) < 0) {
128 syslog(LOG_ERR
, "Can't open %s: %s\n",
129 dev_fcode_file
, strerror(errno
));
133 syslog(LOG_ERR
, "Open on %s failed %d times\n",
134 dev_fcode_file
, nerr
);
137 nbytes
= read(fd
, &tc
, sizeof (tc
));
139 syslog(LOG_ERR
, "Read of %s: %s\n", dev_fcode_file
,
145 syslog(LOG_DEBUG
, "Got request\n");
146 while ((pid
= fork()) < 0) {
149 syslog(LOG_ERR
, "Fork failed: %s\n",
153 if ((nerr
> 1) && pid
) {
154 syslog(LOG_ERR
, "Fork failed %d times\n", nerr
);
158 tpid
= wait(&status
);
160 syslog(LOG_ERR
, "Wait error: %s\n",
162 else if (pid
!= tpid
)
163 syslog(LOG_ERR
, "Wait error, expect pid: %d"
164 " got %d, status: %x\n", pid
, tpid
, status
);
166 syslog(LOG_ERR
, "Wait pid: %d status: %x\n",
168 if (WIFEXITED(status
) &&
169 (WEXITSTATUS(status
) == 1)) {
170 error
= FC_FCODE_ABORT
;
172 error
= FC_EXEC_FAILED
;
174 if (ioctl(fd
, FC_SET_FCODE_ERROR
, &error
) < 0) {
176 "ioctl(FC_SET_FCODE_ERROR)"
180 syslog(LOG_DEBUG
, "Wait: pid: %d\n", pid
);
185 syslog(LOG_DEBUG
, "Child: %d processing request\n",
187 fcntl(fd
, F_DUP2FD
, 0);
188 while (execl("/bin/sh", "sh", efcode_sh_file
, NULL
)) {
191 syslog(LOG_ERR
, "execl(/bin/sh) failed: %s\n",