2 Problems: Segfault on ctrl+c and syslog() prob


 
Thread Tools Search this Thread
Top Forums Programming 2 Problems: Segfault on ctrl+c and syslog() prob
# 1  
Old 06-12-2009
2 Problems: Segfault on ctrl+c and syslog() prob

1. Even if i have the handles for ctrl+c it gives off a segfault
2. syslog doesn't log LOG_ERR event with log masked specified or non specified, it logs LOG_WARNING however...

Code:
#include <sys/types.h>  /* include this before any other sys headers */
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include <syslog.h>
#include <errno.h>
#include <sys/wait.h>   /* header for waitpid() and various macros */
#include <signal.h>     /* header for signal functions */
#include <stdio.h>      /* header for fprintf() */
#include <unistd.h>     /* header for fork() */
#include <stdlib.h>   
void  ChildProcess(void);                /* child process prototype  */
void  ParentProcess(void);               /* parent process prototype */
#define DAEMON_NAME "mplayerdaemon"
#define PID_FILE "/var/run/mplayerdaemon.pid"

/***********************************************************
  Function: signal_handler
  Description: Handles signals the daemon might receive. 
               Gives the daemon enough time to properly shutdown

  Params: sig : The signal(int) received
 *********************************************************************/
void signal_handler(int sig) {
 
    switch(sig) {
        case SIGHUP:
            syslog(LOG_WARNING, "Received SIGHUP signal.");
            break;
        case SIGTERM:
            syslog(LOG_WARNING, "Received SIGTERM signal.");
            break;
        default:
            syslog(LOG_WARNING, "Unhandled signal (%d) %s", strsignal(sig));
            break;
    }
}

