Board index » kylix » Crossplatform server sample

Crossplatform server sample


2005-06-01 07:49:25 AM
kylix1
I've plenty of experience of writing windows services (mainly using svCom)
and am now about to take the plunge with CrossKylix. Are there any sample
servers around that compile both for Windows and Linux (with CrossKylix)?
I'm really looking to avoid re-inventing the wheel if possible. I'm planning
on building web server apps and RemObjects servers.
Thanks, Bob
 
 

Re:Crossplatform server sample

Quote
I've plenty of experience of writing windows services (mainly using svCom)
and am now about to take the plunge with CrossKylix. Are there any sample
servers around that compile both for Windows and Linux (with CrossKylix)?
I'm really looking to avoid re-inventing the wheel if possible. I'm planning
on building web server apps and RemObjects servers.
CodeCentral has several examples:
cc.borland.com/beta/results.aspx
Extending a windows service application to a Kylix daemon more or less
goes like this:
Your windows app probably already has some service start entry point.
In your project file, IFDEF the part creating the service. Add an IFDEF
Block for Linux, in which the following happens:
- optional: set signal handlers so are able to respond to posix signals
(to end the service, to reload it etc)
- mask signals you don't want to receive
- Now deamonize the process by doing fork, setsid, fork, closing standard handles,
and then calling your services entry point
if fork() = 0 then
begin
setsid();
if fork()=0 then
begin
umask(0);
__close(STDIN);
__close(STDOUT);
__close(STDERR);
service_startup;
repeat
sleep(1000);
until 1=0;
end;
end;
_exit(0);
If you need more help, just ask...
Simon
 

Re:Crossplatform server sample

Simon Kissel wrote:
Quote
- Now deamonize the process by doing fork, setsid, fork, closing standard
handles,
and then calling your services entry point

if fork() = 0 then
begin
setsid();
I don't do setsid in my deamons, is it nessecary?
Will i get problems?
I have looked at man setsid, but i'm not getting wiser.
--
Best regards
Stig Johansen
 

{smallsort}

Re:Crossplatform server sample

Quote
>- Now deamonize the process by doing fork, setsid, fork, closing standard
>handles,
>and then calling your services entry point
>
>if fork() = 0 then
>begin
>setsid();

I don't do setsid in my deamons, is it nessecary?
Will i get problems?
I have looked at man setsid, but i'm not getting wiser.
AFAIK, setsid() makes sure your forked process is a process group leader. This
way it won't die if for example the parent process (the one who started it, ie
bash) dies. According to some docs it also makes sure your process doesn't receive
SIGINT on ctrl+c.
I'd suggest adding setsid() to your daemon code.
Btw, here is a quite good howto to all this:
www.linuxprofilm.com/articles/linux-daemon-howto.html
Simon
 

Re:Crossplatform server sample

Simon Kissel wrote:
Quote
I'd suggest adding setsid() to your daemon code.
What happed to the daemon() function? Is it obsulute and I have missed
this information? The daemon() API should do all the things necessary for
a disk and execution monitor.
if daemon(0, 0) = -1 then
begin
WriteLn('Failed'
Halt(1);
end;
Initialize;
// ...
--
Regards,
Andreas Hausladen
 

Re:Crossplatform server sample

Andreas,
Quote
>I'd suggest adding setsid() to your daemon code.

What happed to the daemon() function? Is it obsulute and I have missed
this information? The daemon() API should do all the things necessary for
a disk and execution monitor.

if daemon(0, 0) = -1 then
begin
WriteLn('Failed'
Halt(1);
end;
Initialize;
// ...
You are right, the daemon() function is a nice alternative. However, the
"manual" approach is a bit more compatible. The daemon() call didn't exist in
pre-2.0 glibc and in uclibc back when I checked...
However, these days probably this isn't too relevant anymore. So yeah, using
daemon() probably is the simplest way to do it, and internally it does exactly the
same stuff. What it's not doing is masking signals, you still need to do that
yourself.
Simon
 

Re:Crossplatform server sample

Bob Devine wrote:
Quote
I've plenty of experience of writing windows services (mainly using
svCom) and am now about to take the plunge with CrossKylix. Are there
any sample servers around that compile both for Windows and Linux
(with CrossKylix)? I'm really looking to avoid re-inventing the
wheel if possible. I'm planning on building web server apps and
RemObjects servers.
FWIW, I've had a project on the backburner for some time to do with
building daemons in Kylix, using an add-in rather like the service app
wizard in Delphi.
It was originally going to be part of a BorCon session, however for
obvious reasons it was knocked back (twice, I think), and I was
encouraged to make it an article for BDN, however I haven't had
sufficient incentive to finish and publish it yet.
Now if only I could find the code... <g>
--
Dave Nottage [TeamB]
 

Re:Crossplatform server sample

Thanks all - this is just the information I needed. BTW Simon,
congratulations on a lovely piece of software - makes a dual-platform
approach much easier.
Cheers, Bob
 

Re:Crossplatform server sample

Simon Kissel wrote:
[snip]
Quote
I'd suggest adding setsid() to your daemon code.
After looking at my main project code, i am indeed doing setsid.
Sorry for remembering wrong.
The 'skeleton' program was developed using bits and pieces 4 year ago with
help from this NG.
I have a feeling, that with your answer, i am beginning to understand
why/how my deamon code works. :-)
Thanks.
--
Best regards
Stig Johansen
 

Re:Crossplatform server sample

On 2005-06-02, Andreas Hausladen < XXXX@XXXXX.COM >wrote:
Quote
Simon Kissel wrote:

>I'd suggest adding setsid() to your daemon code.

What happed to the daemon() function? Is it obsulute and I have missed
this information?
Afaik it never was POSIX. It is a BSD call that Linux copied.