Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 134 additions & 138 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*
*/


#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/stat.h>
Expand All @@ -38,52 +37,47 @@
int daemonize = 1;
int verbose = 0;

int write_pid(int pid)
{
FILE *file = NULL;
file = fopen(PROGRAM_PID, "w");
int write_pid(int pid) {
FILE *file = NULL;
file = fopen(PROGRAM_PID, "w");

if(file != NULL) {
fprintf(file, "%d", pid);
fclose(file);
return 1;
if (file != NULL) {
fprintf(file, "%d", pid);
fclose(file);
return 1;

} else {
return 0;
}
} else {
return 0;
}
}

int read_pid()
{
FILE *file = NULL;
int pid = -1;
file = fopen(PROGRAM_PID, "r");

if(file != NULL) {
fscanf(file, "%d", &pid);
fclose(file);
if (kill(pid, 0) == -1 && errno == ESRCH)
{ /* a process with such a pid does not exist, remove the pid file */
if (remove(PROGRAM_PID) == 0) {
return -1;
}
}
return pid;
}

return -1;
int read_pid() {
FILE *file = NULL;
int pid = -1;
file = fopen(PROGRAM_PID, "r");

if (file != NULL) {
fscanf(file, "%d", &pid);
fclose(file);
if (kill(pid, 0) == -1 && errno == ESRCH) { /* a process with such a pid does not exist, remove the pid file */
if (remove(PROGRAM_PID) == 0) {
return -1;
}
}
return pid;
}

return -1;
}

int delete_pid()
{
return remove(PROGRAM_PID);
int delete_pid() {
return remove(PROGRAM_PID);
}

static void cleanup_and_exit(int exit_code)
{
static void cleanup_and_exit(int exit_code) {
delete_pid();
set_fans_auto(fans);

struct s_fans *next_fan;
while (fans != NULL) {
next_fan = fans->next;
Expand Down Expand Up @@ -111,128 +105,130 @@ static void cleanup_and_exit(int exit_code)
exit(exit_code);
}

void signal_handler(int signal)
{

switch(signal) {
case SIGHUP:
syslog(LOG_WARNING, "Received SIGHUP signal.");
retrieve_settings(NULL, fans);
break;

case SIGTERM:
syslog(LOG_WARNING, "Received SIGTERM signal.");
cleanup_and_exit(EXIT_SUCCESS);
break;

case SIGQUIT:
syslog(LOG_WARNING, "Received SIGQUIT signal.");
cleanup_and_exit(EXIT_SUCCESS);
break;

case SIGINT:
syslog(LOG_WARNING, "Received SIGINT signal.");
cleanup_and_exit(EXIT_SUCCESS);
break;

default:
syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal));
break;
}
void signal_handler(int signal) {

switch (signal) {
case SIGHUP:
syslog(LOG_WARNING, "Received SIGHUP signal.");
retrieve_settings(NULL, fans);
break;

case SIGTERM:
syslog(LOG_WARNING, "Received SIGTERM signal.");
cleanup_and_exit(EXIT_SUCCESS);
break;

case SIGQUIT:
syslog(LOG_WARNING, "Received SIGQUIT signal.");
cleanup_and_exit(EXIT_SUCCESS);
break;

case SIGINT:
syslog(LOG_WARNING, "Received SIGINT signal.");
cleanup_and_exit(EXIT_SUCCESS);
break;

default:
syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal,
strsignal(signal));
break;
}
}

void go_daemon(void (*fan_control)())
{

// Setup signal handling before we start
signal(SIGHUP, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGINT, signal_handler);

// Setup syslog logging - see SETLOGMASK(3)
if(verbose) {
setlogmask(LOG_UPTO(LOG_DEBUG));
openlog(PROGRAM_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);

} else {
setlogmask(LOG_UPTO(LOG_INFO));
openlog(PROGRAM_NAME, LOG_CONS, LOG_USER);
}

mbp_log(LOG_INFO, "%s %s starting up", PROGRAM_NAME, PROGRAM_VERSION);

// configure timer slack
int err = prctl(PR_SET_TIMERSLACK, 1000 * 1000 * 1000, 0, 0, 0);
if (err == -1) {
perror("prctl");
}
void go_daemon(void (*fan_control)()) {

pid_t pid_slave;
pid_t sid_slave;
// Setup signal handling before we start
signal(SIGHUP, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGINT, signal_handler);

if (daemonize) {
// Setup syslog logging - see SETLOGMASK(3)
if (verbose) {
setlogmask(LOG_UPTO(LOG_DEBUG));
openlog(PROGRAM_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID,
LOG_USER);

pid_slave = fork();
} else {
setlogmask(LOG_UPTO(LOG_INFO));
openlog(PROGRAM_NAME, LOG_CONS, LOG_USER);
}

if (pid_slave < 0) {
exit(EXIT_FAILURE);
}
mbp_log(LOG_INFO, "%s %s starting up", PROGRAM_NAME, PROGRAM_VERSION);

if (pid_slave > 0) {
signal(SIGCHLD, SIG_IGN);
// kill the father
exit(EXIT_SUCCESS);
}
// configure timer slack
int err = prctl(PR_SET_TIMERSLACK, 1000 * 1000 * 1000, 0, 0, 0);
if (err == -1) {
perror("prctl");
}

umask(0022);
pid_t pid_slave;
pid_t sid_slave;

// new sid_slave for the child process
sid_slave = setsid();
if (daemonize) {

if (sid_slave < 0) {
exit(EXIT_FAILURE);
}
pid_slave = fork();

if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
if (pid_slave < 0) {
exit(EXIT_FAILURE);
}

if (pid_slave > 0) {
signal(SIGCHLD, SIG_IGN);
// kill the father
exit(EXIT_SUCCESS);
}

umask(0022);

/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
// new sid_slave for the child process
sid_slave = setsid();

if (sid_slave < 0) {
exit(EXIT_FAILURE);
}

int current_pid = getpid();
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}

if (read_pid() == -1) {
if (verbose) {
mbp_log(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}

if (write_pid(current_pid) == 0) {
mbp_log(LOG_ERR, "Can not create a .pid file at: %s. Aborting", PROGRAM_PID);
exit(EXIT_FAILURE);
int current_pid = getpid();

} else {
if (verbose) {
mbp_log(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
}
}
if (read_pid() == -1) {
if (verbose) {
mbp_log(LOG_INFO, "Writing a new .pid file with value %d at: %s",
current_pid, PROGRAM_PID);
}

} else {
mbp_log(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", PROGRAM_PID);
exit(EXIT_FAILURE);
}
if (write_pid(current_pid) == 0) {
mbp_log(LOG_ERR, "Can not create a .pid file at: %s. Aborting",
PROGRAM_PID);
exit(EXIT_FAILURE);

} else {
if (verbose) {
mbp_log(LOG_INFO,
"Successfully written a new .pid file with value %d at: %s",
current_pid, PROGRAM_PID);
}
}

} else {
mbp_log(LOG_ERR,
"A previously created .pid file exists at: %s. Aborting",
PROGRAM_PID);
exit(EXIT_FAILURE);
}

fan_control();
fan_control();

if(daemonize) {
syslog(LOG_INFO, "%s daemon exiting", PROGRAM_NAME);
}
if (daemonize) {
syslog(LOG_INFO, "%s daemon exiting", PROGRAM_NAME);
}
}
1 change: 0 additions & 1 deletion src/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ void signal_handler(int signal);
*/
void go_daemon(void (*function)());


#endif
36 changes: 18 additions & 18 deletions src/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ extern int daemonize;
extern int verbose;

struct s_sensors {
FILE* file;
char* path;
unsigned int temperature;
struct s_sensors *next;
FILE *file;
char *path;
unsigned int temperature;
struct s_sensors *next;
};

struct s_fans {
FILE* file;
char* path; // TODO: unused
char* label;
char* fan_output_path;
char* fan_manual_path;
int step_up;
int step_down;
int fan_id;
int old_speed;
int fan_max_speed;
int fan_min_speed;
struct s_fans *next;
FILE *file;
char *path; // TODO: unused
char *label;
char *fan_output_path;
char *fan_manual_path;
int step_up;
int step_down;
int fan_id;
int old_speed;
int fan_max_speed;
int fan_min_speed;
struct s_fans *next;
};

typedef struct s_sensors t_sensors;
typedef struct s_fans t_fans;

extern t_sensors* sensors;
extern t_fans* fans;
extern t_sensors *sensors;
extern t_fans *fans;

#endif
Loading