]>
git.ipfire.org Git - ipfire-3.x.git/blob - initscripts/src/console_check.c
11 #include <linux/serial.h>
12 #include <linux/serial_core.h>
20 struct speeds speed_map
[] =
52 int termcmp(struct termios
*a
, struct termios
*b
) {
53 if (a
->c_iflag
!= b
->c_iflag
|| a
->c_oflag
!= b
->c_oflag
||
54 a
->c_cflag
!= b
->c_cflag
|| a
->c_lflag
!= b
->c_lflag
||
55 cfgetispeed(a
) != cfgetispeed(b
) || cfgetospeed(a
) != cfgetospeed(b
))
57 return memcmp(a
->c_cc
, b
->c_cc
, sizeof(a
->c_cc
));
60 int get_serial_speed(int fd
) {
63 if (!tcgetattr(fd
, &mode
)) {
67 speed
= cfgetospeed(&mode
);
68 for (i
= 0; speed_map
[i
].value
!= 0; i
++)
69 if (speed_map
[i
].speed
== speed
)
70 return speed_map
[i
].value
;
75 int compare_termios_to_console(char *dev
, int *speed
) {
76 struct termios cmode
, mode
;
79 cfd
= open ("/dev/console", O_RDONLY
);
80 tcgetattr(cfd
, &cmode
);
83 fd
= open(dev
, O_RDONLY
|O_NONBLOCK
);
86 if (!termcmp(&cmode
, &mode
)) {
87 *speed
= get_serial_speed(fd
);
95 char *serial_tty_name(int type
) {
97 case PORT_8250
...PORT_MAX_8250
:
112 char *check_serial_console(int *speed
) {
114 char *ret
= NULL
, *device
;
116 struct serial_struct si
, si2
;
119 memset(&si
, 0, sizeof(si
));
120 memset(&si2
, 0, sizeof(si
));
122 fd
= open("/dev/console", O_RDWR
);
123 if (ioctl (fd
, TIOCLINUX
, &twelve
) >= 0)
126 if (ioctl(fd
, TIOCGSERIAL
, &si
) < 0)
130 tty_name
= serial_tty_name(si
.type
);
134 asprintf(&device
, "%s%d", tty_name
, si
.line
);
135 fd
= open(device
, O_RDWR
|O_NONBLOCK
);
139 if (ioctl(fd
, TIOCGSERIAL
, &si2
) < 0)
142 if (memcmp(&si
,&si2
, sizeof(si
)))
145 *speed
= get_serial_speed(fd
);
152 int emit_console_event(char *dev
, int speed
) {
153 char *args
[] = { "initctl", "emit", "--no-wait", "serial-console-available", NULL
, NULL
, NULL
};
155 asprintf(&args
[4],"DEV=%s", dev
);
157 asprintf(&args
[5],"SPEED=%d", speed
);
158 execv("/sbin/initctl", args
);
162 int main(int argc
, char **argv
) {
167 printf("usage: console_check <device>\n");
172 if (!strcmp(device
, "console")) {
173 device
= check_serial_console(&speed
);
175 return emit_console_event(device
, speed
);
176 } else if (compare_termios_to_console(device
, &speed
)) {
177 return emit_console_event(device
, speed
);