Main Page | Class List | File List | Class Members | File Members

rssbgr.c

Go to the documentation of this file.
00001 /*
00002  *  $Id: rssbgr.c,v 1.37 2006/04/17 22:45:19 ghost666 Exp $
00003  *
00004  *  rssbgr - RSS backgroud reader
00005  *  Copyright (C) 2005, 2006 Piotr 'GhosT' Wydrych
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #include <stdio.h>
00023 #include <pthread.h>
00024 #include <sys/types.h>
00025 #include <sys/wait.h>
00026 
00027 #include "share.h"
00028 #include "execute.h"
00029 #include "fetch.h"
00030 #include "lockfile.h"
00031 #include "md5.h"
00032 #include "readconfig.h"
00033 #include "replace_fields.h"
00034 #include "signal.h"
00035 
00036 #define FETCH_BUFFER_SIZE 10240
00037 #define WRITE_BUFFER_SIZE 100
00038 
00039 char *home; /* $HOME */
00040 
00041 int main(void)
00042 {
00043  pid_t child_pid, grandchild_pid;
00044  pthread_t *fetch_threads;
00045  int status;
00046  directory = (char *)malloc(PATH_MAX);
00047  lockfile = (char *)malloc(PATH_MAX);
00048  configfile = (char *)malloc(PATH_MAX);
00049  home = (char *)getenv("HOME");
00050  if (home == NULL) DIE("HOME variable not set. Program will exit.")
00051  sprintf(directory, "%s/" DIRECTORY, home);   directory = (char *)realloc(directory, strlen(directory)+1);
00052  sprintf(lockfile, "%s/" LOCKFILE, home);     lockfile = (char *)realloc(lockfile, strlen(lockfile)+1);
00053  sprintf(configfile, "%s/" CONFIGFILE, home); configfile = (char *)realloc(configfile, strlen(configfile)+1);
00054  i_am = I_AM_PARENT;
00055  unsigned int i;
00056 
00057  if ((child_pid = fork())<0)
00058   DIE("Cannot fork. Program will exit.")
00059  
00060  switch(child_pid)
00061  {
00062   case 0:
00063    i_am = I_AM_CHILD;
00064    DEBUG2("Child starts. PID: %i", (int) getpid())
00065    do
00066    {
00067     if ((grandchild_pid = fork())<0)
00068      DIE("Cannot fork. Program will exit.")
00069     switch(grandchild_pid)
00070     {
00071      case 0:
00072       i_am = I_AM_GRANDCHILD;
00073       set_signal_handlers();
00074       DEBUG2("Grandchild starts. PID: %i", (int) getpid())
00075       create_lockfile();
00076       readconfig();
00077       if (!config_items_count)
00078       {
00079        fprintf(stdout, "No valid entries in config file. Please run rssbgr-conf.\n");
00080        unlink(lockfile);
00081        _exit(1);
00082       }
00083       fetch_threads = (pthread_t *)malloc(config_items_count*sizeof(pthread_t));
00084       for (i=0; i<config_items_count; i++)
00085        pthread_create(&(fetch_threads[i]), NULL, fetch_thread, (void *)((size_t)i));
00086       check_lockfile();
00087       fprintf(stderr, "Lockfile violation.\n");
00088       for (i=0; i<config_items_count; i++)
00089        pthread_cancel(fetch_threads[i]);
00090       unlink(lockfile);
00091       DEBUG1("Grandchild ends")
00092       break;
00093      default:
00094       set_signal_handlers();
00095       waitpid(grandchild_pid, &status, 0);
00096       DEBUG2("Grandchild returned %i", status>>8)
00097       if (!status) fprintf(stderr, "RSS background reader will restart.\n");
00098     }
00099    } while(grandchild_pid && (!(status>>8)));
00100    if (grandchild_pid) DEBUG1("Child ends")
00101    break;
00102   default:
00103    DEBUG2("Parent starts. PID: %i", (int) getpid())
00104    sleep(1);
00105    DEBUG1("Parent ends")
00106  }
00107  return 0;
00108 }

Generated on Fri May 26 08:40:18 2006 for RSSbgr by doxygen 1.3.6