2009-05-04 18:35:27 +02:00
|
|
|
--- Makefile
|
|
|
|
+++ Makefile 2009-05-04 12:21:20.643778409 +0200
|
|
|
|
@@ -71,10 +71,10 @@ libblogger.a: libblogger.o
|
|
|
|
$(AR) $@ $^
|
|
|
|
|
|
|
|
showconsole: showconsole.c libconsole.o
|
|
|
|
- $(CC) $(CFLAGS) $(CLOOP) -o $@ $^ -pthread
|
|
|
|
+ $(CC) $(CFLAGS) $(CLOOP) -o $@ $^ -Wl,--as-needed -pthread
|
|
|
|
|
|
|
|
blogd: blogd.c libconsole.o
|
|
|
|
- $(CC) $(CFLAGS) $(CLOOP) -o $@ $^ -lutil -pthread
|
|
|
|
+ $(CC) $(CFLAGS) $(CLOOP) -o $@ $^ -Wl,--as-needed -lutil -pthread
|
|
|
|
|
|
|
|
blogger: blogger.c libblogger.a
|
|
|
|
$(CC) $(CFLAGS) $(CLOOP) -o $@ $^
|
2008-06-23 04:34:49 +02:00
|
|
|
--- blogd.c
|
2009-05-04 18:35:27 +02:00
|
|
|
+++ blogd.c 2009-05-04 12:04:11.566915578 +0200
|
2008-06-23 04:34:49 +02:00
|
|
|
@@ -220,10 +220,12 @@ int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int fd, fd2, flags;
|
|
|
|
int ptm, pts, cntrtty = 1;
|
|
|
|
- pid_t pid, ppid = getppid();
|
|
|
|
+ const pid_t ppid = getppid();
|
|
|
|
+ const pid_t pgrp = getpgid(ppid);
|
|
|
|
char ptsname[NAME_MAX+1];
|
|
|
|
struct termios t;
|
|
|
|
struct winsize w;
|
|
|
|
+ pid_t pid;
|
|
|
|
time_t tt;
|
|
|
|
char *stt, *name = ttyname(0);
|
|
|
|
|
|
|
|
@@ -276,7 +278,7 @@ int main(int argc, char *argv[])
|
|
|
|
if (!w.ws_row)
|
|
|
|
w.ws_row = 24;
|
|
|
|
if (!w.ws_col)
|
|
|
|
- w.ws_row = 80;
|
|
|
|
+ w.ws_col = 80;
|
|
|
|
|
|
|
|
fd2 = -1;
|
|
|
|
do {
|
|
|
|
@@ -337,6 +339,7 @@ int main(int argc, char *argv[])
|
|
|
|
dup2(ptm, 0);
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
|
|
|
+ close(pts);
|
|
|
|
close(ptm);
|
|
|
|
if (fd > 2)
|
|
|
|
close(fd);
|
|
|
|
@@ -362,7 +365,7 @@ int main(int argc, char *argv[])
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
pidfile();
|
|
|
|
- prepareIO(reconnect, 0, 1, fd2);
|
|
|
|
+ prepareIO(reconnect, pgrp, 0, 1, fd2);
|
|
|
|
while (!signaled)
|
|
|
|
safeIO();
|
|
|
|
|
|
|
|
@@ -380,9 +383,6 @@ int main(int argc, char *argv[])
|
|
|
|
close(1);
|
|
|
|
(void)tcflush(2, TCOFLUSH);
|
|
|
|
close(2);
|
|
|
|
-
|
|
|
|
- (void)tcflush(pts, TCIOFLUSH);
|
|
|
|
- close(pts);
|
|
|
|
(void)tcflush(0, TCIFLUSH);
|
|
|
|
close(0);
|
|
|
|
rmfpid();
|
|
|
|
--- libconsole.c
|
2009-05-04 18:35:27 +02:00
|
|
|
+++ libconsole.c 2009-05-04 12:06:34.477901880 +0200
|
|
|
|
@@ -15,6 +15,7 @@
|
|
|
|
#include <sys/types.h> /* Defines the macros major and minor */
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
+#include <sys/mman.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
@@ -215,7 +216,7 @@ static inline void safeout (int fd, cons
|
2008-11-20 16:27:15 +01:00
|
|
|
/*
|
|
|
|
* Twice used: safe in
|
|
|
|
*/
|
|
|
|
-static inline ssize_t safein (int fd, char *ptr, size_t s)
|
|
|
|
+static inline ssize_t safein (int fd, char *ptr, size_t s, const int noerr)
|
|
|
|
{
|
|
|
|
int saveerr = errno;
|
|
|
|
ssize_t r = 0;
|
2009-05-04 18:35:27 +02:00
|
|
|
@@ -223,7 +224,7 @@ static inline ssize_t safein (int fd, c
|
2008-11-20 16:27:15 +01:00
|
|
|
static int repeated;
|
|
|
|
|
|
|
|
if (s > SSIZE_MAX)
|
|
|
|
- error("Can not read from fd %d: %m", fd, strerror(EINVAL));
|
|
|
|
+ s = SSIZE_MAX;
|
|
|
|
|
|
|
|
if ((ioctl(fd, FIONREAD, &t) < 0) || (t == 0)) {
|
|
|
|
fd_set check;
|
2009-05-04 18:35:27 +02:00
|
|
|
@@ -242,8 +243,11 @@ static inline ssize_t safein (int fd, c
|
2008-11-20 16:27:15 +01:00
|
|
|
} while (r < 0 && (errno == EINTR || errno == EAGAIN));
|
|
|
|
|
|
|
|
/* Do not exit on a broken FIFO */
|
|
|
|
- if (r < 0 && errno != EPIPE)
|
|
|
|
+ if (r < 0 && errno != EPIPE) {
|
|
|
|
+ if (noerr)
|
|
|
|
+ goto out;
|
|
|
|
error("Can not read from fd %d: %s\n", fd, STRERR);
|
|
|
|
+ }
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
2009-05-04 18:35:27 +02:00
|
|
|
@@ -261,6 +265,8 @@ static inline ssize_t safein (int fd, c
|
2008-11-20 16:27:15 +01:00
|
|
|
errno = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
+ if (noerr)
|
|
|
|
+ goto out;
|
|
|
|
error("Can not read from fd %d: %s\n", fd, STRERR);
|
|
|
|
}
|
|
|
|
repeated = 0;
|
2009-05-04 18:35:27 +02:00
|
|
|
@@ -724,10 +730,12 @@ static void *action(void *dummy)
|
2008-06-23 04:34:49 +02:00
|
|
|
* Prepare I/O
|
|
|
|
*/
|
|
|
|
static const char *fifo_name = _PATH_BLOG_FIFO;
|
|
|
|
+static pid_t pgroup = -1;
|
|
|
|
|
|
|
|
-void prepareIO(void (*rfunc)(int), const int in, const int out, const int second)
|
|
|
|
+void prepareIO(void (*rfunc)(int), const pid_t pgrp, const int in, const int out, const int second)
|
|
|
|
{
|
|
|
|
vc_reconnect = rfunc;
|
|
|
|
+ pgroup = pgrp;
|
|
|
|
fdread = in;
|
|
|
|
fdwrite = out;
|
|
|
|
fdsec = second;
|
2009-05-04 18:35:27 +02:00
|
|
|
@@ -744,12 +752,13 @@ void prepareIO(void (*rfunc)(int), const
|
|
|
|
warn("can not open named fifo %s: %s\n", fifo_name, STRERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
+ (void)mlockall(MCL_FUTURE);
|
|
|
|
}
|
|
|
|
|
2008-11-20 16:27:15 +01:00
|
|
|
/*
|
|
|
|
* Seek for input, more input ...
|
|
|
|
*/
|
|
|
|
-static void more_input (struct timeval *timeout)
|
|
|
|
+static void more_input (struct timeval *timeout, const int noerr)
|
|
|
|
{
|
|
|
|
fd_set watch;
|
|
|
|
int nfds, wfds;
|
2009-05-04 18:35:27 +02:00
|
|
|
@@ -777,10 +786,20 @@ static void more_input (struct timeval *
|
2008-11-20 16:27:15 +01:00
|
|
|
goto nothing;
|
2008-06-23 04:34:49 +02:00
|
|
|
|
|
|
|
if (FD_ISSET(fdread, &watch)) {
|
2008-11-20 16:27:15 +01:00
|
|
|
- const ssize_t cnt = safein(fdread, (char*)trans, sizeof(trans));
|
|
|
|
+ const ssize_t cnt = safein(fdread, (char*)trans, sizeof(trans), noerr);
|
2008-06-23 04:34:49 +02:00
|
|
|
+ static struct winsize owz;
|
|
|
|
+ struct winsize wz;
|
|
|
|
|
|
|
|
if (cnt > 0) {
|
|
|
|
- parselog(trans, cnt); /* Parse and make copy of the input */
|
|
|
|
+ if (ioctl(fdwrite, TIOCGWINSZ, &wz) == 0) {
|
|
|
|
+ if (memcmp(&owz, &wz, sizeof(struct winsize))) {
|
|
|
|
+ ioctl(fdread, TIOCSWINSZ, &wz);
|
|
|
|
+ (void)memcpy(&owz, &wz, sizeof(struct winsize));
|
|
|
|
+ if (pgroup > 1)
|
|
|
|
+ killpg(pgroup, SIGWINCH);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ parselog(trans, cnt); /* Parse and make copy of the input */
|
|
|
|
|
|
|
|
safeout(fdwrite, (char*)trans, cnt); /* Write copy of input to real tty */
|
|
|
|
(void)tcdrain(fdwrite);
|
2009-05-04 18:35:27 +02:00
|
|
|
@@ -795,7 +814,7 @@ static void more_input (struct timeval *
|
2008-11-20 16:27:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fdfifo > 0 && FD_ISSET(fdfifo, &watch)) {
|
|
|
|
- const ssize_t cnt = safein(fdfifo, (char*)trans, sizeof(trans));
|
|
|
|
+ const ssize_t cnt = safein(fdfifo, (char*)trans, sizeof(trans), noerr);
|
|
|
|
|
|
|
|
if (cnt > 0) {
|
|
|
|
copylog(trans, cnt); /* Make copy of the input */
|
2009-05-04 18:35:27 +02:00
|
|
|
@@ -873,7 +892,7 @@ skip:
|
2008-11-20 16:27:15 +01:00
|
|
|
|
|
|
|
timeout.tv_sec = 5;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
- more_input(&timeout);
|
|
|
|
+ more_input(&timeout, 0);
|
|
|
|
|
|
|
|
if (flog && !running) {
|
|
|
|
int policy = SCHED_RR;
|
2009-05-04 18:35:27 +02:00
|
|
|
@@ -895,7 +914,7 @@ skip:
|
|
|
|
void closeIO(void)
|
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
- int n = 6;
|
|
|
|
+ int n = 240;
|
|
|
|
|
|
|
|
/* Maybe we've catched a signal, therefore */
|
|
|
|
if (flog) {
|
|
|
|
@@ -918,9 +937,9 @@ void closeIO(void)
|
|
|
|
n--;
|
|
|
|
|
2008-11-20 16:27:15 +01:00
|
|
|
timeout.tv_sec = 0;
|
2009-05-04 18:35:27 +02:00
|
|
|
- timeout.tv_usec = 5*100*1000; /* A half second */
|
|
|
|
+ timeout.tv_usec = 25*1000;
|
2008-11-20 16:27:15 +01:00
|
|
|
|
|
|
|
- more_input(&timeout);
|
|
|
|
+ more_input(&timeout, 1);
|
|
|
|
|
|
|
|
if (!flog)
|
|
|
|
break;
|
2009-05-04 18:35:27 +02:00
|
|
|
@@ -968,7 +987,7 @@ static void ctty(pid_t pid, unsigned int
|
2008-11-20 16:27:15 +01:00
|
|
|
sprintf(fetched, "/proc/%d/stat", (int)pid);
|
|
|
|
if ((fd = open(fetched, O_RDONLY|O_NOCTTY)) < 0)
|
|
|
|
error("can not open(%s): %s\n", fetched, STRERR);
|
|
|
|
- cnt = safein(fd, fetched, sizeof(fetched));
|
|
|
|
+ cnt = safein(fd, fetched, sizeof(fetched), 0);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (cnt <= 0)
|
2008-06-23 04:34:49 +02:00
|
|
|
--- libconsole.h
|
2008-11-20 16:27:15 +01:00
|
|
|
+++ libconsole.h 2008-06-13 13:31:59.302034000 +0200
|
2008-06-23 04:34:49 +02:00
|
|
|
@@ -2,6 +2,6 @@ extern void pushd(const char * path);
|
|
|
|
extern void popd(void);
|
|
|
|
extern char * fetchtty(const pid_t pid, const pid_t ppid, unsigned int *mjmi);
|
|
|
|
extern char * secondtty(char * compare);
|
|
|
|
-extern void prepareIO(void (*rfunc)(int), const int in, const int out, const int second);
|
|
|
|
+extern void prepareIO(void (*rfunc)(int), const pid_t pgrp, const int in, const int out, const int second);
|
|
|
|
extern void safeIO (void);
|
|
|
|
extern void closeIO(void);
|