LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2007, 01:52 PM   #16
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30

Quote:
What parameters do you pass to the command ?
I'd like create a daemon to monitor filesystem, your name is 'myprogram'.

./myprogram -a /directorytomonitor

./myprogram -s (stop daemon)

Quote:
What is the intended purpose of the daemonize function ?
Making daemon, that is to say daemonize() permit to run program in background, without daemonize() this program remain in foreground.

Quote:
What process do you want to kill ?
I want kill myprogram using kill syscall, or other system calls to monitor to kill processes...

Quote:
Why don't you check and print the kill return value to exhibit the problem you are complaining of ?


It is done, there are many tests:

case 's': pid = getpid();
printf("pid = getpid() : %d \n",pid);
--pid;
printf("--pid : %d \n",pid);
kill(pid,15);
printf("PID %d \n",pid);
break;
 
Old 05-27-2007, 02:28 PM   #17
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
That is never going to work the way you have set it up. You can't guarantee that getPid() - 1 is going to equal the pid of the program the last time you ran it. You never know what you might be killing if you just do a getPid() and kill the result - 1.

What most programs which need to do this do is write a file to /var/run like /var/run/myprogram.pid which just contains the pid of the daemonized program. When the program exits normally it should remove that file. If you want to kill the program then you'd read the pid out of the file, kill that pid, and if the kill was successful you'd remove the file.

This make sense?
 
Old 05-27-2007, 03:03 PM   #18
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
As jtshaw wrote, you need to store the daemon pid somewhere. Successive execution of the same program won't share variables magically.

Two more comments:

- Your daemonize function should take care of file descriptors 0, 1 and 2 (stdin, out and err) otherwise it may mess the launching shell.
Quote:



It is done, there are many tests:
...
I see no test for the kill syscall, you should do something like:
Code:
#include <signal.h>
..
if(kill(pid,SIGTERM)==-1)
{
  perror("kill");
}
...
 
Old 05-28-2007, 09:47 AM   #19
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
The test is following:


#./myprogram -a /root


#pstree -p
.....
myprogram(2756)
.....


#./myprogram -s


pid = getpid() : 2774
--pid : 2773
kill: No such process
PID 2773

How can I to obtain process of my daemon? I don't want to use system syscall....

Last edited by shifter; 05-28-2007 at 09:50 AM.
 
Old 05-28-2007, 10:04 AM   #20
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
As we have already told you... you cannot assume that subtracting 1 from your pid is going to get the daemon.

A little background.....

The process ID is simply just some unique number for each individual process on a system. Typically what linux does is just take the next unused PID when a process starts. Once it reaches the maximum value for a PID it starts over again at the beginning and finds the next unused.

If you use some of the extra security options when you build the kernel (grsec/pax patches provides these) then it simply picks a pseudo-random pid from the unused list each time a new process starts.

Either way, while it isn't impossible that starting two processes back to back will cause you to get sequential pids you certainly cannot count on that behavior given the true multiuser multitasking nature of the Linux OS.

When you start up your daemon you are going to have to store the PID of the daemon process somewhere (often in a file such as /var/run/<programname>.pid or /var/run/<programname>/pid). Then you will have to look for that file and read the pid from it to then kill the appropriate thing.

You could also search in running process... but that gets a bit messy, especially if your process ever becomes multithreaded.

I also what to emphasis something that Jilliagre said... kill, like many syscalls, returns a value. You should ALWAYS check the return values of these system calls and handle them accordingly, even if all you do is log it and die. This may seem annoying at times, but it can save you from some nasty debugging down the road.

Last edited by jtshaw; 05-28-2007 at 10:08 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syscall hook robertos Linux - Kernel 15 03-09-2007 07:33 PM
syscall hook robertos Programming 4 03-05-2007 11:48 PM
assembly execve (syscall 11) rblampain Programming 2 07-03-2005 09:23 AM
what is difference between syscall of linux and Unix sdamo Linux - General 1 06-12-2005 01:07 AM
Linux syscall 11 (execve) Genjix Programming 1 12-29-2004 11:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration