* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#define RCSID "$Id: tty.c,v 1.24 2005/07/12 01:09:05 paulus Exp $"
+#define RCSID "$Id: tty.c,v 1.27 2008/07/01 12:27:56 paulus Exp $"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
+#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
* out and we want to use the modem lines, we reopen it later
* in order to wait for the carrier detect signal from the modem.
*/
- hungup = 0;
got_sigterm = 0;
connector = doing_callback? callback_script: connect_script;
if (devnam[0] != 0) {
int err, prio;
prio = privopen? OPRIO_ROOT: tty_options[0].priority;
- if (prio < OPRIO_ROOT)
- seteuid(uid);
+ if (prio < OPRIO_ROOT && seteuid(uid) == -1) {
+ error("Unable to drop privileges before opening %s: %m\n",
+ devnam);
+ status = EXIT_OPEN_FAILED;
+ goto errret;
+ }
real_ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0);
err = errno;
- if (prio < OPRIO_ROOT)
- seteuid(0);
+ if (prio < OPRIO_ROOT && seteuid(0) == -1)
+ fatal("Unable to regain privileges");
if (real_ttyfd >= 0)
break;
errno = err;
if (device_script(initializer, ttyfd, ttyfd, 0) < 0) {
error("Initializer script failed");
status = EXIT_INIT_FAILED;
- goto errret;
+ goto errretf;
}
if (got_sigterm) {
disconnect_tty();
- goto errret;
+ goto errretf;
}
info("Serial port initialized.");
}
if (device_script(connector, ttyfd, ttyfd, 0) < 0) {
error("Connect script failed");
status = EXIT_CONNECT_FAILED;
- goto errret;
+ goto errretf;
}
if (got_sigterm) {
disconnect_tty();
- goto errret;
+ goto errretf;
}
info("Serial connection established.");
}
return ttyfd;
+ errretf:
+ if (real_ttyfd >= 0)
+ tcflush(real_ttyfd, TCIOFLUSH);
errret:
if (pty_master >= 0) {
close(pty_master);
exit(0);
}
charshunt_pid = cpid;
- record_child(cpid, "pppd (charshunt)", charshunt_done, NULL);
+ record_child(cpid, "pppd (charshunt)", charshunt_done, NULL, 1);
return 1;
}