Path: utzoo!utgpu!water!watmath!uunet!lll-winken!pacbell!pbhacker!tlh From: tlh@pbhacker.UUCP (Lee Hounshell) Newsgroups: unix-pc.sources Subject: Repost of phdaemon - version #3 Message-ID: <112@pbhacker.UUCP> Date: 16 Jul 88 02:51:46 GMT Organization: Anything For A Buck Lines: 572 I've had a few requests for a repost of the fixed version of phdaemon.. for those of you not familiar with it, phdaemon will monitor both incoming and outgoing calls on your Unix PC. enjoy. Lee Hounshell -----cut here----- /************************************************************************\ ** ** ** Program name: phdaemon.c (Phone Daemon) ** ** Programmers: original version - Paul J. Condie (UUCP=pcbox!pjc) ** ** modified version - Lenny Tropiano (UUCP=icus!lenny) ** ** fixes/enhancements - Lee Hounshell (UUCP=pbhacker!tlh)** ** kernel access rtns - Michael Ditto (ford@crash.CTS.COM)** ** Date: December 13, 1987 ** ** ** ************************************************************************** ** ** ** Program use: Program is run as a daemon process from the boot ** ** procedure /etc/rc. It can be placed in /etc/daemons/ ** ** and will be started up apon system boot. This program ** ** monitors the data line (defined as /dev/ph1) for ** ** any kind of activity (or lack thereof) and displays ** ** the information in the Phone Managers window. Freeing ** ** up any other windows from being allocated for that use ** ** ** ** This program checks for DATA or VOICE, UUCP logins, ** ** User logins, UUCP outgoing, Internal use of outgoing ** ** modem. This process will terminate if the /etc/ph ** ** process is terminated. ** ** ** ** Enhancements: Bugs in properly detecting incoming calls have been ** ** fixed. The program now reports stray lock files in ** ** the status window. Errors are reported by placing ** ** "ERROR" in the status window. User logins and errors ** ** are now logged in the "/usr/adm/phdaemon.log" file. ** ** ** \************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define SLEEP 15 /* Sleep time (interval between)*/ #define SNOOZE 5 /* Short Sleep time (short time)*/ #define NICE 5 /* Niceness value */ #define LINE "ph1" /* /etc/utmp user for DATA2 */ #define BEEP "\07\07" /* ring the bell */ #define PHPID "/usr/lib/ua/phpid" /* File has pid of /etc/ph */ #define LOCKDIR "/usr/spool/uucp" /* UUCP Lock Directory */ #define LOCKFIL "LCK.." /* Lock file prefix */ #define OFFSET sizeof(struct phdef) /* kernel offset (0 for ph0) */ #define LOGFILE "/usr/adm/phdaemon.log" /* login tracking file */ /* Command to locate window device in use by /etc/ph */ #define WINDCMD "ps -p%d | grep ph | cut -c9-11" #define ESC 27 #define BOLD 1 #define REV 7 #define ROW 1 #define COL 24 #define ERROR ":%c[%dm ERROR %c[0m" /* an error was detected */ #define INCOMING "<%c[%dm%-8.8s%c[0m" /* Incoming call string */ #define OUTGOING ">%c[%dm%-8.8s%c[0m" /* Outgoing call string */ #define IDLE "%c[0m: IDLE " /* Idle phone device */ #define LOGIN "ut_id,LINE)) { werror("Internal Error: /etc/utmp",0); } if (utmp->ut_type == USER_PROCESS) { if (wait_a_bit) { wait_a_bit = 0; sleep(SNOOZE); continue; } if (beep) { sprintf(buffer,INCOMING, ESC,REV,utmp->ut_user,ESC); home_cursor(); write(0,buffer,strlen(buffer)); write(0,BEEP,2); beep = 0; fprintf(log_file, "USER %s logged IN at %s\n", utmp->ut_user, cur_time()); fnd_user = 1; } sleep(SNOOZE); goto STILL_THERE; } if (fnd_user) { fprintf(log_file, "USER %s logged OUT at %s\n", utmp->ut_user, cur_time()); wait_a_bit = 1; fnd_user = 0; continue; } } else goto LCK_ERROR; } wait_a_bit = 1; setpwent(); /* rewind /etc/passwd */ if (calltype == OUT_USER) { if ((pwent = getpwuid(proc.p_uid)) == NULL) { sprintf(buffer, "cannot locate uid %d in /etc/passwd", proc.p_uid); werror(buffer,1); } } else if (calltype == IN_USER) { utmpentry(LINE); if (*userlogin == NULL) { write(0, LOGIN, strlen(LOGIN)); continue; } if ((pwent = getpwnam(userlogin)) == NULL) { sprintf(buffer, "cannot locate user %s in /etc/passwd", userlogin); werror(buffer,1); } } endpwent(); /* close /etc/passwd */ home_cursor(); switch (calltype) { case OUT_USER: if (beep) { sprintf(buffer,OUTGOING,ESC,REV, pwent->pw_name, ESC); write(0,buffer,strlen(buffer)); beep = 0; } break; case UUCP: sprintf(machine,"%s",(char *)findmachine()); utmpentry(LINE); if (*userlogin == NULL) { if (*machine != NULL) { sprintf(buffer,OUTGOING, ESC,BOLD,machine,ESC); } else { sprintf(buffer,UUCICO,ESC, BOLD,'>',ESC); } } else { if (*machine != NULL) { sprintf(buffer,INCOMING, ESC,BOLD,machine,ESC); } else { sprintf(buffer,UUCICO, ESC,BOLD,'<',ESC); } } endutent(); if (beep) { write(0,buffer,strlen(buffer)); write(0,BEEP,2); beep = 0; } break; } } } close(dfd); if (wait_a_bit) sleep(SLEEP); } } check_command(cmd) /* Check the list of commands */ char *cmd; { register i; i = 0; while (*(prog[i].name) != NULL) { if (strcmp(prog[i].name, cmd) == 0) return(prog[i].type); i++; } return(PPROCESS); } utmpentry(line) char *line; { struct utmp *utent, *getutent(); setutent(); while ((utent = getutent()) != NULL) { if (strncmp(utent->ut_line,line,strlen(line)) == 0 && utent->ut_type == USER_PROCESS) { sprintf(userlogin,"%s",utent->ut_user); endutent(); return; } } endutent(); *userlogin = NULL; } home_cursor() { sprintf(buffer,HOME,ESC,ROW,COL); write(0,buffer,7); } int terminate() { home_cursor(1); write(0,BLANK,strlen(BLANK)); close(wfd); close(kmemfd); close(memfd); fclose(log_file); exit(0); } setup() { struct tunable tune; if ((kmemfd=open("/dev/kmem", O_RDWR)) == -1) werror("can't open /dev/kmem",0); if ((memfd=open("/dev/mem", O_RDWR)) == -1) werror("can't open /dev/mem",0); if (nlist("/unix", unixsym)) werror("can't nlist /unix",0); read_kmem((char *)&tune, (unixsym[0].n_value), (long) sizeof tune); read_kmem((char *)&(unixsym[1].n_value), (unixsym[1].n_value), (long)sizeof (unixsym[1].n_value)); MAXPROC = tune.nproc; } void read_kmem(caddr, kaddr, nbytes) char *caddr; long kaddr; long nbytes; { if (lseek(kmemfd, kaddr, 0)<0L || read(kmemfd, caddr, (unsigned)nbytes) != nbytes ) werror("can't read /dev/kmem",0); } void read_mem(caddr, paddr, nbytes) char *caddr; long paddr; long nbytes; { if (lseek(memfd, paddr, 0)<0L || read(memfd, caddr, (unsigned)nbytes) != nbytes) werror("can't read /dev/mem",0); } int findproc(pid) int pid; { register i; for (i=0 ; i