| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- /**************************************************************************************************
- ** **
- ** 文件名称: commSingleApp.c **
- ** 版权所有: CopyRight @ LEON WorkStudio CO.LTD. 2017 **
- ** 文件描述: 判断系统中是否已经存在该进程的实例 **
- ** =========================================================================================== **
- ** 创建信息: | 2017-9-14 | LEON | 创建本模块 **
- ** =========================================================================================== **
- ** 修改信息: 单击此处添加.... **
- **************************************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <printf.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/stat.h>
- #include "commSingleApp.h"
- /**************************************************************************************************
- ** 函数名称: lockfile
- ** 功能描述: 对文件加锁
- ** 输入参数: 无
- ** 输出参数: 无
- ** 返回参数: 无
- **************************************************************************************************/
- static int lockfile(int fd)
- {
- struct flock fl;
- fl.l_type = F_WRLCK;
- fl.l_start = 0;
- fl.l_whence = SEEK_SET;
- fl.l_len = 0;
- return(fcntl(fd, F_SETLK, &fl));
- }
- /**************************************************************************************************
- ** 函数名称: proc_is_exist
- ** 功能描述: 判断系统中是否存在该进程
- ** 输入参数: procname: 进程名
- ** 输出参数: 无
- ** 返回参数: 返回1表示系统中已经存在该进程了;返回0表示系统中不存在该进程
- ** 注意事项: 此处加锁完后无需对文件进行close,而是进程退出后由系统来释放;否则无法起到保护的作用
- **************************************************************************************************/
- int proc_is_exist(const char *procname)
- {
- int fd;
- char buf[16];
- char filename[100];
- sprintf(filename, "/var/run/%s.pid", procname);
- 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;
- }
- if (lockfile(fd) == -1) { /* 尝试对文件进行加锁 */
- printf("file \"%s\" locked. proc already exit!!!\n", filename);
- close(fd);
- return 1;
- } else {
- ftruncate(fd, 0); /* 写入运行实例的pid */
- sprintf(buf, "%ld", (long)getpid());
- write(fd, buf, strlen(buf) + 1);
- return 0;
- }
- }
- #if 0 //测试
- if (proc_is_exist(g_fk_process_name) == TRUE) { /* 单实例运行 */
- print_sys("an \"%s\" already running in system. exit now...\n", g_fk_process_name);
- return 0;
- } else {
- print_sys("\"%s\" starting...\n", g_fk_process_name);
- }
- #endif
|