PracticeDev/study_clang/Mimic/piggie/piggie.c

55 lines
1.1 KiB
C
Raw Normal View History

2022-12-20 17:31:11 +08:00
#define _GNU_SOURCE
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sched.h>
#include <time.h>
#define STKS (4*4096)
#ifndef CLONE_NEWPID
#define CLONE_NEWPID 0x20000000
#endif
int main(int argc, char **argv)
{
//int pid;
//void *stk;
int fd, i = 0;
char log_file[] = "/var/log/piggie.log";
time_t t;
struct tm *timeinfo;
//stk = mmap(NULL, STKS, PROT_READ | PROT_WRITE,
//MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN, 0, 0);
//pid = clone(do_test, stk + STKS, SIGCHLD | CLONE_NEWPID, argv[1]);
//printf("Child forked, pid %d\n", pid);
fd = open("/dev/null", O_RDONLY);
if (fd != 0) {
dup2(fd, 0);
close(fd);
}
if(NULL != argv[1])
fd = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0600);
else
fd = open(log_file, O_WRONLY | O_TRUNC | O_CREAT, 0600);
dup2(fd, 1);
dup2(fd, 2);
if (fd != 1 && fd != 2)
close(fd);
while (1) {
sleep(1);
time(&t);
timeinfo = localtime(&t);
printf("%d %s\n", i++, asctime(timeinfo));
fflush(stdout);
}
return 0;
}