⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 svr_common.c

📁 linux/unix下c/s形式的资源监视,客户端负责搜集机器,再传送到服务端.
💻 C
字号:
/* * Some common functions to build a daemon server program. * * Copyright (c) 2004, by:      Jian Shen *    All rights reserved.      Peking University, China *                             <shenjian@net.pku.edu.cn> * * This file may be used subject to the terms and conditions of the * GNU Library General Public License Version 2, or any later version * at your option, as published by the Free Software Foundation. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU Library General Public License for more details. * */#include "svr_common.h"#include <fcntl.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/stat.h>#include <sys/types.h>static int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len){	struct flock lock;	lock.l_type = type;	lock.l_start = offset;	lock.l_whence = whence;	lock.l_len = len;	return (fcntl(fd, cmd, &lock));}#define write_lock(fd, offset, whence, len) lock_reg(fd, F_SETLK, F_WRLCK, offset, whence, len)void check_single_on(const char* PIDFILE, const char* svr_name){	int fd, val;	char buf[10];	if((fd = open(PIDFILE, O_WRONLY | O_CREAT, 0644)) <0 ) {		perror("open in check_single_on()");		exit(1);	}	/* try and set a write lock on the entire file */	if(write_lock(fd, 0, SEEK_SET, 0) <0 ) {		if(errno == EACCES || errno == EAGAIN) {			printf("%s daemon is already running.\n", svr_name);			exit(0);			/* gracefully exit, daemon is already running */		}else {			perror("write_lock");			close(fd);			exit(1);		}	}	/* truncate to zero length, now that we have the lock */	if(ftruncate(fd, 0) <0) {		perror("ftruncate");		close(fd);				exit(1);	}	/* write our process id */	sprintf(buf, "%d\n", getpid());	if(write(fd, buf, strlen(buf)) != strlen(buf)) {		perror("write in check_single_on()");		close(fd);				exit(1);	}	/* set close-on-exec flag for descriptor */	if((val = fcntl(fd, F_GETFD, 0) <0 )) {		perror("fcntl");		close(fd);		exit(1);	}	val |= FD_CLOEXEC;	if(fcntl(fd, F_SETFD, val) <0 ) {		perror("fcntl again");		close(fd);		exit(1);	}	/* leave file open until we terminate: lock will be held */}int check_proc_running(const char* PIDFILE, const char* svr_name){	int fd, rv;	char buf[10];	if((fd = open(PIDFILE, O_RDWR, 0644)) <0 ) {		printf("%s is not running now.\n", svr_name);		exit(1);	}	/* try and set a write lock on the entire file */	if(write_lock(fd, 0, SEEK_SET, 0) <0 ) {		if(errno == EACCES || errno == EAGAIN) {			/* migsvr server is running, we should return it's pid */			bzero(buf, 10);			rv = read(fd, buf, 10);			buf[rv] = '\0';			close(fd);			return atoi(buf);		}else {			perror("write_lock");			close(fd);			return -1;		}	}	close(fd);	printf("%s is not running now.\n", svr_name);	return -1;}/* to be a daemon process */int daemon_init(){	pid_t pid;	if((pid = fork()) <0)		return -1;	else if(pid !=0 )		exit(0);				/* parent goes bye-bye */	/* child continues *///	setsid();					/* become session leader */	chdir("/");					/* change working directory */	umask(0);					/* clear our file mode creation mask */	return 0;}void daemon_notty(){	setsid();					/* become session leader */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -