UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Processes
  • » how do i know the pid of a daemon launched by shell script?

#1 2006-11-09 11:47 AM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how do i know the pid of a daemon launched by shell script?

[email protected]

i just asked myself if it's possible to get the process id of a process launched with this scenario:
my own process start's a shell script with vfork and exec
now i know the pid of the shell script
the shell script does some intiailization stuff and starts for example a postgresql server
this server runs as a daemon and has parent process id of 1
the shell script exit's
and my process doesn't notice that the shell script started another process

now my question:
how can my prog know that a child process started other processes?
it's like using dbg for debugging child process, but i only need something like a fork hirarchy
one process start's another one, which start's another one, which is a daemon and so on
how do i know the pid's of every process?
i thought about using ptrace function like "strace" does, but i think it could slow down the system if i trace processes like a database server ;-)

[email protected]

Offline

#2 2006-11-09 02:32 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: how do i know the pid of a daemon launched by shell script?

Problem is that daemons in general create their own process group and totally detach themselves from their parent, in your case the shellscript. But also when the parent exits the daemon process becomes a child of init.

First I'd try to approach your problem another way so that you don't need this info. What exactly do you want to do? Maybe it can be done in another way.

Offline

#3 2006-11-09 04:42 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how do i know the pid of a daemon launched by shell script?

until now i'm only evaluating the possibilities

but what i try to do is, something like a userland task scheduler

it's just a simple prog which checks the server load in an interval
if the load rises, it checks it's config
in the config it can find which application should be monitored
for my example it would be easy
i could simply add the postgresql
but i want it to be as much flexible as possible

the final solution should do this:
1. check server load
2. if server load rises check which process is responsible for the higher load
   (i think i will read the "top" source for this, e.g. if a process uses 99% cpu)
3. check the config if this process should be userland-scheduled (and that's why i would need it this way)
4. suspend the process
5. continue the process
6. continue with 1.

the point is: the check if the process should be scheduled should also be easy to administrate
so for my example the admin would just add the shell script to the scheduler task list and the scheduler knows that the bash script started another prog, which means, it has to schedule this task too
so to be maximum "easy to use" and "flexible" i'll have to use pid's, cause app names can change during runtime

hope it's understandable ;-)

the final prog can be used to monitor a server and if it's going to crash, slow down some processes and notify someone OR in combination with a cluster the load of the whole cluster would be easier to balance (that's what i try, finally ;-) ) cause the monitor recognizes if the host is heavy loaded and can stop a process, make an image of the process and resume it on another server with fewer load

Offline

#4 2006-11-09 04:57 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: how do i know the pid of a daemon launched by shell script?

If a script starts something up, and then quits, what then? You can't know whether it started the daemon up or not except if you traced all exec calls. So to achieve this you would need to trace all exec calls on the system (or at least the user). Is that what you want?

Offline

#5 2006-11-09 08:47 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: how do i know the pid of a daemon launched by shell script?

Yeah, I think you're trying too hard to make your config file user-friendly, which can
only come at the expense of greatly increased code complexity and greatly lowered
performance...  I think you'd be best off simply requiring the config file to list the
REAL process that they wish to monitor, not any cover scripts or anything like that...
And, you should probably locate the process via executable name (on Linux, you
can just browse through "/proc/<pid>/" and read the "exe" symlink there)...

Online

#6 2006-11-10 05:20 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how do i know the pid of a daemon launched by shell script?

@i3839
jeah, that's a possiblity
but i think rob is right
it would be a performance problem

@RobSeace
well i think i'll use your suggestion
thx Rob

Offline

#7 2006-11-11 04:17 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how do i know the pid of a daemon launched by shell script?

Offline

#8 2006-11-11 06:16 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: how do i know the pid of a daemon launched by shell script?

Yes, that's what I wanted to suggest when you decided to go tracing all exec calls on the system (tracing clones isn't smart, as those happen way more often, also for thread creation). But you need to trace them all or it won't work. To me it seems quite overkill for your application, and you'd still be matching on program names.

(The reason you need to trace them all is that you don't know beforehand if the LD_PRELOAD var is needed for a specific application, so you can't set it only for the apps you want. Even then another app can start your app and you wouldn't know about it. So you need to trace all exec calls if you use LD_PRELOAD or /etc/ls.preload.)

Offline

#9 2006-11-12 01:08 AM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how do i know the pid of a daemon launched by shell script?

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc,char **argv){
        switch(fork()){
                case 0:
                        fork();
                        sleep(5);
                        return 0;
                 default: sleep(5);
                          return 0;
        }
        return 0;
}

Offline

#10 2006-11-12 04:06 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: how do i know the pid of a daemon launched by shell script?

I meant /etc/ld.preload. Butif you only care about programs started by your app then it isn't needed.

No need for a dlopen, just use dlsym with RTLD_NEXT as (pseudo) handle. See the manpage of dlsym for more info.

Better to have a function pointer to the real "fork" which is set by the lib's init function (__attribute__((constructor)) for gcc IIRC), that way you avoid the lookup all the time.

Fork is a seperate systemcall, but glibc has it's wrapper around it to do thread specific stuff and similar stuff.

Why the hell are you trapping fork instead of the exec* calls?

Offline

#11 2006-11-12 04:16 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how do i know the pid of a daemon launched by shell script?

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <dlfcn.h>

pid_t fork(void);
void __attribute__ ((constructor)) libmyfork_init(void);

pid_t (*libc_fork)(void);

/* The init function for the library */
void __attribute__ ((constructor)) libmyfork_init(void) {
          char *error = NULL;
          libc_fork = (pid_t (*)(void)) dlsym(RTLD_NEXT, "fork");
          if ((error = dlerror()) != NULL) {
               fprintf(stderr, "%s\n", error);
               exit(-1);
          }
}

pid_t fork(void){
   pid_t retval = 0;
   printf("calling fork()\n");
   retval = libc_fork();
   printf("fork() returned %u\n", retval);
   return retval;
}

Offline

#12 2006-11-12 05:45 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: how do i know the pid of a daemon launched by shell script?

Some minor nitpicks:

- Best to make libc_fork and libmyfork_init static. Best to keep the global namespace as clean as possible, especially if you're a guest lib.

- You can set that constructor attribute also only on the declaration of the function, and leaving it away by the function definition. This makes the code cleaner.

- If you trace fork then you probably should also trace vfork and clone calls.

Offline

#13 2006-11-12 06:20 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: how do i know the pid of a daemon launched by shell script?

Realize that if you start any setuid/setgid binaries, then $LD_PRELOAD won't work,
because the dynamic linker will wipe out all such envvars from the environment,
because they're potential security vulnerabilities...  I think "/etc/ld.so.preload" will still
work on them, though...

Online

  • Index
  • » Processes
  • » how do i know the pid of a daemon launched by shell script?

Board footer

Powered by FluxBB