struct sockaddr_un local;
local.sun_family = AF_UNIX;
- strcpy(local.sun_path, path); // TODO: strlen ???
+ if(sizeof(local.sun_path) <= strlen(path)) {
+ log_printf(ERROR, "socket path is to long (max %d)", sizeof(local.sun_path)-1);
+ return -1;
+ }
+ strcpy(local.sun_path, path);
unlink(local.sun_path);
int len = SUN_LEN(&local);
int ret = bind(fd, (struct sockaddr*)&local, len);
static char buffer[100];
int ret = 0;
- do { // TODO: replace this whith a actually working readline
+ do { // TODO: replace this whith a actually working non-blocking-readline
memset(buffer, 0, 100);
ret = recv(fd, buffer, sizeof(buffer), 0);
if(!ret)
static char buffer[100];
int ret = 0;
- do { // TODO: replace this whith a actually working readline
+ do { // TODO: replace this whith a actually working non-blocking-readline
memset(buffer, 0, 100);
ret = read(door_fd, buffer, sizeof(buffer));
if(!ret)
int main_loop(int door_fd, int cmd_listen_fd)
{
- log_printf(INFO, "entering main loop");
+ log_printf(NOTICE, "entering main loop");
fd_set readfds, tmpfds;
FD_ZERO(&readfds);
fclose(pid_file);
}
- int door_fd = open(opt.door_dev_, O_RDWR | O_NOCTTY);
- if(door_fd < 0) {
- log_printf(ERROR, "unable to open %s: %s", opt.door_dev_, strerror(errno));
- options_clear(&opt);
- log_close();
- exit(-1);
- }
- ret = setup_tty(door_fd);
- if(ret) {
- close(door_fd);
- options_clear(&opt);
- log_close();
- exit(-1);
- }
-
-
int cmd_listen_fd = init_command_socket(opt.command_sock_);
if(cmd_listen_fd < 0) {
- close(door_fd);
options_clear(&opt);
log_close();
exit(-1);
}
+
+ int door_fd = 0;
+ for(;;) {
+ door_fd = open(opt.door_dev_, O_RDWR | O_NOCTTY);
+ if(door_fd < 0)
+ ret = 2;
+ else {
+ ret = setup_tty(door_fd);
+ if(ret)
+ ret = 2;
+ else
+ ret = main_loop(door_fd, cmd_listen_fd);
+ }
- ret = main_loop(door_fd, cmd_listen_fd);
+ if(ret == 2) {
+ log_printf(ERROR, "%s error, trying to reopen in 5 seconds..", opt.door_dev_);
+ if(door_fd > 0)
+ close(door_fd);
+ sleep(5);
+ }
+ else
+ break;
+ }
close(cmd_listen_fd);
- close(door_fd);
+ if(door_fd > 0)
+ close(door_fd);
if(!ret)
log_printf(NOTICE, "normal shutdown");
else if(ret < 0)
log_printf(NOTICE, "shutdown after error");
- else if(ret == 2)
- log_printf(ERROR, "shutdown after %s read error", opt.door_dev_);
else
log_printf(NOTICE, "shutdown after signal");