Changeset 99318ad for win32.c


Ignore:
Timestamp:
2005-11-07T16:32:40Z (18 years ago)
Author:
Jelmer Vernooij <jelmer@…>
Branches:
master
Children:
abe53d3
Parents:
d1d6776
Message:

Import work on services-based Win32 port

File:
1 edited

Legend:

Unmodified
Added
Removed
  • win32.c

    rd1d6776 r99318ad  
    55  \********************************************************************/
    66
    7 /* Main file (Unix specific part)                                       */
     7/* Main file (Windows specific part)                                   */
    88
    99/*
     
    2424*/
    2525
     26#define BITLBEE_CORE
    2627#include "bitlbee.h"
    2728#include "commands.h"
     
    3031#include "help.h"
    3132#include <signal.h>
    32 #include <unistd.h>
    33 #include <sys/time.h>
    34 #include <winreg.h>
    35 #include <winbase.h>
     33#include <windows.h>
    3634
    3735global_t global;        /* Against global namespace pollution */
    3836
    39 int main( int argc, char *argv[] )
     37static void WINAPI service_ctrl (DWORD dwControl)
     38{
     39        switch (dwControl)
     40        {
     41        case SERVICE_CONTROL_STOP:
     42                        /* FIXME */
     43            break;
     44
     45        case SERVICE_CONTROL_INTERROGATE:
     46            break;
     47
     48        default:
     49            break;
     50
     51    }
     52}
     53
     54void service_main (DWORD argc, LPTSTR *argv)
    4055{
    4156        int i = -1;
     57        SERVICE_STATUS_HANDLE handle;
     58        SERVICE_STATUS status;
     59
     60    handle = RegisterServiceCtrlHandler("bitlbee", service_ctrl);
     61
     62    if (!handle)
     63                return;
     64
     65    status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
     66    status.dwServiceSpecificExitCode = 0;
     67
    4268        memset( &global, 0, sizeof( global_t ) );
    4369       
    4470        global.loop = g_main_new( FALSE );
    45        
    46         log_init( );
    47         nogaim_init( );
    4871       
    4972        global.conf = conf_load( argc, argv );
    5073        if( global.conf == NULL )
    51                 return( 1 );
     74                return;
    5275       
    5376        if( global.conf->runmode == RUNMODE_INETD )
     
    6891       
    6992        if( i != 0 )
    70                 return( i );
     93                return;
    7194       
    7295        if( access( global.conf->configdir, F_OK ) != 0 )
    7396                log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", global.conf->configdir );
    74         else if( access( global.conf->configdir, R_OK ) != 0 || access( global.conf->configdir, W_OK ) != 0 )
     97        else if( access( global.conf->configdir, 06 ) != 0 )
    7598                log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir );
    7699        if( help_init( &(global.help) ) == NULL )
    77100                log_message( LOGLVL_WARNING, "Error opening helpfile %s.", global.helpfile );
     101
     102        SetServiceStatus(handle, &status);
    78103       
    79104        g_main_run( global.loop );
    80        
    81         return( 0 );
     105}
     106
     107int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
     108{
     109    SERVICE_TABLE_ENTRY dispatch_table[] =
     110    {
     111        { TEXT("bitlbee"), (LPSERVICE_MAIN_FUNCTION)service_main },
     112        { NULL, NULL }
     113    };
     114
     115        nogaim_init( );
     116
     117    if (!StartServiceCtrlDispatcher(dispatch_table))
     118                log_message( LOGLVL_ERROR, "StartServiceCtrlDispatcher failed.");
     119       
     120        return 0;
    82121}
    83122
     
    119158        HKEY key, key_main, key_proxy;
    120159        char *tmp;
    121         RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Bitlbee", &key);
    122         RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Bitlbee\\main", &key_main);
    123         RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Bitlbee\\proxy", &key_proxy);
     160
     161        RegOpenKey(HKEY_CURRENT_USER, "SOFTWARE\\Bitlbee", &key);
     162        RegOpenKey(key, "main", &key_main);
     163        RegOpenKey(key, "proxy", &key_proxy);
    124164       
    125165        memset( &global, 0, sizeof( global_t ) );
     
    190230  return (addr->s_addr == INADDR_NONE) ? 0 : 1;
    191231}
     232
     233void log_error(char *msg)
     234{
     235        log_message(LOGLVL_ERROR, "%s", msg);
     236}
     237
     238void log_message(int level, char *message, ...)
     239{
     240    HANDLE  hEventSource;
     241    LPTSTR  lpszStrings[2];
     242        WORD elevel;
     243    va_list ap;
     244
     245    va_start(ap, message);
     246
     247    hEventSource = RegisterEventSource(NULL, TEXT("bitlbee"));
     248
     249    lpszStrings[0] = TEXT("bitlbee");
     250    lpszStrings[1] = g_strdup_vprintf(message, ap);
     251    va_end(ap);
     252
     253        switch (level) {
     254        case LOGLVL_ERROR: elevel = EVENTLOG_ERROR_TYPE; break;
     255        case LOGLVL_WARNING: elevel = EVENTLOG_WARNING_TYPE; break;
     256        case LOGLVL_INFO: elevel = EVENTLOG_INFORMATION_TYPE; break;
     257#ifdef DEBUG
     258        case LOGLVL_DEBUG: elevel = EVENTLOG_AUDIT_SUCCESS; break;
     259#endif
     260        }
     261
     262    if (hEventSource != NULL) {
     263        ReportEvent(hEventSource,
     264        elevel,
     265        0,                   
     266        0,                   
     267        NULL,                 
     268        2,                   
     269        0,                   
     270        lpszStrings,         
     271        NULL);               
     272
     273        DeregisterEventSource(hEventSource);
     274    }
     275
     276        g_free(lpszStrings[1]);
     277}
Note: See TracChangeset for help on using the changeset viewer.