Hello,
i am very very puzzled,
im doing this project for school, its a deamon logger, but anyways
I'm supposed to run the daemon, let it run on the backgroun, and then run a different program (from command like prompt). but when i run my daemon, it never goes back to the nova> prompt.
i dont know what that means, and what i did wrong... someone please help.
Below is my code:
*Basically what it's supposed to do it look for files in a given direcotry, and append them to the Main Log File, and delete the old files. Only keeping the MainLogFile. So yea..... i got to work as a regular program, but when i tried to make into a daemon, it just freezes up, after eerything is done, not letting me run the main program.
i dont know if i'm exmplaining myself right.... but PLEASE HELP ME!!!!
----Tanya
int filedata(char *pathname, int x);
int List()
//list files in the current working directory
{
//check if already a daemon
if(getppid() ==1) return(0);
//fork
int i;
i = fork();
if(i<0) exit(1); //fork error, exit
if(i>0) exit(0); //parent exits
//child continues......
setsid(); //obtain new process group
//variable declarations
struct dirent *d;
char *NewDir;
DIR *dp;
char buffer[50];
static int x=0;
chdir("LogFiles");
NewDir = getcwd(buffer, 50);
cout << "Dir: " << NewDir << endl;
dp = opendir(NewDir);
while(d = readdir(dp))
{
if(d->d_ino != 0)
{
filedata(d->d_name,x);
x++;
}
}
closedir(dp);
//signla handlers
signal(SIGCHLD, SIG_IGN); //ignore the child
signal(SIGTSTP, SIG_IGN); //ignore tty signals
sigset(SIGHUP, SIG_IGN);
sigset(SIGINT, SIG_IGN);
sigset(SIGPOLL, SIG_IGN);
sigset(SIGCLD, SIG_IGN);
sigset(SIGTERM, SIG_IGN);
return (0);
}
int filedata(char *pathname, int x)
//list file detail in the directory
{
struct stat statbuf;
ifstream in; //reading
if(stat(pathname, &statbuf) == -1)
{
cout << "could not stat " << pathname << endl;
return (-1);
}
cout << pathname << endl;
string Msg, ToFileFinal;
string ToFile;
if(x <2)
{
cout << ". and .. right now...." << endl;
}
else
{
cout << "FileName: " << pathname << endl;
//read from a file
in.open(pathname);
getline(in, ToFile);
getline(in,Msg);
cout << "ToFile: " << ToFile << endl;
cout << "Message: " << Msg << endl;
//write to a file
ToFileFinal = "../";
ToFileFinal.append(ToFile);
cout << "the output will be written to: " << ToFileFina
cout << endl;
ofstream out;
out.open(ToFileFinal.c_str(), ios::app);
out << Msg;
out << endl;
out << "*** | *** | *** | *** | *** | ***" << endl;
//close the files before leaving
in.close();
out.close();
//delete the input files
cout << "about to remove..." << pathname << endl;
remove(pathname);
wait(30);
}
return (statbuf.st_mode);
}
int main()
{
List();
while(1) sleep(1);//run
return (0);
}