1 /*
2 *
3 * Copyright (c) 2003 The Regents of the University of California. All
4 * rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * - Neither the name of the University nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31 #include "fusdd_net_i.h"
32
33 /*
34 * FUSDd_net.c
35 *
36 * Remote device support for FUSD
37 *
38 */
39
40 /* global state variable */
41 struct fusdnet state = {};
42
43 /*
44 * Log support
45 */
46
47 void nlog(int loglevel, char *fmt, ...)
48 {
49 va_list ap;
50 char buf[4096];
51 int buflen = 0;
52 struct timeval tv;
53 char timebuf[64];
54 time_t tmp;
55
56 va_start(ap, fmt);
57
58 /* prepend the string parsed by emrun, if needed */
59 gettimeofday(&tv, NULL);
60 tmp = tv.tv_sec;
61 strcpy(timebuf, asctime(localtime(&tmp)));
62 memmove(timebuf+23, timebuf+19, 8);
63 sprintf(timebuf+19, ".%03d", (int) tv.tv_usec/1000);
64 BUFPRINTF("%s: ", timebuf);
65
66 vsnprintf(buf+buflen, sizeof(buf)-buflen-1, fmt, ap);
67 buflen = strlen(buf); // retval from vsprintf is wacky
68
69 /* take off extra CRLFs, if there (some messages have them, some don't) */
70 while (buf[buflen-1] == '\n' || buf[buflen-1] == '\r')
71 buflen--;
72
73 /* make room for trailing CR */
74 if (buflen >= (sizeof(buf)-2))
75 buflen = sizeof(buf)-2;
76
77 /* and append a final CR: */
78 buf[buflen++] = '\n';
79 buf[buflen] = 0;
80
81 /* emit the log message */
82 logring_write(state.log, buf);
83 fprintf(stderr, "%s\n", buf);
84 va_end(ap);
85 }
86
87
88 /*
89 * Status handler
90 */
91
92 static
93 int fusdnet_status_print(status_context_t *ctx, buf_t *buf)
94 {
95 fn_t *fn = sd_data(ctx);
96 //fnc_status_print(fn, buf);
97 fns_status_print(fn, buf);
98 return STATUS_MSG_COMPLETE;
99 }
100
101 void fn_status_notify(fn_t *fn)
102 {
103 g_status_dev_notify(fn->status);
104 }
105
106 /*
107 * Startup
108 */
109
110 void start_fusdnet(int verbose)
111 {
112 /* create a log device */
113 {
114 logring_opts_t log_opts = {
115 device: {
116 devname: FUSD_NET_LOG_DEVNAME,
117 device_info: &state,
118 local_only: 1
119 }
120 };
121
122 if (g_logring(&log_opts, &(state.log)) < 0) {
123 fprintf(stderr, "fusdnet: Unable to create device %s\n",
124 FUSD_NET_LOG_DEVNAME);
125 exit(1);
126 }
127 }
128
129 /* create a status dev */
130 {
131 status_dev_opts_t s_opts = {
132 device: {
133 devname: FUSD_NET_STATUS_DEVNAME,
134 device_info: &state,
135 local_only: 1
136 },
137 printable: fusdnet_status_print
138 };
139
140 if (g_status_dev(&s_opts, &state.status) < 0) {
141 fprintf(stderr, "fusdnet: Unable to create device %s\n",
142 FUSD_NET_STATUS_DEVNAME);
143 exit(1);
144 }
145 }
146
147 /* intialize the server and client sides */
148 fusdnet_server_init(&state);
149 //fusdnet_client_init(&state);
150
151 /* daemonize? */
152 if (!verbose) {
153 int status = fork();
154
155 if (status < 0) {
156 fprintf(stderr, "fusdd: Unable to fork!\n");
157 exit(1);
158 }
159
160 if (status)
161 return;
162
163 /* child: daemonize */
164 setpgrp();
165 chdir("/");
166 close(0);
167 close(1);
168 close(2);
169 }
170
171 g_main();
172 exit(1);
173 }
174
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.