Вот пытаюсь понять почему не работает такой код:
Код: Выделить всё
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <signal.h>
#include <sysexits.h>
#include <mysql.h>
#define DAEMON_NAME "daemon_test2"
int ipfw2_table_no = -1;
static int exit_requested = 0;
const char *pid_file = NULL;
int lfp;
static void handle_sigs(int __unused sig) {
switch(sig) {
case SIGHUP:
syslog(LOG_INFO,"hangup signal catched");
break;
case SIGTERM:
syslog(LOG_INFO,"terminate signal catched");
exit_requested = 1;
break;
}
}
static void usage(void) {
fprintf(stderr,
"\n"
"Usage: %s -t table [-s seconds] [-p pid_file] [-a hostname] -n db_name -l db_login [-w db_passwd]\n"
" -t set the IPFW table number\n"
" -s set the sleep time between cycles, default: 5sec\n"
" -p PID file path, default: none\n"
" -f run the daemon in the foreground (do not daemonize)\n"
" -a MySQL database host\n"
" -n MySQL database name\n"
" -l MySQL database login\n"
" -w MySQL database password\n"
" -h print this message.\n"
"\n", DAEMON_NAME);
exit(EXIT_SUCCESS);
}
void stop_daemon(void) {
syslog(LOG_INFO,"start stop_daemon()");
if(pid_file) {
close(lfp);
unlink(pid_file);
}
}
int run_in_daemon(void) {
char *str;
syslog(LOG_INFO,"start run_in_daemon()");
if(daemon(0,0)) {
stop_daemon();
errx(EX_OSERR, "Failed to become a daemon");
}
if(pid_file) {
syslog(LOG_INFO,"open pid_file");
lfp=open(pid_file,O_RDWR|O_CREAT|O_EXLOCK|O_TRUNC|O_NONBLOCK,0640);
syslog(LOG_INFO,"check pid_file");
if (lfp<0) {
syslog(LOG_INFO,"error1 pid_file");
errx(EX_OSERR, "Failed! Can't open pid file");
}
if (lockf(lfp,F_TLOCK,0)<0) {
syslog(LOG_INFO,"error2 pid_file");
errx(EX_OSERR, "Failed! Can't lock pid file");
}
sprintf(str,"%d",getpid());
write(lfp,str,strlen(str));
}
signal(SIGCHLD,SIG_IGN);
signal(SIGTSTP,SIG_IGN);
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGHUP,handle_sigs);
signal(SIGTERM,handle_sigs);
syslog(LOG_INFO,"Run id daemon mode\n");
}
int main(int argc, char **argv) {
int ch;
int sleep_time = 5;
int daemonize = 1;
char *db_host = "localhost";
char *db_name = "billing";
char *db_login = "root";
char *db_pass = "";
openlog(DAEMON_NAME, LOG_PID | LOG_NDELAY, LOG_AUTH);
syslog(LOG_INFO, "Starting.....");
if (argc <= 2) {
usage();
exit(EXIT_SUCCESS);
}
while ((ch = getopt(argc, argv, "t:s:a:n:l:w:hfp:")) != -1) {
switch (ch) {
case 't':
ipfw2_table_no = atoi(optarg);
if (ipfw2_table_no > 127 || ipfw2_table_no < 0)
errx(EX_USAGE, "table number can be from 0 to 127");
break;
case 'a':
db_host=optarg;
fprintf(stderr,"db_host = %s\n",db_host);
break;
case 'n':
db_name=optarg;
fprintf(stderr,"db_name = %s\n",db_name);
break;
case 'l':
db_login=optarg;
fprintf(stderr,"db_login = %s\n",db_login);
break;
case 'w':
db_pass=optarg;
fprintf(stderr,"db_pass = %s\n",db_pass);
break;
case 's':
sleep_time = atoi(optarg);
fprintf(stderr,"sleep_time = %d\n",sleep_time);
break;
case 'f':
daemonize = 0;
fprintf(stderr,"daemonize = %d\n",daemonize);
break;
case 'p':
pid_file = optarg;
fprintf(stderr,"pid_file = %s\n",pid_file);
break;
case 'h':
default:
usage();
}
}
if(daemonize) {
run_in_daemon();
}
/* The Big Loop */
for(;;) {
if(exit_requested) {
break;
}
if(daemonize) {
syslog(LOG_INFO,"Do some task here ...\n");
} else {
fprintf(stderr,"Do some task here ...\n");
}
sleep(sleep_time);
}
stop_daemon();
free(argv);
closelog();
exit(EXIT_SUCCESS);
}