#include #include #include #include #include #include #include #include #include #include #define BUFLEN 255#define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ } while(0)int main(){ int fd; int fd_log; char *buf01="open /dev/urandom success\n"; char *buf02="open /dev/urandom failed\n"; char dest[100]; int size; int max=604800;//7days int i=0; time_t timep; char tmpBuf[BUFLEN]; umask(0); fd_log = open("/var/log/test_urandom.log",O_CREAT|O_APPEND|O_RDWR,0666); if (fd_log == -1){ ERR_EXIT("open error"); }else{ printf("open /var/log/test_urandom.log success"); } while(i < max){ fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY); //now time(&timep); strcpy(tmpBuf,ctime(&timep)); //failed if (fd < 0){ strcpy(dest,tmpBuf); strcat(dest,":"); strcat(dest,buf02); if((size=write(fd_log,dest,strlen(dest))) < 0){ perror("write failed"); } }else{ //success strcpy(dest,tmpBuf); strcat(dest,":"); strcat(dest,buf01); if ((size=write(fd_log,dest,strlen(dest))) < 0){ perror("write failed"); } } close(fd); sleep(1); i+=1; } close(fd_log); return 0;}