summaryrefslogtreecommitdiffstats
path: root/attachtty.c
blob: 0b2eb60d443d7e2e63b18ce023b0aafdb5684757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2005 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * attachtty.c
 *
 * Very simple program to attach to a serial port.
 */

#include <fcntl.h>
#include <sys/file.h>
#include <sysexits.h>
#include <sys/poll.h>
#include <string.h>
#include "attachtty.h"

noreturn usage(int ex)
{
  fprintf(stderr, "Usage: %s port [speed]\n", program);
  exit(ex);
}

static int
config_port(int portfd, struct termios *oldconfig, speed_t speed)
{
  struct termios tio;

  if ( tcgetattr(portfd, &tio) )
    return -1;

  if ( oldconfig )
    memcpy(oldconfig, &tio, sizeof *oldconfig);

  cfmakeraw(&tio);

  if ( speed != B0 ) {
    cfsetispeed(&tio, speed);
    cfsetospeed(&tio, speed);
  }

  if ( tcsetattr(portfd, TCSANOW, &tio) )
    return -1;

  return 0;
}

static ssize_t
xwrite(int fd, const void *buf, size_t count)
{
  const char *p = buf;
  ssize_t rv, done = 0;

  while ( count ) {
    rv = write(fd, p, count);

    if ( rv == -1 ) {
      if ( errno == EINTR || errno == EAGAIN )
	continue;
      else
	return -1;
    }

    p += rv;
    count -= rv;
    done += rv;
  }

  return done;
}

static int
talk_to_port(int portfd, int infd, int outfd)
{
  char buffer[64];
  struct pollfd pollfds[2];
  ssize_t rv;

  pollfds[0].fd = portfd;
  pollfds[0].events = POLLIN|POLLERR|POLLHUP;
  pollfds[1].fd = infd;
  pollfds[1].events = POLLIN|POLLERR|POLLHUP;

  while ( poll(pollfds, 2, -1) >= 0 ) {
    if ( pollfds[0].revents ) {
      rv = read(portfd, buffer, sizeof buffer);
      if ( rv == -1 ) {
	if ( errno != EINTR && errno != EAGAIN )
	  return -1;
      } else if ( rv > 0 ) {
	xwrite(outfd, buffer, rv);
      }
    }

    if ( pollfds[1].revents ) {
      rv = read(infd, buffer, sizeof buffer);
      if ( rv == -1 ) {
	if ( errno != EINTR && errno != EAGAIN )
	  return -1;
      } else if ( rv > 0 ) {
	xwrite(portfd, buffer, rv);
      }
    }
  }

  return -1;
}

int main(int argc, char *argv[])
{
  const char *port;
  unsigned long speed;
  speed_t tiospeed = B0;
  int portfd;
  struct termios old_serial, old_console;

  port = (argc >= 2) ? argv[1] : NULL;
  speed = (argc >= 3) ? strtoul(argv[2], NULL, 0) : 0;

  if ( !port )
    usage(EX_USAGE);

  if ( speed ) {
    tiospeed = speed_to_termios(speed);
    if ( tiospeed == B0 ) {
      fprintf(stderr, "%s: unknown speed: %lu\n", program, speed);
      exit(EX_USAGE);
    }
  }

  portfd = open(port, O_RDWR);
  if ( !portfd ) {
    perror(port);
    exit(EX_NOINPUT);
  }

  if ( flock(portfd, LOCK_EX|LOCK_NB) ) {
    fprintf(stderr, "Port %s not available\n", port);
    close(portfd);
    exit(EX_UNAVAILABLE);
  }

  config_port(portfd, &old_serial, tiospeed);
  config_port(0, &old_console, B0);

  talk_to_port(portfd, 0, 1);

  tcsetattr(portfd, TCSANOW, &old_serial);
  tcsetattr(0, TCSANOW, &old_console);
  close(portfd);

  return 0;
}