static int proc_detect(const char *procname)
{
char filename[100] = {0};
sprintf(filename, "%s/%s.pid", LOG_DIR, procname);
int fd = open(filename, O_RDWR | O_CREAT, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
if (fd < 0) {
printf("open file \"%s\" failed!!!\n", filename);
return 1;
}
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_start = 0;
fl.l_whence = SEEK_SET;
fl.l_len = 0;
int ret = fcntl(fd, F_SETLK, &fl);
if (-1 == ret) {
printf("file \"%s\" locked. proc already exit!!!\n", filename);
close(fd);
return 1;
} else {
ftruncate(fd, 0);
char buf[16];
sprintf(buf, "%ld", (long)getpid());
write(fd, buf, strlen(buf) + 1);
//do not close file
return 0;
}
}