Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- COMPILATION COMMAND:
- arm-linux-gnueabi-gcc -static -o hw_daemon_mssiraj hw_daemon_mssiraj.c
- */
- #include <stdio.h>
- #include <stdarg.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/times.h>
- #include <sys/stat.h>
- #include <sys/param.h>
- #include <unistd.h>
- #include <syslog.h>
- #include <errno.h>
- #include <signal.h>
- #include <inttypes.h>
- #include <string.h>
- #include <time.h>
- #define OK 0
- #define true 1
- #define ERR_WTF 1
- #define ERR_FORK 1
- #define ERR_SETSID 1
- #define ERR_CHDIR 1
- #define ERR_TIME 1
- #define ERROR_FORMAT "ERROR MESSAGE: %s"
- #define DAEMON_NAME "sample_daemon_mssiraj"
- static void _signal_handler(const int signal) {
- switch(signal) {
- case SIGHUP:
- break;
- case SIGTERM:
- syslog(LOG_INFO, "received SIGTERM, exiting.");
- closelog();
- exit(OK);
- break;
- default:
- syslog(LOG_INFO, "received unhandled signal");
- }
- }
- static int _do_work(void) {
- while(1) {
- time_t present_time = time(NULL);
- struct tm *ptm = localtime(&present_time);
- if (ptm == NULL) {
- return ERR_TIME;
- }
- syslog(LOG_INFO, "Present Time: %d:%d:%d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
- sleep(1);
- }
- }
- int main(void) {
- openlog(DAEMON_NAME, LOG_PID | LOG_NDELAY | LOG_NOWAIT, LOG_DAEMON);
- syslog(LOG_INFO, "Starting hw_daemon_mssiraj");
- pid_t pid = fork();
- if (pid < 0) {
- syslog(LOG_ERR, ERROR_FORMAT, strerror(errno));
- return ERR_FORK;
- }
- if (pid > 0) {
- return OK;
- }
- if (setsid() < -1){
- syslog(LOG_ERR, ERROR_FORMAT, strerror(errno));
- return ERR_SETSID;
- }
- close(STDIN_FILENO);
- close(STDOUT_FILENO);
- close(STDERR_FILENO);
- umask(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
- if (chdir("/") < 0) {
- syslog(LOG_ERR, ERROR_FORMAT, strerror(errno));
- return ERR_CHDIR;
- }
- signal(SIGTERM, _signal_handler);
- signal(SIGHUP, _signal_handler);
- _do_work();
- return ERR_WTF;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement