diff --git a/mbpfan.init b/mbpfan.init
index 6fd72db..5fbe7d0 100755
--- a/mbpfan.init
+++ b/mbpfan.init
@@ -77,23 +77,23 @@ case "$1" in
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
restart|force-reload)
- log_daemon_msg "Restarting $DESC" "$NAME"
- do_stop
- case "$?" in
- 0|1)
- do_start
- case "$?" in
- 0) log_end_msg 0 ;;
- 1) log_end_msg 1 ;; # Old process is still running
- *) log_end_msg 1 ;; # Failed to start
- esac
- ;;
- *)
- # Failed to stop
- log_end_msg 1
- ;;
- esac
- ;;
+ log_daemon_msg "Restarting $DESC" "$NAME"
+ do_stop
+ case "$?" in
+ 0|1)
+ do_start
+ case "$?" in
+ 0) log_end_msg 0 ;;
+ 1) log_end_msg 1 ;; # Old process is still running
+ *) log_end_msg 1 ;; # Failed to start
+ esac
+ ;;
+ *)
+ # Failed to stop
+ log_end_msg 1
+ ;;
+ esac
+ ;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
diff --git a/src/daemon.c b/src/daemon.c
index 0dc4e11..a9b7fa1 100644
--- a/src/daemon.c
+++ b/src/daemon.c
@@ -1,7 +1,20 @@
/**
- * 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/stat.h>
#include <stdio.h>
@@ -16,8 +29,33 @@
#include "mbpfan.h"
#include "global.h"
-#define DAEMON_NAME "mbpfan"
-#define PID_FILE "/var/run/mbpfan.pid"
+int write_pid(int 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)
@@ -27,15 +65,18 @@ void signal_handler(int signal)
case SIGHUP:
//TODO: restart myself
syslog(LOG_WARNING, "Received SIGHUP signal.");
+ delete_pid();
exit(0);
break;
case SIGTERM:
syslog(LOG_WARNING, "Received SIGTERM signal.");
+ delete_pid();
//TODO: free resources
exit(0);
break;
case SIGINT:
syslog(LOG_WARNING, "Received SIGINT signal.");
+ delete_pid();
//TODO: free resources
exit(0);
default:
@@ -44,8 +85,7 @@ void signal_handler(int signal)
}
}
-
-void go_daemon(void (*function)())
+void go_daemon(void (*mbpfan)())
{
// Setup signal handling before we start
@@ -53,36 +93,37 @@ void go_daemon(void (*function)())
signal(SIGTERM, 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)
if(verbose) {
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 {
setlogmask(LOG_UPTO(LOG_INFO));
- openlog(DAEMON_NAME, LOG_CONS, LOG_USER);
+ openlog(program_name, LOG_CONS, LOG_USER);
}
- pid_t pid;
- pid_t sid;
+ pid_t pid_slave;
+ pid_t sid_slave;
if (daemonize) {
- pid = fork();
- if (pid < 0) {
+ pid_slave = fork();
+ if (pid_slave < 0) {
exit(EXIT_FAILURE);
}
- if (pid > 0) {
- exit(EXIT_SUCCESS);
+ if (pid_slave > 0) {
+ // kill the father
+ exit(EXIT_SUCCESS);
}
- umask(0);
+ umask(0022);
- // new SID for the child process
- sid = setsid();
- if (sid < 0) {
+ // new sid_slave for the child process
+ sid_slave = setsid();
+ if (sid_slave < 0) {
exit(EXIT_FAILURE);
}
@@ -90,17 +131,48 @@ void go_daemon(void (*function)())
exit(EXIT_FAILURE);
}
+
+
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_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)
- syslog(LOG_INFO, "%s daemon exiting", DAEMON_NAME);
+ if(daemonize){
+ syslog(LOG_INFO, "%s daemon exiting", program_name);
+ }
return;
}
diff --git a/src/daemon.h b/src/daemon.h
index 2e2d30d..e46bff1 100644
--- a/src/daemon.h
+++ b/src/daemon.h
@@ -1,8 +1,49 @@
+/**
+ * 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 :-)
*/
void signal_handler(int signal);
+
/**
* Daemonizes
*/
diff --git a/src/global.h b/src/global.h
index 910ed89..e1dd842 100644
--- a/src/global.h
+++ b/src/global.h
@@ -1,2 +1,5 @@
extern int daemonize;
-extern int verbose;
\ No newline at end of file
+extern int verbose;
+
+extern const char* program_name;
+extern const char* program_pid;
\ No newline at end of file
diff --git a/src/main.c b/src/main.c
index 693efcd..ba2880e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,3 +1,18 @@
+/**
+ * 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 <stdlib.h>
#include <unistd.h>
@@ -8,6 +23,9 @@
int daemonize = 1;
int verbose = 0;
+const char *program_name = "mbpfan";
+const char *program_pid = "/var/run/mbpfan.pid";
+
void print_usage(int argc, char *argv[])
{
if (argc >=1) {
@@ -44,7 +62,7 @@ int main(int argc, char *argv[])
}
// pointer to mbpfan() function in mbpfan.c
- void (*function)() = mbpfan;
- go_daemon(function);
+ void (*mbpfan)() = mbpfan;
+ go_daemon(mbpfan);
exit(0);
}
\ No newline at end of file
diff --git a/src/mbpfan.c b/src/mbpfan.c
index 419cc1c..91f39a9 100644
--- a/src/mbpfan.c
+++ b/src/mbpfan.c
@@ -18,7 +18,7 @@
*
* Notes:
* 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 root use
*
diff --git a/src/mbpfan.h b/src/mbpfan.h
index 20bcfe2..80bf96c 100644
--- a/src/mbpfan.h
+++ b/src/mbpfan.h
@@ -1,3 +1,19 @@
+/**
+ * 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
*/
extern int min_fan_speed;
Fixes #9
7e65d1e6582b2051ac27c91f96b3b478f4997749
Daniel Graziotin <dgraziotin@task3.cc> · 2012-06-15T21:07 ·
browse files at this commit
parents:
cd3c15b