Commit 7e65d1e6 authored by Daniel Graziotin's avatar Daniel Graziotin

Fixes #9

parent cd3c15ba
...@@ -77,23 +77,23 @@ case "$1" in ...@@ -77,23 +77,23 @@ case "$1" in
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;; ;;
restart|force-reload) restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME" log_daemon_msg "Restarting $DESC" "$NAME"
do_stop do_stop
case "$?" in case "$?" in
0|1) 0|1)
do_start do_start
case "$?" in case "$?" in
0) log_end_msg 0 ;; 0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running 1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start *) log_end_msg 1 ;; # Failed to start
esac esac
;; ;;
*) *)
# Failed to stop # Failed to stop
log_end_msg 1 log_end_msg 1
;; ;;
esac esac
;; ;;
*) *)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3 exit 3
......
/** /**
* Credits: http://peterlombardo.wikidot.com/linux-daemon-in-c * Copyright (C) 2012 Peter Lombardo <http://peterlombardo.wikidot.com/linux-daemon-in-c>
* Modifications (2012) by Daniel Graziotin <dgraziotin@task3.cc>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
*/ */
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdio.h> #include <stdio.h>
...@@ -16,8 +29,33 @@ ...@@ -16,8 +29,33 @@
#include "mbpfan.h" #include "mbpfan.h"
#include "global.h" #include "global.h"
#define DAEMON_NAME "mbpfan" int write_pid(int pid){
#define PID_FILE "/var/run/mbpfan.pid" FILE *file = NULL;
file = fopen(program_pid, "w");
if(file != NULL) {
fprintf(file, "%d", pid);
fclose(file);
return 1;
}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);
return pid;
}
return -1;
}
int delete_pid(){
return remove(program_pid);
}
void signal_handler(int signal) void signal_handler(int signal)
...@@ -27,15 +65,18 @@ void signal_handler(int signal) ...@@ -27,15 +65,18 @@ void signal_handler(int signal)
case SIGHUP: case SIGHUP:
//TODO: restart myself //TODO: restart myself
syslog(LOG_WARNING, "Received SIGHUP signal."); syslog(LOG_WARNING, "Received SIGHUP signal.");
delete_pid();
exit(0); exit(0);
break; break;
case SIGTERM: case SIGTERM:
syslog(LOG_WARNING, "Received SIGTERM signal."); syslog(LOG_WARNING, "Received SIGTERM signal.");
delete_pid();
//TODO: free resources //TODO: free resources
exit(0); exit(0);
break; break;
case SIGINT: case SIGINT:
syslog(LOG_WARNING, "Received SIGINT signal."); syslog(LOG_WARNING, "Received SIGINT signal.");
delete_pid();
//TODO: free resources //TODO: free resources
exit(0); exit(0);
default: default:
...@@ -44,8 +85,7 @@ void signal_handler(int signal) ...@@ -44,8 +85,7 @@ void signal_handler(int signal)
} }
} }
void go_daemon(void (*mbpfan)())
void go_daemon(void (*function)())
{ {
// Setup signal handling before we start // Setup signal handling before we start
...@@ -53,36 +93,37 @@ void go_daemon(void (*function)()) ...@@ -53,36 +93,37 @@ void go_daemon(void (*function)())
signal(SIGTERM, signal_handler); signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
syslog(LOG_INFO, "%s starting up", DAEMON_NAME); syslog(LOG_INFO, "%s starting up", program_name);
// Setup syslog logging - see SETLOGMASK(3) // Setup syslog logging - see SETLOGMASK(3)
if(verbose) { if(verbose) {
setlogmask(LOG_UPTO(LOG_DEBUG)); setlogmask(LOG_UPTO(LOG_DEBUG));
openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); openlog(program_name, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
} else { } else {
setlogmask(LOG_UPTO(LOG_INFO)); setlogmask(LOG_UPTO(LOG_INFO));
openlog(DAEMON_NAME, LOG_CONS, LOG_USER); openlog(program_name, LOG_CONS, LOG_USER);
} }
pid_t pid; pid_t pid_slave;
pid_t sid; pid_t sid_slave;
if (daemonize) { if (daemonize) {
pid = fork(); pid_slave = fork();
if (pid < 0) { if (pid_slave < 0) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (pid > 0) { if (pid_slave > 0) {
exit(EXIT_SUCCESS); // kill the father
exit(EXIT_SUCCESS);
} }
umask(0); umask(0022);
// new SID for the child process // new sid_slave for the child process
sid = setsid(); sid_slave = setsid();
if (sid < 0) { if (sid_slave < 0) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
...@@ -90,17 +131,48 @@ void go_daemon(void (*function)()) ...@@ -90,17 +131,48 @@ void go_daemon(void (*function)())
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* Close out the standard file descriptors */ /* Close out the standard file descriptors */
close(STDIN_FILENO); close(STDIN_FILENO);
close(STDOUT_FILENO); close(STDOUT_FILENO);
close(STDERR_FILENO); close(STDERR_FILENO);
} }
int current_pid = getpid();
if (read_pid() == -1){
if (verbose){
printf("Writing a new .pid file with value %d at: %s", current_pid, program_pid);
syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, program_pid);
}
if (write_pid(current_pid) == 0){
syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", program_pid);
if (verbose){
printf("ERROR: Can not create a .pid file at: %s. Aborting", program_pid);
}
exit(EXIT_FAILURE);
}else{
if (verbose){
printf("Successfully written a new .pid file with value %d at: %s", current_pid, program_pid);
syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, program_pid);
}
}
}else{
syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", program_pid);
if (verbose){
printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", program_pid);
}
exit(EXIT_FAILURE);
}
function(); mbpfan();
if(daemonize) if(daemonize){
syslog(LOG_INFO, "%s daemon exiting", DAEMON_NAME); syslog(LOG_INFO, "%s daemon exiting", program_name);
}
return; return;
} }
/**
* Copyright (C) (2012) Daniel Graziotin <dgraziotin@task3.cc>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
*/
/**
* Write the PID of the forked daemon to the
* .pid file defined in char *program_pid
* Return TRUE on success
* Return FALSE otherwise
*/
int write_pid(int pid);
/**
* Read PID of the forked daemon from the
* .pid file defined in char *program_pid
* Return the pid on success
* Return -1 otherwise
*/
int read_pid();
/**
* Deletes the .pid file defined in
* char *program_pid
* Return TRUE on success
* Return FALSE otherwise
*/
int delete_pid();
/** /**
* ...handles signals :-) * ...handles signals :-)
*/ */
void signal_handler(int signal); void signal_handler(int signal);
/** /**
* Daemonizes * Daemonizes
*/ */
......
extern int daemonize; extern int daemonize;
extern int verbose; extern int verbose;
\ No newline at end of file
extern const char* program_name;
extern const char* program_pid;
\ No newline at end of file
/**
* Copyright (C) (2012) Daniel Graziotin <dgraziotin@task3.cc>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
...@@ -8,6 +23,9 @@ ...@@ -8,6 +23,9 @@
int daemonize = 1; int daemonize = 1;
int verbose = 0; int verbose = 0;
const char *program_name = "mbpfan";
const char *program_pid = "/var/run/mbpfan.pid";
void print_usage(int argc, char *argv[]) void print_usage(int argc, char *argv[])
{ {
if (argc >=1) { if (argc >=1) {
...@@ -44,7 +62,7 @@ int main(int argc, char *argv[]) ...@@ -44,7 +62,7 @@ int main(int argc, char *argv[])
} }
// pointer to mbpfan() function in mbpfan.c // pointer to mbpfan() function in mbpfan.c
void (*function)() = mbpfan; void (*mbpfan)() = mbpfan;
go_daemon(function); go_daemon(mbpfan);
exit(0); exit(0);
} }
\ No newline at end of file
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
* *
* Notes: * Notes:
* Assumes any number of processors and fans (max. 10) * Assumes any number of processors and fans (max. 10)
* It Uses only the temperatures from the processors as input. * It uses only the temperatures from the processors as input.
* Requires coretemp and applesmc kernel modules to be loaded. * Requires coretemp and applesmc kernel modules to be loaded.
* Requires root use * Requires root use
* *
......
/**
* Copyright (C) 2010 Allan McRae <allan@archlinux.org>
* Modifications (2012) by Daniel Graziotin <dgraziotin@task3.cc>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
*/
/** Basic fan speed parameters /** Basic fan speed parameters
*/ */
extern int min_fan_speed; extern int min_fan_speed;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment