Subject: Re: Win98se and using SSHD as a TRUE service
From: JAmes Coleman <[email-masked]>
To: Gerald Villemure <[email-masked]>
Cc: [email-masked]
Date: Thu, 15 Nov 2001 11:18:38 +0000

Hi Gerald,

Gerald Villemure wrote:
> 
> The idea here is to have SSHD start as a true service in Win9x such that it
> will NOT dies when a user logs out.  To do this the process must make the
> system call "RegisterServiceProcess" which only exist in Win95, Win98 and
> WinMe.
> 
> James Coleman:  I tryied adding the #include "windows.h" line but I still
> have compile errors:
> 
> $ make
> gcc -g -O2 -Wall -Wpointer-arith -Wno-uninitialized -I. -I.. -I. -I./..  -DH
> AVE_CONFIG_H -c daemon.c
> daemon.c: In function `daemon':
> daemon.c:67: parse error before `kerneldll'
> daemon.c:68: `kerneldll' undeclared (first use in this function)
> daemon.c:68: (Each undeclared identifier is reported only once
> daemon.c:68: for each function it appears in.)
> daemon.c:72: parse error before `('
> daemon.c:73: `RegisterService' undeclared (first use in this function)
> daemon.c:79: `RegisterService' used prior to declaration
> daemon.c:79: warning: implicit declaration of function `RegisterService'
> make: *** [daemon.o] Error 1

I had a go at compiling what you sent, obviously I can't compile it
properly
 but I can test the syntax anyway.
 I just had to remove #include "includes.h"
 For me, gcc version 2.9-cygwin-990830, I got similar errors to you and
others 
 which I got rid of by fixing includes ... (except for one).

daemon.c: In function `daemon':
daemon.c:82: parse error before `kerneldll'
daemon.c:83: `kerneldll' undeclared (first use in this function)
daemon.c:83: (Each undeclared identifier is reported only once
daemon.c:83: for each function it appears in.)
daemon.c:87: parse error before `('
daemon.c:88: `RegisterService' undeclared (first use in this function)
daemon.c:95: `RegisterService' used prior to declaration
daemon.c:95: warning: implicit declaration of function `RegisterService'
daemon.c:108: `_PATH_DEVNULL' undeclared (first use in this function)

(I add the following includes)
//#include <includes.h>
#include <sys/types.h>
     #include <unistd.h>
#include <sys/types.h>
     #include <sys/stat.h>
     #include <fcntl.h>
#define HAVE_CYGWIN

ANYWAY

Two things .... (possibly caused by command line options?)

  1. compiler doesn't like // comments

  2. compiler doesn't like declaration of variables except at start of
function

The following compiles okay for me (with only the one error I introduced
which you shouldn't have)

int
daemon(nochdir, noclose)
        int nochdir, noclose;
{
        int fd;
        HINSTANCE kerneldll;
        DWORD (*RegisterService)(DWORD, DWORD);

        switch (fork()) {
        case -1:
                return (-1);
        case 0:
                break;
        default:
#ifdef HAVE_CYGWIN
                /*
                 * This sleep avoids a race condition which kills the
                 * child process if parent is started by a NT/W2K
service.
                 */
                sleep(1);

                /*
                 * This is the code that I took from WinVNC
                 * The best solution would be to enclose this in a IF
Win9x
clause
                 * that is if I knew propers C syntax I could do this
                 */

                         /* Obtain a handle to the kernel library */
                         kerneldll = LoadLibrary("KERNEL32.DLL");
                         if (kerneldll == NULL)
                                 break;
 
                         /* And find the RegisterServiceProcess function
*/
                         RegisterService = (DWORD (*)(DWORD, DWORD))
                                 GetProcAddress(kerneldll,
"RegisterServiceProcess");
                         if (RegisterService == NULL)
                                 break;
 
                         /* Register this process with the OS as a
service! */
                         RegisterService(NULL, 1);

#endif
                _exit(0);
        }

        if (setsid() == -1)
                return (-1);

        if (!nochdir)
                (void)chdir("/");

        if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
                (void)dup2(fd, STDIN_FILENO);
                (void)dup2(fd, STDOUT_FILENO);
                (void)dup2(fd, STDERR_FILENO);
                if (fd > 2)
                        (void)close (fd);
        }
        return (0);
}

> 
> The source is at the end of this email.
> 
> The problem may be easy to spot but its beyond me.

I can help a bit with C and a little with cygwin but I don't do all that
much
cygwin+windows programming.   Hopefully this is useful.

> 
> I am trying to fix the right file for this right?

:)

That's a very good question .... and I really don't know the answer to
it :-7

Last time I played with services was on msdos5.something!

> 
> Thanks for any help,
> Gérald
> 

Good Luck!

JAmes.

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/