Daemon...Zombie?? Please help me

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. :frowning:
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. :frowning:
i dont know if i'm exmplaining myself right.... but PLEASE HELP ME!!!!

----Tanya :slight_smile:

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);
}

Please make sure that you have read our rules. And note:
(6) Do not post classroom or homework problems.

But we sometimes bend this rule when a student has done a lot of work. So I'll make a few comments based on a quick scan of your code. This line of code:
while(1) sleep(1);//run
is dangerous. Why do you have it? I would delete it. If you meant to do something like:
while (1) { List() ; sleep(1) ; }
then you have the fork() in the wrong place. You need to fork() once at the beginning of the program. At present, your program is running List() once and then falling into your infinite sleep loop. You have some re-writing to do.

wait(30);
is illegal and also dangerous. I don't even know what you want to happen there. The last thing you do in List is to ignore some signals. You should ignore signals earlier. If you put List() in a loop, you need to move the signal calls to near the beginning somewhere.

Your instructor probably told you to check if the parent pid is 1, but your instructor is wrong. That is not the definition of a daemon. As it happens, you can probably get away with test you have. But the definition of a daemon is a program with no controlling terminal. You could check that by attempting to open /dev/tty. A daemon must fail but a non-damon must succeed.

Hi,
i understand what you say about the while(1), sleep(1), someone had actually told me to put it in on another forum(which wasn't that helpfull at all),
but my question is about the fork, when you said i need to fork in the begining, do you mean all the forking is done in the main(), and it just calls the function from there?(instead of forking in the List())
.... i was thinking of putting
for(;;){ open directory, and do all the file stuff here}, but that isn't working either.
I'm sorry to bugg you with this... i think i'm just having problems with the logic here. like you said it goes into infinate sleep.... so i though an infinite loop would help. but is it in the wrong place?

thanks again
Tanya R.

I don't understand what your program is supposed to be doing, so I don't know what it should look like. I would tend to fork and then ignore signals in main. It's not wrong to do stuff like that in a function. But I don't know if you want to repeatedly call List in a loop or not. If it is called repeatedly, then you need to remove one-time initialization from it. I will close this thread now. If you still have problems you need to talk to your instructor.