Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4230518
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T01:52:28+00:00 2026-05-21T01:52:28+00:00

I have a program that checks the modification time of a file and executes

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T01:52:28+00:00Added an answer on May 21, 2026 at 1:52 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that checks if a file is present every 3 seconds,
I have a program that I need to run under *nix and windows. because
We have a program that the installer checks for the existence of a config
I have a WinForm program that checks a database for changes, and if there
I have a program that spits out both standard error and standard out, and
I have a program that creates a Windows user account using the NetUserAdd() API
I have a program that uses the mt19937 random number generator from boost::random. I
I have a program that spits out an Excel workbook in Excel 2003 XML
I have a program that monitors debug messages and I have tried using a
I have a program that will calculate the minimal area taken by fitting rectangles

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.