00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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;
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 }