Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<unistd.h>
- #include<syslog.h>
- #include<signal.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- void handle_alarm( int sig ) {
- syslog( LOG_INFO, "ALARM FROM MY DAEMON");
- }
- int main( int argc, char* argv[] ) {
- pid_t pid, sid;
- // Forkataan prosessi daemonille
- pid = fork();
- if (pid < 0) {
- return -1;
- }
- // Prosessi luotu, voidaan poistua parent -prosessista
- if (pid > 0) {
- return 0;
- }
- umask(0);
- // Luodaan uusi sessio
- sid = setsid();
- if (sid < 0) {
- return -1;
- }
- // Vaihdetaan hakemisto root:iin
- if ((chdir("/")) < 0) {
- return -1;
- }
- // Suljetaan standardi streamit
- close(STDIN_FILENO);
- close(STDOUT_FILENO);
- close(STDERR_FILENO);
- // Itse daemonin koodi...
- signal( SIGALRM, handle_alarm );
- openlog( "MYDAEMON", LOG_PID, LOG_DAEMON);
- alarm( 10 );
- while (true) {
- syslog( LOG_INFO, "DAEMON WOKE UP TO DO SOME THINGS...");
- sleep( 10 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement