#define KEY 0x0000DEAD // Default KEY used by DAEMON #include #include #include #include #include #include #include #include #include #include #include #include #include struct ONLINE { char tagline[64]; char username[24]; char status[256]; char host[256]; char currentdir[256]; long groupid; time_t login_time; struct timeval tstart; unsigned long bytes_xfer; pid_t procid; }; static struct ONLINE *online; static int dl = 0; static int ul = 0; static int num_users = 0; static int shmid; static struct shmid_ds ipcbuf; static void quit(); static double calc_time(); static char * trim( char *str ) { char *ibuf; char *obuf; if ( str ) { for ( ibuf = obuf = str; *ibuf; ) { while ( *ibuf && ( isspace ( *ibuf ) ) ) ibuf++; if ( *ibuf && ( obuf != str ) ) *( obuf++ ) = ' '; while ( *ibuf && ( !isspace ( *ibuf ) ) ) *( obuf++ ) = *( ibuf++ ); } *obuf = '\0'; } return( str ); } static char hmsbuf[50]; void pid_hms_format( time_t testtime ) { time_t timenow = time( NULL ); time_t difftime; int days = 0; int hours = 0; int minutes = 0; int seconds = 0; difftime = timenow - testtime; while ( difftime >= (time_t)3600 ) { difftime -= (time_t)3600; hours++; } while ( difftime >= (time_t)60 ) { difftime -= (time_t)60; minutes++; } if ( hours != 0 ) sprintf( hmsbuf, "%02d:%02d:%02d", hours, minutes, difftime ); else sprintf( hmsbuf, " %02d:%02d", minutes, difftime ); } static int checkusers(void) { register int i, j, found = 0; char statbuf[ 500 ]; char pidbuf[ 10 ]; char filename[20]; for (i=0; i 5) { sprintf( statbuf, "Idle: %-8.8s", hmsbuf ); } /* Doing something else... */ else { sprintf( statbuf, "\"%s\"", online[i].status ); trim(statbuf); } pid_hms_format( online[i].login_time); fprintf( stdout, "| %-8.8s | %-5.5s | %-8.8s | %-44.44s |\n", online[i].username, pidbuf, hmsbuf, statbuf); found++; } return (found); } int main( int argc, char **argv ) { int numusers; if ((shmid = shmget( (key_t)KEY, 0, 0)) == -1) { fprintf(stdout, "No Users Currently On Site!\n"); exit(1); } if ((online = (struct ONLINE *)shmat( shmid, NULL, SHM_RDONLY)) == (struct ONLINE *)-1) { fprintf(stdout, "Error: (SHMAT) Failed!\n"); exit(1); } shmctl( shmid, IPC_STAT, &ipcbuf); num_users = ipcbuf.shm_segsz / sizeof( struct ONLINE ); fprintf(stdout,".----------------------------------------------------------------------------.\n"); fprintf(stdout,"| Username | PID | OnLine | Action |\n" ); fprintf(stdout,"|----------+-------+----------+----------------------------------------------|\n" ); numusers = checkusers(); fprintf(stdout,"`----------------------------------------------------------------------------'\n" ); quit( 0 ); } static double calc_time(int pid) { struct timeval tstop; double delta, rate; if (online[pid].bytes_xfer < 1) return 0; gettimeofday(&tstop, (struct timezone *)0 ); delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000.)) - ((online[pid].tstart.tv_sec*10.)+(online[pid].tstart.tv_usec/100000.)); delta = delta/10.; rate = ((online[pid].bytes_xfer / 1024.0) / (delta)); if (!rate) rate++; return (double)(rate); } static void quit(int exit_status) { shmctl( shmid, IPC_STAT, &ipcbuf); if (ipcbuf.shm_nattch <= 1) { shmctl( shmid, IPC_RMID, 0); } shmdt(0); exit(exit_status); }