4. Your main() Routine
Prev | Chapter 4. Generic server | Next

## 4. Your main() Routine

As mentioned, your `main()` routine can be quite brief. If you want to initialize global parameters, or read global configuration tables, this is the place to do it. At the end of the routine, you should call the function

    int statserv_main(int argc, char **argv,
                      bend_initresult *(*bend_init)(bend_initrequest *r),
                      void (*bend_close)(void *handle));

The third and fourth arguments are pointers to handlers. Handler `bend_init` is called whenever the server receives an Initialize Request, so it serves as a Z39.50 session initializer. The `bend_close` handler is called when the session is closed.

`statserv_main` will establish listening sockets according to the parameters given. When connection requests are received, the event handler will typically `fork()` and create a sub-process to handle a new connection. Alternatively the server may be setup to create threads for each connection. If you do use global variables and forking, you should be aware, then, that these cannot be shared between associations, unless you explicitly disable forking by command line parameters.

The server provides a mechanism for controlling some of its behavior without using command-line options. The function

        statserv_options_block *statserv_getcontrol(void);

will return a pointer to a `struct statserv_options_block` describing the current default settings of the server. The structure contains these elements:

`int dynamic`

  A boolean value, which determines whether the server will fork on each incoming request (TRUE), or not (FALSE). Default is TRUE. This flag is only read by UNIX-based servers (WIN32 based servers doesn't fork).

`int threads`

  A boolean value, which determines whether the server will create a thread on each incoming request (TRUE), or not (FALSE). Default is FALSE. This flag is only read by UNIX-based servers that offer POSIX Threads support. WIN32-based servers always operate in threaded mode.

`int inetd`

  A boolean value, which determines whether the server will operates under a UNIX INET daemon (inetd). Default is FALSE.

`char logfile[ODR_MAXNAME+1]`

  File for diagnostic output ("": stderr).

`char apdufile[ODR_MAXNAME+1]`

  Name of file for logging incoming and outgoing APDUs ("": don't log APDUs, "-": `stderr`).

`char default_listen[1024]`

  Same form as the command-line specification of listener address. "": no default listener address. Default is to listen at "tcp:@:9999". You can only specify one default listener address in this fashion.

`enum oid_proto default_proto;`

  Either `PROTO_Z3950` or `PROTO_SR`. Default is `PROTO_Z39_50`.

`int idle_timeout;`

  Maximum session idle-time, in minutes. Zero indicates no (infinite) timeout. Default is 15 minutes.

`int maxrecordsize;`

  Maximum permissible record (message) size. Default is 64 MB. This amount of memory will only be allocated if a client requests a very large amount of records in one operation (or a big record). Set it to a lower number if you are worried about resource consumption on your host system.

`char configname[ODR_MAXNAME+1]`

  Passed to the backend when a new connection is received.

`char setuid[ODR_MAXNAME+1]`

  Set user id to the user specified, after binding the listener addresses.

`void (*bend_start)(struct statserv_options_block *p)`

  Pointer to function which is called after the command line options have been parsed - but before the server starts listening. For forked UNIX servers this handler is called in the mother process; for threaded servers this handler is called in the main thread. The default value of this pointer is NULL in which case it isn't invoked by the frontend server. When the server operates as an NT service this handler is called whenever the service is started.

`void (*bend_stop)(struct statserv_options_block *p)`

  Pointer to function which is called whenever the server has stopped listening for incoming connections. This function pointer has a default value of NULL in which case it isn't called. When the server operates as an NT service this handler is called whenever the service is stopped.

`void *handle`

  User defined pointer (default value NULL). This is a per-server handle that can be used to specify "user-data". Do not confuse this with the session-handle as returned by bend_init.

The pointer returned by `statserv_getcontrol` points to a static area. You are allowed to change the contents of the structure, but the changes will not take effect before you call

    void statserv_setcontrol(statserv_options_block *block);

### Note

that you should generally update this structure before calling `statserv_main()`.

Prev | Up | Next
3. The Backend API | Home | 5. The Backend Functions