Operating System - Linux
1827892 Members
1923 Online
109969 Solutions
New Discussion

Re: how to get file date/time of creation or update

 
SOLVED
Go to solution
Pedro Dinis
Advisor

how to get file date/time of creation or update

how to get file date/time of creation or update

i have this daemon that must read this config xml file, but when this xml file changes the daemon must detect this and perform a new read.


how can i do this ?

thanks

SLB SLB SLB Glorioso
14 REPLIES 14
Ralph Grothe
Honored Contributor
Solution

Re: how to get file date/time of creation or update

The usual way for daemons would be that they implement a signal handler that catches a SIGHUP, and on this event causes them to reread their configuration.
SIGHUP is commonly taken because daemons have no real use for this signal anymore as they have dissociated from any controlling terminal and have either redirected or reopened their standard file descriptors stdin, stdout and stderr.

Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: how to get file date/time of creation or update

The usual way for daemons would be that they implement a signal handler that catches a SIGHUP, and on this event causes them to reread their configuration.
SIGHUP is commonly taken because daemons have no real use for this signal anymore as they have dissociated from any controlling terminal and have either closed or reopened their standard file descriptors stdin, stdout and stderr.

Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: how to get file date/time of creation or update

Excuse the double post,
the ITRC webserver is slow
Madness, thy name is system administration
James R. Ferguson
Acclaimed Contributor

Re: how to get file date/time of creation or update

Hi:

Ralph has provided the standard approach.

If you feel that you must be intrinsically sensitive to changes in the configuration file, your daemon could periodically 'stat()' the configuration file for its 'mtime'. At startup you would have cached the file's value. If the 'mtime' has changed, reread the configuration and re-cache the new timestamp. A 'sigalam' could be useful to construct the appropriate behavior.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: how to get file date/time of creation or update

Hi (again):

By the way, Unix has no such thing as a "creation" timestamp. The 'mtime' (last modification time) [see 'stat(2)'] happens to equate to a creation point only when a file is first created. Subsequent modifications of the file's contents obliterate the original timestamp value.

Regards!

...JRF...
Pedro Dinis
Advisor

Re: how to get file date/time of creation or update

i managed to have this working with stat,

but i am still trying to do this in the standard way with the signal signal(SIGHUP,mostrasinal); anyone knows where i can get an example of a daemon with configuration files.

thanks

///buscar o mtime do ficheiro
time_t buscafichmtime(const char *filename)
{
struct stat sbuf;
if(stat(filename,&sbuf) == -1)
{
perror("stat");
exit(1);
}
return sbuf.st_mtime;
}


main()


/* Daemonize */
daemonize( DAEMON_NAME );

time_init=buscafichmtime(CONFIG_FILE_NAME);
time_temp = 0;

while(1)
{
time_actual=buscafichmtime(CONFIG_FILE_NAME);
cout << "time_actual : " << time_actual << endl;

if (time_temp==0)
{
if(time_actual!=time_init)
{

cout << "time_actual!=time_init"< runconfdeamon();
}
time_temp=time_actual;
}
else
{
if(time_actual!=time_temp)
{
cout << "time_actual!=time_temp"< runconfdeamon();
}
time_temp=time_actual;
}
cout << "mtime init :" << time_init << endl;

sleep(1); /*run*/

}
SLB SLB SLB Glorioso
Dennis Handly
Acclaimed Contributor

Re: how to get file date/time of creation or update

>but i am still trying to do this in the standard way with the signal
signal(SIGHUP,mostrasinal);

What problems are you having? In your signal handler mostrasinal, you just set a volatile flag indicating you need to reread your config files.

Then in your main while loop, you test it.
Pedro Dinis
Advisor

Re: how to get file date/time of creation or update

the problem is when someone changes the config.xml file the signal is not called, only with stat i can verify if the file was changed.


thanks
SLB SLB SLB Glorioso
Dennis Handly
Acclaimed Contributor

Re: how to get file date/time of creation or update

>When someone changes the config.xml file the signal is not called

Ah, I thought the suggestion was that you were going to require them to send SIGHUP?
James R. Ferguson
Acclaimed Contributor

Re: how to get file date/time of creation or update

Hi (again):

A couple of comments.

> the problem is when someone changes the config.xml file the signal is not called, only with stat i can verify if the file was changed

You need to have a signal handler that catches the SIGHUP signal (when it is sent) and calls a function whose purpose is to re-read your configuration file.

As Ralph noted, this is a standard paradigm for handling a daemon's configuration file and the need to re-read it without terminating and restarting the daemon. It gives *you* control over *when* you want to re-read a modified configuration file.

My suggestion to use SIGALRM was based on the idea that you could do something like the following. In the normal life of your daemon, events happen to awaken it perform its intended work. Perhaps as a you finish a cycle of that work you also call a function that stat()'s the configuration file to see if it has changed and re-read it if so.

Also as a part of the normal work cycle, you raise your own SIGALRM to schedule a "wake-up" in some reasonable number of seconds (maybe every minute). A signal handler also exists for SIGALRM and if invoked, it calls your fucntion to stat()' the configurtion file. The idea is that if no other event awakens your daemon, your alarm does and thus you remain sensitive to configuration file changes. Cancel the SIGALRM any time yo had real work and have otherwise interrogated the configuration file.

By the way, in the code you posted, you poll every second. This will make your code very processor intensive without any real gain. After all, the granularity of stat() timestamps is only one-second anyway. Perhaps you have the setting so low only for testing your code.

Regards!

...JRF...
Pedro Dinis
Advisor

Re: how to get file date/time of creation or update

hi

i am doing sleep(1) to test.

The problem is that when someone changes the configuration file , the daemon it is not getting any event.

the only solution i have this moment thanks to you is to stat() the configuration file.

i am now looking for a way to monitor the configuration file.

the sleep cannot be larger the 1 minute , if someone changes the configuration file and the daemon is not updating the memory segment that someone is going to be very unhappy
SLB SLB SLB Glorioso
Pedro Dinis
Advisor

Re: how to get file date/time of creation or update

Linux has something called FAM

can i find something like this in HP-UX ?
SLB SLB SLB Glorioso
Dennis Handly
Acclaimed Contributor

Re: how to get file date/time of creation or update

>if someone changes the configuration file and the daemon is not updating the memory segment that someone is going to be very unhappy

That's why you put the burden on the end user to send that SIGHUP.
James R. Ferguson
Acclaimed Contributor

Re: how to get file date/time of creation or update

HI (again):

> Linux has something called FAM...can i find something like this in HP-UX ?

I don't think so. Based on the documentation, I think you understand your options. Once agan, the SIGHUP method is a standard one. Perhaps most importantly, using SIGHUP gives *you* control as to when you want your daemon to "see" a changed configuration.

I found a seemingly good overview of the FAM product here:

http://oss.sgi.com/projects/fam/faq.html

Regards!

...JRF...