I amm currently porting one of our DOS producst to Linux. one of the
features this DOS product had was via a TCP/IP conenction a remote
user could reboot the PC, if they had changed the configuration of the
application.
In my Linux port I don't need to reboot the whole pc as I can get a
way with just restarting my application.
The question is how..
Currently I have created the administration module (this is the bit
that hande re-starts) as a separate process which is loaded and
started by the main application and it works fine. My restart code in
the admin module looks a bit like this...
pid_t pid = fork();
/* set up args */
...
switch( pid )
{
case -1:
fprintf( stderr, "reset failed" );
break;
case 0:
execvp( app, args );
break;
default:
exit( EXIT_SUCCESS );
break;
}
I have 3 problems
1) when the main app comes back up it does not restart the admin
process
2) The main app can not bind to the port due the address being in use
despite closing and shutting it down on exit.
3) the original admin process is reported as defunct when I do a ps x
any help much apreciated.
thanks
Robin
------
Robin Imrie
Software Engineer
Email: robin [dot] imrie [at] sysmedia [dot] com
You should have a newline at the end of that error
message. But actually perror would have been much
more apropriate. In which case it would have looked
like:
perror("fork()");
Notice that no newline is required in this case.
Actually the error checking should be done right
after calling fork. Doing more stuff after calling
fork and before checking the return value seems
like a bad idea. Probably whatever code you have
there really should be done before calling fork
or after checking the return value and only by
one of the processes. Since the parent is going
to terminate I guess you really want to do it in
the parent process. Though without knowledge of
your program this is of course just guessing.
> break;
>
> case 0:
> execvp( app, args );
Remember to check for errors. Actually execvp will
only return if there is an error, so you actually
don't need to check anything, just print an error
message. Again perror is apropriate, I suggest
adding this line between execvp and break:
perror(app);
> break;
>
> default:
> exit( EXIT_SUCCESS );
> break;
What is the exact purpose of this? You create a new
process that execute a given command, and the parent
terminates. Why not just execute in the current
process and avoid the fork altogether? If you really
want to do the above take a look on the daemon
library function, it might be useful.
> }
>
> I have 3 problems
>
> 1) when the main app comes back up it does not restart the admin
> process
Doesn't look like that problem is related to the
piece of code you posted. Probably the reason is
really elsewhere. Would you by any chance be trying
to bind to a port that is already in use?
>
> 2) The main app can not bind to the port due the address being in use
> despite closing and shutting it down on exit.
Possibly you want to make use of the SO_REUSEADDR
socket option. Here is an example from one of my
programs:
int main()
{
int sock_fd;
struct sockaddr_in sa;
int yes=1;
sock_fd=socket(PF_INET,SOCK_STREAM,0);
if (sock_fd == -1) errstop("scoket");
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)))
errstop("SO_REUSEADDR");
>
> 3) the original admin process is reported as defunct when I do a ps x
That means the parent process still exists and
have not queried the kernel about the exit
status of the process:
http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#zombie
--
Kasper Dupont -- der bruger for meget tid paa usenet.
For sending spam use mailto:aaa...@daimi.au.dk
/* Would you like fries with that? */