Re: gigasetm101d daemon [resent]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jan 29, 2008 at 02:58:29PM +0100, Tilman Schmidt wrote:
> More or less, yes. Only it shouldn't be limited to three hardcoded
> line disciplines. Recognizing the name of any LD known to the kernel
> would be nice, but accepting arbitrary LD numbers will do.
>
> So how do we get there? Porting the OpenBSD utility appears as more
> of a hassle to me than generifying mine. I'd just have to rename it
> from gigasetm101d to ldattach, add a <discipline> argument, and
> adapt the manpage.
>
> If I do that, will you accept it for inclusion in util-linux-ng?
> How should I go about submitting it? Which subdirectory should it
> live in?

I've hacked up a trivial and probably buggy port of the openbsd one,
which is attached below.  I have no idea whether that or adopting your
version makes more sense, but I'd like to request to keep it compatible
to the openbsd variant if we want to call it ldattach.
/*	$OpenBSD: ldattach.c,v 1.4 2008/01/05 17:33:28 mbalmer Exp $	*/

/*
 * Copyright (c) 2007, 2008 Marc Balmer <mbalmer@xxxxxxxxxxx>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * A replacement for slattach(8) and nmeaattach(8) that can be used from
 * the commandline or from init(8) (using entries in /etc/ttys).
 */

#include <sys/ioctl.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <termios.h>
#include <unistd.h>
#include <limits.h>

#include "tty.h"

void		usage(void);
void		coroner(int);

volatile sig_atomic_t dying = 0;

void
usage(void)
{
	extern char *__progname;

	fprintf(stderr, "usage: %s [-27dehmo] [-s baudrate] "
	    "[-t cond] discipline device\n", __progname);
	exit(1);
}

int
main(int argc, char *argv[])
{
	struct termios tty;
#ifndef __linux__
	struct tstamps tstamps;
#endif
	const char *errstr;
	sigset_t sigset;
	pid_t ppid;
	int ch, fd, ldisc, nodaemon = 0;
	int  bits = 0, parity = 0, stop = 0, flowcl = 0, hupcl = 1;
	speed_t speed = 0;
	char devn[32], *dev, *disc;

	if ((ppid = getppid()) == 1)
		nodaemon = 1;

	while ((ch = getopt(argc, argv, "27dehmos:")) != -1) {
		switch (ch) {
		case '2':
			stop = 2;
			break;
		case '7':
			bits = 7;
			break;
		case 'd':
			nodaemon = 1;
			break;
		case 'e':
			parity = 'e';
			break;
		case 'h':
			flowcl = 1;
			break;
		case 'm':
			hupcl = 0;
			break;
		case 'o':
			parity = 'o';
			break;
		case 's':
			/* XXX(hch): use strtoul or similar */
			speed = (speed_t)atoi(optarg);
			if (errstr) {
				if (ppid != 1)
					errx(1,  "speed is %s: %s", errstr,
					    optarg);
				else
					goto bail_out;
			}
			break;
		default:
			if (ppid != -1)
				usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (ppid != 1 && argc != 2)
		usage();

	disc = *argv++;
	dev = *argv;
	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
		(void)snprintf(devn, sizeof(devn),
		    "%s%s", _PATH_DEV, dev);
		dev = devn;
	}

	if (!strcmp(disc, "slip")) {
		bits = 8;		/* make sure we use 8 databits */
		ldisc = N_SLIP;
	} else {
		syslog(LOG_ERR, "unknown line discipline %s", disc);
		goto bail_out;
	}

	syslog(LOG_INFO, "attach %s on %s", disc, dev);
	if ((fd = open(dev, O_RDWR)) < 0)
		goto bail_out;

	/*
	 * Get the current line attributes, modify only values that are
	 * either requested on the command line or that are needed by
	 * the line discipline (e.g. nmea has a default baudrate of
	 * 4800 instead of 9600).
	 */
	if (tcgetattr(fd, &tty) < 0) {
		if (ppid != 1)
			warnx("tcgetattr");
		goto bail_out;
	}


	if (bits == 7) {
		tty.c_cflag &= ~CS8;
		tty.c_cflag |= CS7;
	} else if (bits == 8) {
		tty.c_cflag &= ~CS7;
		tty.c_cflag |= CS8;
	}

	if (parity != 0)
		tty.c_cflag |= PARENB;
	if (parity == 'o')
		tty.c_cflag |= PARODD;
	else
		tty.c_cflag &= ~PARODD;

	if (stop == 2)
		tty.c_cflag |= CSTOPB;
	else
		tty.c_cflag &= ~CSTOPB;

	if (flowcl)
		tty.c_cflag |= CRTSCTS;

	if (hupcl == 0)
		tty.c_cflag &= ~HUPCL;

	if (speed != 0)
		cfsetspeed(&tty, speed);

	/* setup common to all line disciplines */
	if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
		syslog(LOG_ERR, "can't set the %s line discipline on %s", disc,
		    dev);
		goto bail_out;
	}

	/* line discpline specific setup */
	switch (ldisc) {
	case N_SLIP:
		tty.c_iflag = 0;
		tty.c_lflag = 0;
		tty.c_oflag = 0;
		tty.c_cc[VMIN] = 1;
		tty.c_cc[VTIME] = 0;
		break;
	}

	/* finally set the line attributes */
	if (tcsetattr(fd, TCSADRAIN, &tty) < 0) {
		if (ppid != 1)
			warnx("tcsetattr");
		goto bail_out;
	}

	if (!nodaemon && daemon(0, 0))
		errx(1, "can't daemonize");

	signal(SIGHUP, coroner);
	signal(SIGTERM, coroner);

	sigemptyset(&sigset);
	while (!dying)
		sigsuspend(&sigset);

bail_out:
	if (ppid == 1)
		sleep(30);	/* delay restart when called from init */

	return 0;
}

/* ARGSUSED */
void
coroner(int useless)
{
	dying = 1;
}
/*
 * These defintions are copy & pasted from the kernel's tty.h.
 */
#define N_TTY           0
#define N_SLIP          1
#define N_MOUSE         2
#define N_PPP           3
#define N_STRIP         4
#define N_AX25          5
#define N_X25           6       /* X.25 async */
#define N_6PACK         7
#define N_MASC          8       /* Reserved for Mobitex module <kaz@xxxxxxxx> */
#define N_R3964         9       /* Reserved for Simatic R3964 module */
#define N_PROFIBUS_FDL  10      /* Reserved for Profibus <Dave@xxxxxxxx> */
#define N_IRDA          11      /* Linux IrDa - http://irda.sourceforge.net/ */
#define N_SMSBLOCK      12      /* SMS block mode - for talking to GSM data */
                                /* cards about SMS messages */
#define N_HDLC          13      /* synchronous HDLC */
#define N_SYNC_PPP      14      /* synchronous PPP */
#define N_HCI           15      /* Bluetooth HCI UART */
#define N_GIGASET_M101  16      /* Siemens Gigaset M101 serial DECT adapter */
#define N_SLCAN         17      /* Serial / USB serial CAN Adaptors */

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux