#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/file.h>
#include "confile.h"
static char port[] = "eth1";
static char cmd_powerdown[] = "/usr/bin/net_powerdown.sh";
static char cmd_powerup[] = "/usr/bin/net_powerup.sh";
static char cmd_linkdown[] = "/usr/bin/net_linkdown.sh";
static char cmd_linkup[] = "/usr/bin/net_linkup.sh";
static char cmd_ping[] = "ping www.baidu.com -c 1";
static int reboot_period = 24 * 60 * 60;
struct system_configs* get_system_configs(char *configfile)
{
struct system_configs *new_syscfg = (struct system_configs *) malloc(sizeof(struct system_configs));
strcpy(new_syscfg->port, port);
new_syscfg->linkdown_time = 15;
new_syscfg->reconnect_cnt = 3;
new_syscfg->repower_en = 0;
new_syscfg->repower_cnt = 3;
new_syscfg->powerdown_time = 10;
new_syscfg->max_repower_time = 120;
new_syscfg->reboot_en = 0;
new_syscfg->ping_faile_cnt = 2;
new_syscfg->ping_faile_en = 0;
new_syscfg->ping_delay = 0;
new_syscfg->cmp_packets_en = 0;
new_syscfg->cmp_packets_delay = 60;
new_syscfg->rx_change_cnt = 11;
new_syscfg->tx_change_cnt = 12;
strcpy(new_syscfg->cmd_powerdown, cmd_powerdown);
strcpy(new_syscfg->cmd_powerup, cmd_powerup);
strcpy(new_syscfg->cmd_linkdown, cmd_linkdown);
strcpy(new_syscfg->cmd_linkup, cmd_linkup);
strcpy(new_syscfg->cmd_ping, cmd_ping);
new_syscfg->reboot_period = reboot_period;
FILE *fp = fopen(configfile, "r");
if (fp == NULL){
printf("Failed to open file %s\n", configfile);
exit(1);
}
char buf[1024] = {0};
char *line;
char *endofbuf = buf + 1023;
while(fgets(buf, sizeof(buf), fp) != NULL) {
buf[strlen(buf) - 1] = '\0';
line = buf;
while(line < endofbuf && *line == ' ')
line++;
if(*line == '#')
continue;
if(!strncmp(line, "port", 4)) {
line += 4;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
char *tmp = line;
while(tmp < endofbuf) {
if(*tmp == ' ' || *tmp == '\t' || *tmp == '\0') {
*tmp = '\0';
break;
}
tmp++;
}
strcpy(new_syscfg->port, line);
} else if(!strncmp(line, "reconnect_cnt", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->reconnect_cnt = strtol(line, NULL, 10);
} else if(!strncmp(line, "linkdown_time", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->linkdown_time = strtol(line, NULL, 10);
} else if(!strncmp(line, "repower_en", 10)) {
line += 10;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->repower_en = strtol(line, NULL, 10);
} else if(!strncmp(line, "repower_cnt", 11)) {
line += 11;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->repower_cnt = strtol(line, NULL, 10);
} else if(!strncmp(line, "powerdown_time", 14)) {
line += 14;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->powerdown_time = strtol(line, NULL, 10);
} else if(!strncmp(line, "max_repower_time", 16)) {
line += 14;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->max_repower_time = strtol(line, NULL, 10);
} else if(!strncmp(line, "reboot_en", 9)) {
line += 9;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->reboot_en = strtol(line, NULL, 10);
} else if(!strncmp(line, "reboot_period", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->reboot_period = strtol(line, NULL, 10);
} else if(!strncmp(line, "ping_delay", 10)) {
line += 10;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->ping_delay = strtol(line, NULL, 10);
} else if(!strncmp(line, "ping_faile_cnt", 14)) {
line += 14;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->ping_faile_cnt = strtol(line, NULL, 10);
} else if(!strncmp(line, "ping_faile_en", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->ping_faile_en = strtol(line, NULL, 10);
} else if(!strncmp(line, "cmp_packets_en", 14)) {
line += 14;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->cmp_packets_en = strtol(line, NULL, 10);
} else if(!strncmp(line, "cmp_packets_delay", 17)) {
line += 17;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->cmp_packets_delay = strtol(line, NULL, 10);
} else if(!strncmp(line, "rx_change_cnt", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->rx_change_cnt = strtol(line, NULL, 10);
} else if(!strncmp(line, "tx_change_cnt", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->tx_change_cnt = strtol(line, NULL, 10);
} else if(!strncmp(line, "cmd_powerdown", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
strcpy(new_syscfg->cmd_powerdown, line);
} else if(!strncmp(line, "cmd_powerup", 11)) {
line += 11;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
strcpy(new_syscfg->cmd_powerup, line);
} else if(!strncmp(line, "cmd_linkdown", 12)) {
line += 12;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
strcpy(new_syscfg->cmd_linkdown, line);
} else if(!strncmp(line, "cmd_linkup", 10)) {
line += 10;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
strcpy(new_syscfg->cmd_linkup, line);
} else if(!strncmp(line, "cmd_ping", 8)) {
line += 8;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
strcpy(new_syscfg->cmd_ping, line);
}
}
fclose(fp);
return new_syscfg;
}
static int readfile(char *path, char *content, size_t size)
{
int ret;
FILE *f;
f = fopen(path, "r");
if (f == NULL)
return -1;
ret = fread(content, 1, size, f);
fclose(f);
return ret;
}
long read_packets_from_path(char *path)
{
char buf[64];
int ret;
ret = readfile(path, buf, 64);
if( ret <= 0)
return 0;
return strtol(buf, NULL, 10);
}
int is_port_exist(char *port)
{
DIR *netdir;
struct dirent *dent;
int match = -1;
netdir = opendir("/sys/class/net");
if (netdir == NULL) {
return 0;
}
while ((dent = readdir(netdir)) != NULL) {
if (strcmp(dent->d_name, port) == 0) {
match = 1;
break;
}
}
if(match != 1) {
return 0;
}
closedir(netdir);
return 1;
}
int read_int_from_file (char *pidfile)
{
FILE *f;
int pid;
if (!(f=fopen(pidfile,"r")))
return 0;
fscanf(f,"%d", &pid);
fclose(f);
return pid;
}
int write_int_to_file (char *pidfile, int value)
{
FILE *f;
int fd;
if ( ((fd = open(pidfile, O_RDWR|O_CREAT|O_TRUNC, 0644)) == -1)
|| ((f = fdopen(fd, "r+")) == NULL) ) {
fprintf(stderr, "Can't open or create %s.\n", pidfile);
return 0;
}
if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
fclose(f);
printf("Can't lock.\n");
return 0;
}
if (!fprintf(f,"%d\n", value)) {
printf("Can't write value, %s.\n", strerror(errno));
close(fd);
return 0;
}
fflush(f);
if (flock(fd, LOCK_UN) == -1) {
printf("Can't unlock pidfile %s, %s.\n", pidfile, strerror(errno));
close(fd);
return 0;
}
close(fd);
return 1;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/file.h>
#include "confile.h"
static char port[] = "eth1";
static char cmd_powerdown[] = "/usr/bin/net_powerdown.sh";
static char cmd_powerup[] = "/usr/bin/net_powerup.sh";
static char cmd_linkdown[] = "/usr/bin/net_linkdown.sh";
static char cmd_linkup[] = "/usr/bin/net_linkup.sh";
static char cmd_ping[] = "ping www.baidu.com -c 1";
static int reboot_period = 24 * 60 * 60;
struct system_configs* get_system_configs(char *configfile)
{
struct system_configs *new_syscfg = (struct system_configs *) malloc(sizeof(struct system_configs));
strcpy(new_syscfg->port, port);
new_syscfg->linkdown_time = 15;
new_syscfg->reconnect_cnt = 3;
new_syscfg->repower_en = 0;
new_syscfg->repower_cnt = 3;
new_syscfg->powerdown_time = 10;
new_syscfg->max_repower_time = 120;
new_syscfg->reboot_en = 0;
new_syscfg->ping_faile_cnt = 2;
new_syscfg->ping_faile_en = 0;
new_syscfg->ping_delay = 0;
new_syscfg->cmp_packets_en = 0;
new_syscfg->cmp_packets_delay = 60;
new_syscfg->rx_change_cnt = 11;
new_syscfg->tx_change_cnt = 12;
strcpy(new_syscfg->cmd_powerdown, cmd_powerdown);
strcpy(new_syscfg->cmd_powerup, cmd_powerup);
strcpy(new_syscfg->cmd_linkdown, cmd_linkdown);
strcpy(new_syscfg->cmd_linkup, cmd_linkup);
strcpy(new_syscfg->cmd_ping, cmd_ping);
new_syscfg->reboot_period = reboot_period;
FILE *fp = fopen(configfile, "r");
if (fp == NULL){
printf("Failed to open file %s\n", configfile);
exit(1);
}
char buf[1024] = {0};
char *line;
char *endofbuf = buf + 1023;
while(fgets(buf, sizeof(buf), fp) != NULL) {
buf[strlen(buf) - 1] = '\0';
line = buf;
while(line < endofbuf && *line == ' ')
line++;
if(*line == '#')
continue;
if(!strncmp(line, "port", 4)) {
line += 4;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
char *tmp = line;
while(tmp < endofbuf) {
if(*tmp == ' ' || *tmp == '\t' || *tmp == '\0') {
*tmp = '\0';
break;
}
tmp++;
}
strcpy(new_syscfg->port, line);
} else if(!strncmp(line, "reconnect_cnt", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->reconnect_cnt = strtol(line, NULL, 10);
} else if(!strncmp(line, "linkdown_time", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->linkdown_time = strtol(line, NULL, 10);
} else if(!strncmp(line, "repower_en", 10)) {
line += 10;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->repower_en = strtol(line, NULL, 10);
} else if(!strncmp(line, "repower_cnt", 11)) {
line += 11;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->repower_cnt = strtol(line, NULL, 10);
} else if(!strncmp(line, "powerdown_time", 14)) {
line += 14;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->powerdown_time = strtol(line, NULL, 10);
} else if(!strncmp(line, "max_repower_time", 16)) {
line += 14;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->max_repower_time = strtol(line, NULL, 10);
} else if(!strncmp(line, "reboot_en", 9)) {
line += 9;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->reboot_en = strtol(line, NULL, 10);
} else if(!strncmp(line, "reboot_period", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->reboot_period = strtol(line, NULL, 10);
} else if(!strncmp(line, "ping_delay", 10)) {
line += 10;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->ping_delay = strtol(line, NULL, 10);
} else if(!strncmp(line, "ping_faile_cnt", 14)) {
line += 14;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->ping_faile_cnt = strtol(line, NULL, 10);
} else if(!strncmp(line, "ping_faile_en", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->ping_faile_en = strtol(line, NULL, 10);
} else if(!strncmp(line, "cmp_packets_en", 14)) {
line += 14;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->cmp_packets_en = strtol(line, NULL, 10);
} else if(!strncmp(line, "cmp_packets_delay", 17)) {
line += 17;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->cmp_packets_delay = strtol(line, NULL, 10);
} else if(!strncmp(line, "rx_change_cnt", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->rx_change_cnt = strtol(line, NULL, 10);
} else if(!strncmp(line, "tx_change_cnt", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
new_syscfg->tx_change_cnt = strtol(line, NULL, 10);
} else if(!strncmp(line, "cmd_powerdown", 13)) {
line += 13;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
strcpy(new_syscfg->cmd_powerdown, line);
} else if(!strncmp(line, "cmd_powerup", 11)) {
line += 11;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
strcpy(new_syscfg->cmd_powerup, line);
} else if(!strncmp(line, "cmd_linkdown", 12)) {
line += 12;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
strcpy(new_syscfg->cmd_linkdown, line);
} else if(!strncmp(line, "cmd_linkup", 10)) {
line += 10;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
strcpy(new_syscfg->cmd_linkup, line);
} else if(!strncmp(line, "cmd_ping", 8)) {
line += 8;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == buf || *line != '=')
continue;
line++;
while(line < endofbuf && (*line == ' ' || *line == '\t'))
line++;
if(line == endofbuf || *line == '\0')
continue;
strcpy(new_syscfg->cmd_ping, line);
}
}
fclose(fp);
return new_syscfg;
}
static int readfile(char *path, char *content, size_t size)
{
int ret;
FILE *f;
f = fopen(path, "r");
if (f == NULL)
return -1;
ret = fread(content, 1, size, f);
fclose(f);
return ret;
}
long read_packets_from_path(char *path)
{
char buf[64];
int ret;
ret = readfile(path, buf, 64);
if( ret <= 0)
return 0;
return strtol(buf, NULL, 10);
}
int is_port_exist(char *port)
{
DIR *netdir;
struct dirent *dent;
int match = -1;
netdir = opendir("/sys/class/net");
if (netdir == NULL) {
return 0;
}
while ((dent = readdir(netdir)) != NULL) {
if (strcmp(dent->d_name, port) == 0) {
match = 1;
break;
}
}
if(match != 1) {
return 0;
}
closedir(netdir);
return 1;
}
int read_int_from_file (char *pidfile)
{
FILE *f;
int pid;
if (!(f=fopen(pidfile,"r")))
return 0;
fscanf(f,"%d", &pid);
fclose(f);
return pid;
}
int write_int_to_file (char *pidfile, int value)
{
FILE *f;
int fd;
if ( ((fd = open(pidfile, O_RDWR|O_CREAT|O_TRUNC, 0644)) == -1)
|| ((f = fdopen(fd, "r+")) == NULL) ) {
fprintf(stderr, "Can't open or create %s.\n", pidfile);
return 0;
}
if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
fclose(f);
printf("Can't lock.\n");
return 0;
}
if (!fprintf(f,"%d\n", value)) {
printf("Can't write value, %s.\n", strerror(errno));
close(fd);
return 0;
}
fflush(f);
if (flock(fd, LOCK_UN) == -1) {
printf("Can't unlock pidfile %s, %s.\n", pidfile, strerror(errno));
close(fd);
return 0;
}
close(fd);
return 1;
}
//char *fgets(char *str, int n, FILE *stream)
/*功能:
*从文件指针stream中读取n-1个字符,存到以str为起始地址的空间里,直到读完一行,如果成功则返回str的指针,否则返回NULL。*/
/*参数:
* str -- 这是指向一个字符数组的指针,该数组存储了要读取的字符串。
* n -- 这是要读取的最大字符数(包括最后的空字符)。通常是使用以 str 传递的数组长度。
* stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了要从中读取字符的流。*/
/*返回值:
* 成功返回buf,失败或者读到文件结尾返回NULL
* */
/*结束的标志:
* 当读取 (n-1) 个字符时,或者读取到换行符时,或者到达文件末尾时,它会停止,具体视情况而定*/
/*出错终止的判断:
*不能直接通过fgets的返回值来判断函数是否是出错而终止的,应该借助feof函数或者ferror函数来判
flock()会依参数operation所指定的方式对参数fd所指的文件做各种锁定或解除锁定的动作。此函数只能锁定整个文件,无法锁定文件的某一区域。
表头文件 #include<sys/file.h>
定义函数 int flock(int fd,int operation);
参数 operation有下列四种情况:
LOCK_SH 建立共享锁定。多个进程可同时对同一个文件作共享锁定。
LOCK_EX 建立互斥锁定。一个文件同时只有一个互斥锁定。
LOCK_UN 解除文件锁定状态。
LOCK_NB 无法建立锁定时,此操作可不被阻断,马上返回进程。通常与LOCK_SH或LOCK_EX 做OR(|)组合。
单一文件无法同时建立共享锁定和互斥锁定,而当使用dup()或fork()时文件描述词不会继承此种锁定。
返回值 返回0表示成功,若有错误则返回-1,错误代码存于errno。