/*----------------------------------------------------------------------* * * * Description: Source du fichier exec. /equipe/sysadmin/bin/wtmpxcut * * qui permet de reduire le fichier /var/adm/wtmpx en enle-* * vant toutes les entrees plus vieilles qu'une certaine * * date specifiee en parametre. Si aucun parametre n'est * * specifie, on utilise une valeur par defaut (DELAIS). * * * *----------------------------------------------------------------------*/ #include #include #include #include #include #include #include #define ORIGFILE "/var/adm/wtmpx" #define TEMPFILE "/export/local/sbin/wtmpx_temp" #define DELFILE "/export/local/sbin/wtmpx_del" #define DELAIS (time_t) (45) /* in days */ int main(argc, argv) int argc; char **argv; { struct utmpx wtmpx_rec; int old_fd, new_fd, del_fd; int wtmpx_rec_sz = sizeof(wtmpx_rec); time_t periode, temps_limite; char garbage[80]; if (argc > 1) periode = (time_t) atoi(argv[1]); else periode = DELAIS; /* * Copie de /var/adm/wtmpx into /tmp/wtmpx */ putenv("IFS=' \t\n'"); /* Securite */ putenv("PATH=/usr/bin"); /* Securite */ sprintf(garbage,"cp %s %s", ORIGFILE, TEMPFILE); if (system(garbage) < 0) { fprintf(stderr,"%s: Error in copying %s into %s.\nBye.\n",argv[0], ORIGFILE, TEMPFILE); exit(1); } /* if */ /* * Calcule du temps limite en time_t */ temps_limite = time(NULL) - (periode * 24 * 3600); fprintf(stdout, "\n%s: The date limit is %s\n", argv[0], asctime(localtime(&temps_limite))); /* * Open the new file for writing */ if ((new_fd = open(ORIGFILE, O_WRONLY | O_TRUNC)) < 0) { fprintf(stderr, "%s: Error %d in writing %s .\nBye.\n", argv[0], errno, ORIGFILE); exit(1); } /* if */ /* * Open the del file for writing */ if ((del_fd = open(DELFILE, O_WRONLY | O_TRUNC | O_CREAT )) < 0) { fprintf(stderr, "%s: Error %d in writing %s.\nBye.\n", argv[0], errno, DELFILE); exit(1); } /* if */ /* * Open the copy of wtmp for readonly */ if ((old_fd = open(TEMPFILE, O_RDONLY)) < 0) { fprintf(stderr, "%s: Error %d in opening %s.\nBye.\n", argv[0], errno, TEMPFILE); exit(1); } /* if */ /* * En partant du debut du fichier, on compare les dates * et si elles sont trop vieilles, on les oublies. * Lorsqu'elles sont dans la periode, on les garde dans * le fichier /var/adm/wtmpx. */ while (read(old_fd, &wtmpx_rec, wtmpx_rec_sz) == wtmpx_rec_sz) { if (wtmpx_rec.ut_xtime >= temps_limite) { if (write(new_fd, &wtmpx_rec, wtmpx_rec_sz) != wtmpx_rec_sz) { fprintf(stderr, "%s: Error %d in writing the record.\nBye.", argv[0], errno); exit(1); } /* if */ } /* if */ else { if (write(del_fd, &wtmpx_rec, wtmpx_rec_sz) != wtmpx_rec_sz) { fprintf(stderr, "%s: Error %d in writing the record.\nBye.", argv[0], errno); exit(1); } /* if */ } } /* while */ unlink (TEMPFILE); exit(0); /* Nice Exit */ } /* main() */