1107120Sjulian/*
2107120Sjulian * hcseriald.c
3107120Sjulian *
4107120Sjulian * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5107120Sjulian * All rights reserved.
6107120Sjulian *
7107120Sjulian * Redistribution and use in source and binary forms, with or without
8107120Sjulian * modification, are permitted provided that the following conditions
9107120Sjulian * are met:
10107120Sjulian * 1. Redistributions of source code must retain the above copyright
11107120Sjulian *    notice, this list of conditions and the following disclaimer.
12107120Sjulian * 2. Redistributions in binary form must reproduce the above copyright
13107120Sjulian *    notice, this list of conditions and the following disclaimer in the
14107120Sjulian *    documentation and/or other materials provided with the distribution.
15107120Sjulian *
16107120Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17107120Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18107120Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19107120Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20107120Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21107120Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22107120Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23107120Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24107120Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25107120Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26107120Sjulian * SUCH DAMAGE.
27107120Sjulian *
28121054Semax * $Id: hcseriald.c,v 1.3 2003/05/21 22:40:32 max Exp $
29107120Sjulian * $FreeBSD$
30107120Sjulian */
31107120Sjulian
32107120Sjulian#include <sys/types.h>
33107120Sjulian#include <sys/ioctl.h>
34107120Sjulian
35107120Sjulian#include <netgraph/ng_message.h>
36107120Sjulian#include <netgraph.h>
37121054Semax#include <netgraph/bluetooth/include/ng_h4.h>
38107120Sjulian
39107120Sjulian#include <errno.h>
40107120Sjulian#include <fcntl.h>
41107120Sjulian#include <signal.h>
42107120Sjulian#include <stdarg.h>
43107120Sjulian#include <stdio.h>
44107120Sjulian#include <stdlib.h>
45107120Sjulian#include <string.h>
46107120Sjulian#include <syslog.h>
47107120Sjulian#include <termios.h>
48107120Sjulian#include <unistd.h>
49107120Sjulian
50107120Sjulian/* Prototypes */
51107120Sjulianstatic int	open_device	(char const *, speed_t, char const *);
52107120Sjulianstatic void	sighandler	(int);
53107120Sjulianstatic void	usage		();
54107120Sjulian
55107120Sjulianstatic char const * const	hcseriald = "hcseriald";
56107120Sjulianstatic int			done = 0;
57107120Sjulian
58107120Sjulianint
59107120Sjulianmain(int argc, char *argv[])
60107120Sjulian{
61107120Sjulian	char			*device = NULL, *name = NULL;
62107120Sjulian	speed_t			 speed = 115200;
63107120Sjulian	int			 n, detach = 1;
64107120Sjulian	char			 p[FILENAME_MAX];
65107120Sjulian	FILE			*f = NULL;
66107120Sjulian	struct sigaction	 sa;
67107120Sjulian
68107120Sjulian	/* Process command line arguments */
69114879Sjulian	while ((n = getopt(argc, argv, "df:n:s:h")) != -1) {
70107120Sjulian		switch (n) {
71107120Sjulian		case 'd':
72107120Sjulian			detach = 0;
73107120Sjulian			break;
74107120Sjulian
75107120Sjulian		case 'f':
76107120Sjulian			device = optarg;
77107120Sjulian			break;
78107120Sjulian
79107120Sjulian		case 'n':
80107120Sjulian			name = optarg;
81107120Sjulian			break;
82107120Sjulian
83107120Sjulian		case 's':
84107120Sjulian			speed = atoi(optarg);
85107120Sjulian			if (speed < 0)
86107120Sjulian				usage(argv[0]);
87107120Sjulian			break;
88107120Sjulian
89114879Sjulian		case 'h':
90107120Sjulian		default:
91107120Sjulian			usage(argv[0]);
92107120Sjulian			break;
93107120Sjulian		}
94107120Sjulian	}
95107120Sjulian
96107120Sjulian	if (device == NULL || name == NULL)
97107120Sjulian		usage(argv[0]);
98107120Sjulian
99107120Sjulian	openlog(hcseriald, LOG_PID | LOG_NDELAY, LOG_USER);
100107120Sjulian
101107120Sjulian	/* Open device */
102107120Sjulian	n = open_device(device, speed, name);
103107120Sjulian
104188130Semax	if (detach && daemon(0, 0) < 0) {
105188130Semax		syslog(LOG_ERR, "Could not daemon(0, 0). %s (%d)",
106188130Semax			strerror(errno), errno);
107188130Semax		exit(1);
108107120Sjulian	}
109107120Sjulian
110107120Sjulian	/* Write PID file */
111107120Sjulian	snprintf(p, sizeof(p), "/var/run/%s.%s.pid", hcseriald, name);
112107120Sjulian	f = fopen(p, "w");
113107120Sjulian	if (f == NULL) {
114107120Sjulian		syslog(LOG_ERR, "Could not fopen(%s). %s (%d)",
115107120Sjulian			p, strerror(errno), errno);
116107120Sjulian		exit(1);
117107120Sjulian	}
118107120Sjulian	fprintf(f, "%d", getpid());
119107120Sjulian	fclose(f);
120107120Sjulian
121107120Sjulian	/* Install signal handler */
122107120Sjulian	memset(&sa, 0, sizeof(sa));
123107120Sjulian	sa.sa_handler = sighandler;
124107120Sjulian
125107120Sjulian	if (sigaction(SIGTERM, &sa, NULL) < 0) {
126107120Sjulian		syslog(LOG_ERR, "Could not sigaction(SIGTERM). %s (%d)",
127107120Sjulian			strerror(errno), errno);
128107120Sjulian		exit(1);
129107120Sjulian	}
130107120Sjulian
131107120Sjulian	if (sigaction(SIGHUP, &sa, NULL) < 0) {
132107120Sjulian		syslog(LOG_ERR, "Could not sigaction(SIGHUP). %s (%d)",
133107120Sjulian			strerror(errno), errno);
134107120Sjulian		exit(1);
135107120Sjulian	}
136107120Sjulian
137107120Sjulian	if (sigaction(SIGINT, &sa, NULL) < 0) {
138107120Sjulian		syslog(LOG_ERR, "Could not sigaction(SIGINT). %s (%d)",
139107120Sjulian			strerror(errno), errno);
140107120Sjulian		exit(1);
141107120Sjulian	}
142107120Sjulian
143107120Sjulian	/* Keep running */
144107120Sjulian	while (!done)
145107120Sjulian		select(0, NULL, NULL, NULL, NULL);
146107120Sjulian
147107120Sjulian	/* Remove PID file and close device */
148107120Sjulian	unlink(p);
149107120Sjulian	close(n);
150107120Sjulian	closelog();
151107120Sjulian
152107120Sjulian	return (0);
153107120Sjulian} /* main */
154107120Sjulian
155107120Sjulian/* Open terminal, set settings, push H4 line discipline and set node name */
156107120Sjulianstatic int
157107120Sjulianopen_device(char const *device, speed_t speed, char const *name)
158107120Sjulian{
159107120Sjulian	int		fd, disc, cs, ds;
160107120Sjulian	struct termios	t;
161107120Sjulian	struct nodeinfo	ni;
162107120Sjulian	struct ngm_name	n;
163122758Sharti	char		p[NG_NODESIZ];
164107120Sjulian
165107120Sjulian	/* Open terminal device and setup H4 line discipline */
166107120Sjulian	fd = open(device, O_RDWR|O_NOCTTY);
167107120Sjulian	if (fd < 0) {
168107120Sjulian		syslog(LOG_ERR, "Could not open(%s). %s (%d)",
169107120Sjulian			device, strerror(errno), errno);
170107120Sjulian		exit(1);
171107120Sjulian	}
172107120Sjulian
173107120Sjulian	tcflush(fd, TCIOFLUSH);
174107120Sjulian
175107120Sjulian	if (tcgetattr(fd, &t) < 0) {
176107120Sjulian		syslog(LOG_ERR, "Could not tcgetattr(%s). %s (%d)",
177107120Sjulian			device, strerror(errno), errno);
178107120Sjulian		exit(1);
179107120Sjulian	}
180107120Sjulian
181107120Sjulian	cfmakeraw(&t);
182107120Sjulian
183107120Sjulian	t.c_cflag |= CLOCAL;	/* clocal */
184107120Sjulian	t.c_cflag &= ~CSIZE;	/* cs8 */
185107120Sjulian	t.c_cflag |= CS8; 	/* cs8 */
186107120Sjulian	t.c_cflag &= ~PARENB;	/* -parenb */
187107120Sjulian	t.c_cflag &= ~CSTOPB;	/* -cstopb */
188107120Sjulian	t.c_cflag |= CRTSCTS;	/* crtscts */
189107120Sjulian
190107120Sjulian	if (tcsetattr(fd, TCSANOW, &t) < 0) {
191107120Sjulian		syslog(LOG_ERR, "Could not tcsetattr(%s). %s (%d)",
192107120Sjulian			device, strerror(errno), errno);
193107120Sjulian		exit(1);
194107120Sjulian	}
195107120Sjulian
196107120Sjulian	tcflush(fd, TCIOFLUSH);
197107120Sjulian
198107120Sjulian	if (cfsetspeed(&t, speed) < 0) {
199107120Sjulian		syslog(LOG_ERR, "Could not cfsetspeed(%s). %s (%d)",
200107120Sjulian			device, strerror(errno), errno);
201107120Sjulian		exit(1);
202107120Sjulian	}
203107120Sjulian
204107120Sjulian	if (tcsetattr(fd, TCSANOW, &t) < 0) {
205107120Sjulian		syslog(LOG_ERR, "Could not tcsetattr(%s). %s (%d)",
206107120Sjulian			device, strerror(errno), errno);
207107120Sjulian		exit(1);
208107120Sjulian	}
209107120Sjulian
210107120Sjulian	disc = H4DISC;
211107120Sjulian	if (ioctl(fd, TIOCSETD, &disc) < 0) {
212107120Sjulian		syslog(LOG_ERR, "Could not ioctl(%s, TIOCSETD, %d). %s (%d)",
213107120Sjulian			device, disc, strerror(errno), errno);
214107120Sjulian		exit(1);
215107120Sjulian	}
216107120Sjulian
217107120Sjulian	/* Get default name of the Netgraph node */
218107120Sjulian	memset(&ni, 0, sizeof(ni));
219107120Sjulian	if (ioctl(fd, NGIOCGINFO, &ni) < 0) {
220107120Sjulian		syslog(LOG_ERR, "Could not ioctl(%d, NGIOGINFO). %s (%d)",
221107120Sjulian			fd, strerror(errno), errno);
222107120Sjulian		exit(1);
223107120Sjulian	}
224107120Sjulian
225107120Sjulian	/* Assign new name to the Netgraph node */
226107120Sjulian	snprintf(p, sizeof(p), "%s:", ni.name);
227107120Sjulian	snprintf(n.name, sizeof(n.name), "%s", name);
228107120Sjulian
229107120Sjulian	if (NgMkSockNode(NULL, &cs, &ds) < 0) {
230107120Sjulian		syslog(LOG_ERR, "Could not NgMkSockNode(). %s (%d)",
231107120Sjulian			strerror(errno), errno);
232107120Sjulian		exit(1);
233107120Sjulian	}
234107120Sjulian
235107120Sjulian	if (NgSendMsg(cs, p, NGM_GENERIC_COOKIE, NGM_NAME, &n, sizeof(n)) < 0) {
236107120Sjulian		syslog(LOG_ERR, "Could not NgSendMsg(%d, %s, NGM_NAME, %s). " \
237107120Sjulian			"%s (%d)", cs, p, n.name, strerror(errno), errno);
238107120Sjulian		exit(1);
239107120Sjulian	}
240107120Sjulian
241107120Sjulian	close(cs);
242107120Sjulian	close(ds);
243107120Sjulian
244107120Sjulian	return (fd);
245107120Sjulian} /* open_device */
246107120Sjulian
247107120Sjulian/* Signal handler */
248107120Sjulianstatic void
249107120Sjuliansighandler(int s)
250107120Sjulian{
251107120Sjulian	done = 1;
252107120Sjulian} /* sighandler */
253107120Sjulian
254107120Sjulian/* Usage */
255107120Sjulianstatic void
256107120Sjulianusage(void)
257107120Sjulian{
258114879Sjulian	fprintf(stderr, "Usage: %s -f device -n node_name [-s speed -d -h]\n" \
259107120Sjulian			"Where:\n" \
260244040Seadler			"\t-f device    tty device name, ex. /dev/cuau1\n" \
261107120Sjulian			"\t-n node_name set Netgraph node name to node_name\n" \
262107120Sjulian			"\t-s speed     set tty speed, ex. 115200\n" \
263114879Sjulian			"\t-d           run in foreground\n" \
264114879Sjulian			"\t-h           display this message\n",
265107120Sjulian			hcseriald);
266107120Sjulian	exit(255);
267107120Sjulian} /* usage */
268107120Sjulian
269