// sitarctl.c
// $Id: sitarctl.c,v 1.1.1.1 2007/09/19 05:49:54 sendan Exp $
// masashi shimakura

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>

void initdaemon(void);

char co[1024];
char pr[1024];
char aa[1024];
char * conf;
char * prog;
char * all;
char * path;
FILE * fp;

int main(int argc, char * argv[])
{
int times, stat_return;
int uid;
int pid;

initdaemon();

path = "/usr/local/zither/lock/sitarctl.pid";
conf = co;
prog = pr;
all = aa;

if(argc < 5 || argc > 5){
   printf("------- sitarictl Vr1.1.1c is a program that starts sitar. ------\n");
   printf("--------- Please start by the following options. -------\n");
   printf("sitarctl <sitar path> <sitar conf path> <exec UID> <restart interval>\n");
   exit(1);
   }

prog = argv[1];
conf = argv[2];
uid = strtol(argv[3], (char **)NULL, 10);
times = strtol(argv[4], (char **)NULL, 10);

if(1024 > ((int)strlen(conf) + (int)strlen(prog) + (int)strlen(" -c "))){
   memcpy(all, prog, (int)strlen(prog)+1);
   strncat(all, " -c ", 5);
   strncat(all, conf, (int)strlen(conf)+1);
   fprintf(stderr,"exec options %s\n", all);
   }
else{
   fprintf(stderr,"data long\n");
   exit(1);
   }

if(setuid(uid)){
   fprintf(stderr,"Not setuid err\n");
   exit(-1);
   } 

pid = getpid();
if((fp = fopen(path, "w+"))!=NULL){
   fprintf(fp,"%d\n", pid);
   fclose(fp);
   }
else {
   fprintf(stderr,"-- Not open sitar pid file. %s\n", path);
   exit(-1);
   } 

for(;;){
   stat_return = system(all);
   fprintf(stderr,"Reactivation in sitar process stop and %d seconds\n", times);
   fprintf(stderr,"return stat %d\n", stat_return);
   sleep(times);
   }
return stat_return;
}  



