c - Why do I get a seg fault on Ubuntu but not mac?


Question: 

I have a program that checks the modification time of a file and executes the file if it has changed. Currently it works if I run it on my mac, but it seg faults if I run it on ubuntu. Please help me.

note: this is in c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define CONTERROR(cond, statement) \
   if (cond) { \
      perror(statement); \
      continue; \
   }
#define FATALERROR(cond, statement) \
   if (cond) { \
      perror(statement); \
      exit(EXIT_FAILURE); \
   }

/**
 * Handler for the signals.
 */
static void handler(int signum) {
   ;
}

/**
 * Main.
 */
int main(int argc, char *argv[]) {
   struct sigaction sa;
   struct stat buf;
   struct itimerval tb;
   pid_t pid;
   int modTime;

   if (argc != 2) {
      fprintf(stderr, "usage: remote file\n");
      exit(EXIT_FAILURE);
   }

   FATALERROR(stat(argv[1], &buf) == -1, "stat");
   modTime = buf.st_mtime;

   tb.it_interval.tv_sec = 0;
   tb.it_interval.tv_usec = 50000;
   tb.it_value.tv_sec = 0;
   tb.it_value.tv_usec = 50000;

   setitimer(ITIMER_REAL, &tb, 0);

   sa.sa_handler = handler;
   FATALERROR(sigemptyset(&sa.sa_mask) == -1, "mask");
   FATALERROR(sigaction(SIGALRM, &sa, NULL) == -1, "sigaction");

   while (1) {
      pause();
      CONTERROR(stat(argv[1], &buf) == -1, "stat");
      if (modTime != buf.st_mtime) {
         modTime = buf.st_mtime;
         pid = fork();
         FATALERROR(pid == -1, "fork");
         if (!pid) {
            execlp("rexec", "rexec", NULL);
            fprintf(stderr, "exec\n");
         }
      }
   }
   exit(EXIT_SUCCESS);
}



1 Answer: 

Most of your sigaction structure is not initialized, so could contain random data. If sa_flags.SA_SIGINFO is accidentally set in this uninitialized data, then the signal will cause sa_sigaction instead of sa_handler to be called, which is also uninitialized, so will almost certainly crash.

You may find it easier to debug if you initialize all the fields, including making sure you have set the flags in a way the ensures the signals behaves the way you want.

 

More Articles


php - How to redirect everything after slash to domain before?

This is what I'm trying to do:I want example.com/car/{whatever} redirected to example.com/{whatever}. For example, example.com/car/honda should be redirected to example.com/hondaBUT if there is nothing after /car/, then just leave it as that. How can i do that in .htaccess ?

ruby on rails - tmux: are my other terminals still running?

I have started using a certain technique for launching my web code since I switched server setups. I type tmuxsudo startserverrailsCtrl+B then DThen to reboot I type tmuxsudo killall rubysudo startserverrailsCtrl+B then DMy questions are: when I kill the ruby, does that end the session that I starte

node.js - Not able to run my node app at port 80 on ubuntu 16.04

I want to run my node app at port 80 in ubuntu 16.04. But the moment I'm trying to run my app with npm start its throwing error Port in already in use whereas it's not in use. According to Github Reply, We need root privileges to run on ports below 1048 which is correct.So the moment I'm trying to r


shell - Move output of Find command unix

Im learning unix programming , i want to find all files whose size is greater than 1M an print them into a file.here is my code find. -size +1M -print0 | xargs -I -O '{}' mv '{}' files all sites i have found refer to this one as right one , but it does not work . currently im working on ubuntu

php - Ubuntu Server Virtual Host not redirecting properly

I have installed Web site on my Ubuntu Server. The following things are installed on my Server for running web service. 1) Apache22) PHP53) MySql My main website is redirected to https.Example (changed for security purpose) <VirtualHost *.80>ServerName www.example.comServerAlias example.co

php - Page not found error on apache server on ubuntu 14.04

I have a project which i developed using WAMP. The project works fine as expected. I tried to use the same project on LAMP stack (Ubuntu 14.04). I imported the database to mysql server and then copied the files to /var/www/html folder and gave permissions using chmod to all files in www folder. When


linux - Run a intotify sricpt in background

how can i run 12 bash scripts simultaniously in the backgroud?That's the script:#/bin/bashwhile inotifywait -e close_write /var/www/html/wp-content/QM-Handbuch /01-Inhaltsverzeichnis.doc; do soffice --headless --convert-to pdf /var/www/html/wp-content/QM-Handbuch/01-Inhaltsverzeichnis.doc &

mysql - How to install older PHP version in XAMPP on Ubuntu 16.04?

I'm trying to install Magento on my system, But it gives me an error that the PHP version that I have is newer and Magento is not supported that version. So now I'm trying to downgrade my PHP version from XAMPP that I'm installed on Ubuntu 16.04.Error : PHP Version Check Your PHP version is 7.1.1.

c - Printf ubuntu Segmentation fault (core dumped)

I have this code:int a;printf("&a = %u\n",(unsigned)&a);printf("a\n");printf("b\n");printf("c\n");printf("d\n");I tried to print the pointer of a variable.But it fail on the row printf("a\n"); and says Segmentation fault (core dumped)Output:&a = 134525024Segmentation fault (core dumped)W

ubuntu - Docker: bash terminal starts without prompt

I have a simple container that looks like this:FROM devbox/rails3.2.1RUN apt-get install -y -q libmysql-ruby libmysqlclient-devRUN apt-get install -y -q libqtwebkit-devEXPOSE 3000CMD /bin/bashwhere devbox/rails3.2.1 is a container I made that starts with 'FROM ubuntu' and installs Ruby on Rails. Thi