win_svc.c File Reference

This file contains functions to integrate with the windows services API. More...

#include "config.h"
#include "winrc/win_svc.h"
#include "winrc/w_inst.h"
#include "daemon/daemon.h"
#include "daemon/worker.h"
#include "daemon/remote.h"
#include "util/config_file.h"
#include "util/netevent.h"
#include "util/ub_event.h"
#include "util/net_help.h"

Macros

#define ATTR_UNUSED(x)   x
 

Functions

static void report_status (DWORD state, DWORD exitcode, DWORD wait)
 Report current service status to service control manager. More...
 
static void hdlr (DWORD ctrl)
 Service control handler. More...
 
static void reportev (const char *str)
 report event to system event log For use during startup and shutdown. More...
 
static char * lookup_reg_str (const char *key, const char *name)
 Obtain registry string (if it exists). More...
 
static int lookup_reg_int (const char *key, const char *name)
 Obtain registry integer (if it exists). More...
 
static void waitforubanchor (PROCESS_INFORMATION *pinfo)
 wait for unbound-anchor process to finish
 
static void call_root_update (void)
 Perform root anchor update if so configured, by calling that process.
 
static int service_init (int r, struct daemon **d, struct config_file **c)
 Init service. More...
 
static void service_deinit (struct daemon *daemon, struct config_file *cfg)
 Deinit the service.
 
static void service_main (DWORD argc, LPTSTR *argv)
 The main function for the service. More...
 
static void service_start (const char *cfgfile, int v, int c)
 start the service
 
void wsvc_command_option (const char *wopt, const char *cfgfile, int v, int c)
 Handle commandline service for windows. More...
 
void worker_win_stop_cb (int fd, short ev, void *arg)
 windows worker stop event callback handler
 
static void waitforit (PROCESS_INFORMATION *pinfo)
 wait for cron process to finish
 
static void * win_do_cron (void *arg)
 Do the cron action and wait for result exit value.
 
static void set_cron_timer (void)
 Set the timer for cron for the next wake up.
 
void wsvc_cron_cb (void *arg)
 windows cron timer callback handler
 
void wsvc_setup_worker (struct worker *worker)
 Setup lead worker events. More...
 
void wsvc_desetup_worker (struct worker *worker)
 Desetup lead worker events. More...
 

Variables

static SERVICE_STATUS service_status
 global service status
 
static SERVICE_STATUS_HANDLE service_status_handle
 global service status handle
 
static WSAEVENT service_stop_event = NULL
 global service stop event
 
static struct ub_eventservice_stop_ev = NULL
 event struct for stop callbacks
 
static int service_stop_shutdown = 0
 if stop even means shutdown or restart
 
static char * service_cfgfile = CONFIGFILE
 config file to open. More...
 
static int service_cmdline_verbose = 0
 commandline verbosity. More...
 
static struct comm_timerservice_cron = NULL
 the cron callback
 
static ub_thread_type cron_thread = NULL
 the cron thread
 
static int cron_was_quick = 0
 if cron has already done its quick check
 

Detailed Description

This file contains functions to integrate with the windows services API.

This means it handles the commandline switches to install and remove the service (via CreateService and DeleteService), it handles the ServiceMain() main service entry point when started as a service, and it handles the Handler[_ex]() to process requests to the service (such as start and stop and status).

Function Documentation

◆ report_status()

static void report_status ( DWORD  state,
DWORD  exitcode,
DWORD  wait 
)
static

Report current service status to service control manager.

Parameters
statecurrent state
exitcodeerror code (when stopped)
waitpending operation estimated time in milliseconds.

References service_status, and service_status_handle.

Referenced by call_root_update(), hdlr(), and waitforubanchor().

◆ hdlr()

static void hdlr ( DWORD  ctrl)
static

Service control handler.

Called by serviceControlManager when a control code is sent to the service (with ControlService).

Parameters
ctrlcontrol code

References log_err(), report_status(), service_status, service_stop_event, and service_stop_shutdown.

◆ reportev()

static void reportev ( const char *  str)
static

report event to system event log For use during startup and shutdown.

Parameters
strthe error

Referenced by lookup_reg_int(), and lookup_reg_str().

◆ lookup_reg_str()

static char* lookup_reg_str ( const char *  key,
const char *  name 
)
static

Obtain registry string (if it exists).

Parameters
keykey string
namename of value to fetch.
Returns
malloced string with the result or NULL if it did not exist on an error (logged) was encountered.

References reportev().

Referenced by call_root_update(), and service_init().

◆ lookup_reg_int()

static int lookup_reg_int ( const char *  key,
const char *  name 
)
static

Obtain registry integer (if it exists).

Parameters
keykey string
namename of value to fetch.
Returns
integer value (if it exists), or 0 on error.

References reportev().

Referenced by set_cron_timer().

◆ service_init()

static int service_init ( int  r,
struct daemon **  d,
struct config_file **  c 
)
static

Init service.

Keeps calling status pending to tell service control manager that this process is not hanging.

Parameters
rrestart, true on restart
ddaemon returned here.
cconfig file returned here.
Returns
false if failed.

References lookup_reg_str(), and service_cfgfile.

◆ service_main()

static void service_main ( DWORD  argc,
LPTSTR *  argv 
)
static

The main function for the service.

Called by the services API when starting unbound on windows in background. Arguments could have been present in the string 'path'.

Parameters
argcnr args
argvarg text.

Referenced by service_start().

◆ wsvc_command_option()

void wsvc_command_option ( const char *  wopt,
const char *  cfgfile,
int  v,
int  c 
)

Handle commandline service for windows.

Parameters
woptwindows option string (install, remove, service).
cfgfileconfigfile to open (default or passed with -c).
vamount of commandline verbosity added with -v.
ctrue if cfgfile was set by commandline -c option.

References service_start(), wsvc_install(), wsvc_rc_start(), wsvc_rc_stop(), and wsvc_remove().

Referenced by main().

◆ wsvc_setup_worker()

void wsvc_setup_worker ( struct worker worker)

Setup lead worker events.

Parameters
workerthe worker

References service_stop_ev, service_stop_event, and ub_winsock_register_wsaevent().

◆ wsvc_desetup_worker()

void wsvc_desetup_worker ( struct worker worker)

Desetup lead worker events.

Parameters
workerthe worker

Referenced by worker_delete().

Variable Documentation

◆ service_cfgfile

char* service_cfgfile = CONFIGFILE
static

config file to open.

global communication to service_main()

Referenced by service_init(), and service_start().

◆ service_cmdline_verbose

int service_cmdline_verbose = 0
static

commandline verbosity.

global communication to service_main()