4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $Id: hcseriald.c,v 1.3 2003/05/21 22:40:32 max Exp $
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
35 #include <netgraph/ng_message.h>
37 #include <netgraph/bluetooth/include/ng_h4.h>
51 static int open_device (char const *, speed_t, char const *);
52 static void sighandler (int);
55 static char const * const hcseriald = "hcseriald";
59 main(int argc, char *argv[])
61 char *device = NULL, *name = NULL;
62 speed_t speed = 115200;
68 /* Process command line arguments */
69 while ((n = getopt(argc, argv, "df:n:s:h")) != -1) {
96 if (device == NULL || name == NULL)
99 openlog(hcseriald, LOG_PID | LOG_NDELAY, LOG_USER);
102 n = open_device(device, speed, name);
107 if (pid == (pid_t) -1) {
108 syslog(LOG_ERR, "Could not fork(). %s (%d)",
109 strerror(errno), errno);
116 if (daemon(0, 0) < 0) {
117 syslog(LOG_ERR, "Could not daemon(0, 0). %s (%d)",
118 strerror(errno), errno);
124 snprintf(p, sizeof(p), "/var/run/%s.%s.pid", hcseriald, name);
127 syslog(LOG_ERR, "Could not fopen(%s). %s (%d)",
128 p, strerror(errno), errno);
131 fprintf(f, "%d", getpid());
134 /* Install signal handler */
135 memset(&sa, 0, sizeof(sa));
136 sa.sa_handler = sighandler;
138 if (sigaction(SIGTERM, &sa, NULL) < 0) {
139 syslog(LOG_ERR, "Could not sigaction(SIGTERM). %s (%d)",
140 strerror(errno), errno);
144 if (sigaction(SIGHUP, &sa, NULL) < 0) {
145 syslog(LOG_ERR, "Could not sigaction(SIGHUP). %s (%d)",
146 strerror(errno), errno);
150 if (sigaction(SIGINT, &sa, NULL) < 0) {
151 syslog(LOG_ERR, "Could not sigaction(SIGINT). %s (%d)",
152 strerror(errno), errno);
158 select(0, NULL, NULL, NULL, NULL);
160 /* Remove PID file and close device */
168 /* Open terminal, set settings, push H4 line discipline and set node name */
170 open_device(char const *device, speed_t speed, char const *name)
172 int fd, disc, cs, ds;
178 /* Open terminal device and setup H4 line discipline */
179 fd = open(device, O_RDWR|O_NOCTTY);
181 syslog(LOG_ERR, "Could not open(%s). %s (%d)",
182 device, strerror(errno), errno);
186 tcflush(fd, TCIOFLUSH);
188 if (tcgetattr(fd, &t) < 0) {
189 syslog(LOG_ERR, "Could not tcgetattr(%s). %s (%d)",
190 device, strerror(errno), errno);
196 t.c_cflag |= CLOCAL; /* clocal */
197 t.c_cflag &= ~CSIZE; /* cs8 */
198 t.c_cflag |= CS8; /* cs8 */
199 t.c_cflag &= ~PARENB; /* -parenb */
200 t.c_cflag &= ~CSTOPB; /* -cstopb */
201 t.c_cflag |= CRTSCTS; /* crtscts */
203 if (tcsetattr(fd, TCSANOW, &t) < 0) {
204 syslog(LOG_ERR, "Could not tcsetattr(%s). %s (%d)",
205 device, strerror(errno), errno);
209 tcflush(fd, TCIOFLUSH);
211 if (cfsetspeed(&t, speed) < 0) {
212 syslog(LOG_ERR, "Could not cfsetspeed(%s). %s (%d)",
213 device, strerror(errno), errno);
217 if (tcsetattr(fd, TCSANOW, &t) < 0) {
218 syslog(LOG_ERR, "Could not tcsetattr(%s). %s (%d)",
219 device, strerror(errno), errno);
224 if (ioctl(fd, TIOCSETD, &disc) < 0) {
225 syslog(LOG_ERR, "Could not ioctl(%s, TIOCSETD, %d). %s (%d)",
226 device, disc, strerror(errno), errno);
230 /* Get default name of the Netgraph node */
231 memset(&ni, 0, sizeof(ni));
232 if (ioctl(fd, NGIOCGINFO, &ni) < 0) {
233 syslog(LOG_ERR, "Could not ioctl(%d, NGIOGINFO). %s (%d)",
234 fd, strerror(errno), errno);
238 /* Assign new name to the Netgraph node */
239 snprintf(p, sizeof(p), "%s:", ni.name);
240 snprintf(n.name, sizeof(n.name), "%s", name);
242 if (NgMkSockNode(NULL, &cs, &ds) < 0) {
243 syslog(LOG_ERR, "Could not NgMkSockNode(). %s (%d)",
244 strerror(errno), errno);
248 if (NgSendMsg(cs, p, NGM_GENERIC_COOKIE, NGM_NAME, &n, sizeof(n)) < 0) {
249 syslog(LOG_ERR, "Could not NgSendMsg(%d, %s, NGM_NAME, %s). " \
250 "%s (%d)", cs, p, n.name, strerror(errno), errno);
271 fprintf(stderr, "Usage: %s -f device -n node_name [-s speed -d -h]\n" \
273 "\t-f device tty device name, ex. /dev/cuad1\n" \
274 "\t-n node_name set Netgraph node name to node_name\n" \
275 "\t-s speed set tty speed, ex. 115200\n" \
276 "\t-d run in foreground\n" \
277 "\t-h display this message\n",