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
|
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include "config.h"
#include "ttyutils.h"
static struct speed_struct {
int user_speed;
speed_t termios_speed;
} speed_table[] = {
{300, B300},
{600, B600},
{1200, B1200},
{2400, B2400},
{4800, B4800},
{9600, B9600},
{19200, B19200},
{38400, B38400},
{-1, B0}
};
int tty_raw(int fd, int hwflag)
{
struct termios term;
if (tcgetattr(fd, &term) == -1) {
perror("tty_raw: tcgetattr");
return FALSE;
}
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
term.c_iflag = IGNBRK | IGNPAR;
term.c_oflag = 0;
term.c_lflag = 0;
#ifdef CIBAUD
term.c_cflag = (term.c_cflag & (CBAUD | CIBAUD)) | CREAD | CS8 | CLOCAL;
#else
term.c_cflag = (term.c_cflag & CBAUD) | CREAD | CS8 | CLOCAL;
#endif
if (hwflag)
term.c_cflag |= CRTSCTS;
if (tcsetattr(fd, TCSANOW, &term) == -1) {
perror("tty_raw: tcsetattr");
return FALSE;
}
return TRUE;
}
int tty_speed(int fd, int speed)
{
struct termios term;
struct speed_struct *s;
for (s = speed_table; s->user_speed != -1; s++)
if (s->user_speed == speed)
break;
if (s->user_speed == -1) {
fprintf(stderr, "tty_speed: invalid speed %d\n", speed);
return FALSE;
}
if (tcgetattr(fd, &term) == -1) {
perror("tty_speed: tcgetattr");
return FALSE;
}
cfsetispeed(&term, s->termios_speed);
cfsetospeed(&term, s->termios_speed);
if (tcsetattr(fd, TCSANOW, &term) == -1) {
perror("tty_speed: tcsetattr");
return FALSE;
}
return TRUE;
}
int tty_is_locked(char *tty)
{
char buffer[50], *s;
FILE *fp;
int pid = 0;
if ((s = strrchr(tty, '/')) != NULL)
s++;
else
s = tty;
sprintf(buffer, "%s/LCK..%s", LOCK_SERIAL_DIR, s);
if ((fp = fopen(buffer, "r")) == NULL)
return FALSE;
if (fscanf(fp, "%d", &pid) != 1) {
fclose(fp);
return FALSE;
}
fclose(fp);
if (kill(pid, 0) == 0)
return TRUE;
return FALSE;
}
int tty_lock(char *tty)
{
char buffer[50], *s;
FILE *fp;
if ((s = strrchr(tty, '/')) != NULL)
s++;
else
s = tty;
sprintf(buffer, "%s/LCK..%s", LOCK_SERIAL_DIR, s);
if ((fp = fopen(buffer, "w")) == NULL)
return FALSE;
fprintf(fp, "%10d\n", getpid());
fclose(fp);
return TRUE;
}
int tty_unlock(char *tty)
{
char buffer[50], *s;
if ((s = strrchr(tty, '/')) != NULL)
s++;
else
s = tty;
sprintf(buffer, "%s/LCK..%s", LOCK_SERIAL_DIR, s);
return unlink(buffer) == 0;
}
|