int  main(int argc, char *argv[])
{
#if defined(DEBUG)
    int daemonize=0;
#else
    int daemonize=1;
#endif
    
    // Setup signal handling before we start
    signal(SIGABRT, signal_handler);
    signal(SIGHUP, signal_handler);
    signal(SIGTERM, signal_handler);
    signal(SIGINT, signal_handler);
    signal(SIGQUIT, signal_handler);

    syslog(LOG_INFO, "[INFO] %s daemon starting up",DAEMON_NAME);

#if defined(DEBUG)
        setlogmask(LOG_UPTO(LOG_DEBUG));
        openlog(DAEMON_NAME,LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
#else
        setlogmask(LOG_UPTO(LOG_INFO));
        openlog(DAEMON_NAME, LOG_CONS, LOG_USER);
#endif

    /* Our process ID and Session ID */
    pid_t pid, sid;

    syslog(LOG_WARNING,"[ERROR] testing error");
    daemonize=0; //for debug purposes only
    if (daemonize) {
        syslog(LOG_INFO, "[INFO] starting the daemon process");


        /* Fork off the parent */
        pid = fork();
        if (pid<0) {
            exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then 
          we can tell the parent to exit*/
        if (pid>0){
            exit(EXIT_SUCCESS);
        }
        /* Child will always have a 0 if successfully created*/

        /* Change umask so that the program will have access to all child files */
        umask(0);

        /* Create a new SID for the child process */
        sid = setsid();
        if (sid<0){
            syslog(LOG_WARNING,"[ERROR] Error while getting new session id, error code : -1");
            exit(EXIT_FAILURE);
        }

        /* Change the current working directory */
        /*
         if ((chdir("/")) < 0){
            syslog(LOG_ERR,"Error while changing directory error code : -1");
             exit(EXIT_FAILURE);
         }

         */

        /* Close out the standard file descriptors 
           Because daemon won't be accepting input from console */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
        
    }

    while(1){
          int rc=system("ps -C mplayer -opid=");
                        
          rc = WEXITSTATUS(rc); /* Check if mplayer is running */
          printf("I was here pid: %d\n",rc);

        /*if mplayer quits unexpectedly */
        if(rc > 0 ){
           
                 launchmplayer();
                 sleep(2);
              }

     }

    /* if loop breaks daemon exits*/
    syslog(LOG_INFO, "%s daemon exiting",DAEMON_NAME);

    exit(0);
}

void launchmplayer(void)
{
     system("mplayer ~/test.avi");
     return;
}

# 2  
Old 06-16-2009
Hi

What OS/architecture are you using? I tried using Linux and although the code doesn't compile at first, it works fine and I did not receive a sigsev.

I remember once I had the same problem but it was in an architecture different to i386.
# 3  
Old 06-16-2009
Default kernel of ubuntu 9.04
I386 Archetechure
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automation of keyboard inputs..like Ctrl+d and Ctrl+a

Hi..! I'm stuck with my automation of starting a process and keeping it running even after the current ssh session has exited.. So i'm trying to use command 'screen'. which is doing exactly what i wanted, But the problem is automation of the same. i will have to press Ctrl+a and Ctrl+d for... (2 Replies)
Discussion started by: chandana hs
2 Replies

2. UNIX for Dummies Questions & Answers

Ctrl-V + Ctrl-J for newline character does not work inside vi editor

Hi friends, I am trying to add a newline char ('\n') between the query and the commit statement in the following shell script. #! /bin/sh echo "select * from tab; commit;" > data.sql I have tried typing in "Ctrl-V + Ctrl-J" combination which has inserted ^@ (NUL) character but the commit... (1 Reply)
Discussion started by: royalibrahim
1 Replies

3. Shell Programming and Scripting

How to handle CTRL+Z or CTRL+C in shells script?

Hi, while executing shell script, in the middle of the process, if we kill the shell script( ctrl+z or ctrl+c), script will be killed and the files which using for the script will be in the folder. How to handle those scenarios. Is there any possibilities, if user breaks the script, I need to... (3 Replies)
Discussion started by: ckchelladurai
3 Replies

4. Programming

Is Drive Valid Segfault

I have a program that allows users to specify the debug log file location and name. I have tried using the access() and stat() but they both segfault if the drive say (d:\) is invalid. Both seem to be fine if the drive exists. Could someone please point me in the direction to a function that... (1 Reply)
Discussion started by: robfwauk
1 Replies

5. Shell Programming and Scripting

Ctrl-C or Ctrl-Z causing exit the session

H! I have written script where it need to invoke the perl script in background, then write the pid in temp file then bring back the job to foreground. whenever the Ctrl-C or Ctrl-Z is pressed in the script has to exit and prompt should be dispalyed. but this script causing exit from shell session... (2 Replies)
Discussion started by: jramesh1
2 Replies

6. Programming

id3lib SEGFAULT

Hello everyone, I'm writing a program using the id3lib unfortunately I've encountered with memory issue that cause segmentation fault. I tried to rerun and analyze the program with valgrind but it doesn't point me anywhere. I really stuck on this one. Valgrind output: ==14716== Invalid read of... (2 Replies)
Discussion started by: errb
2 Replies

7. UNIX for Dummies Questions & Answers

[Linux] How Do I Run Until Segfault

Hello, sorry if this has been posted before but i was wondering if there is a way to run a program until a segmentation fault is found. Currently i'm using a simple shell script which runs my program 100 times, sleeps 1 second because srand(time(0)) is dependent on seconds. Is there a possible... (1 Reply)
Discussion started by: aslambilal
1 Replies

8. Shell Programming and Scripting

Catching ctrl-C or ctrl-D

Hi there, I'm using HP-UX 11 machine. I am running a script, thats gonna take a long time to execute. When I press ctrl-c to come out of my script, I have to catch that signal(ctrl-c) and display that ctrl-c had been pressed. How can I do it. Thanks in advance (2 Replies)
Discussion started by: sendhilmani123
2 Replies

9. AIX

Disable ctrl-c,ctrl-d,ctrl-d in ksh script

I wrote a ksh script for Helpdesk. I need to know how to disable ctrl-c,ctrl-z,ctrl-d..... so that helpdesk would not be able to get to system prompt :confused: (6 Replies)
Discussion started by: wtofu
6 Replies

10. UNIX for Dummies Questions & Answers

problems with ctrl-z, to switch foreground, background

my shell is /sbin/sh. i added stty susp '^Z' with the intention of being able to switch between foreground and background. but the result was strange. i had 2 servers. one is sun the os is 8 and the other is hpux v11. both of them had the same shell. but on hpux, it works perfectly fine while... (9 Replies)
Discussion started by: yls177
9 Replies
Login or Register to Ask a Question