Changeset 78


Ignore:
Timestamp:
08/17/10 10:23:57 (18 months ago)
Author:
jay
Message:

-- win32service application with start, stop, uninstall service, and start server normally integrated into one package. This is the initial commit for this approach.

Location:
trunk/sources/win32service
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/win32service/svcconfig/src/svcconfig.cpp

    r73 r78  
    33#include <strsafe.h> 
    44#include <stdio.h> 
     5#include "svcconfig.h" 
    56 
    67#pragma comment(lib, "advapi32.lib") 
    78 
    8 TCHAR szCommand[10]; 
    9 TCHAR szSvcName[80]; 
    10  
    11 VOID __stdcall DisplayUsage(void); 
    12  
    13 VOID __stdcall DoQuerySvc(void); 
    14 VOID __stdcall DoUpdateSvcDesc(void); 
    15 VOID __stdcall DoDisableSvc(void); 
    16 VOID __stdcall DoEnableSvc(void); 
    17 VOID __stdcall DoDeleteSvc(void); 
     9TCHAR szConfigCommand[TEXT_SIZE]; 
     10TCHAR szConfigSvcName[TEXT_SIZE]; 
     11 
    1812 
    1913// 
     
    2721//   None 
    2822// 
    29 void __cdecl _tmain(int argc, TCHAR *argv[]) 
    30 { 
     23//void __cdecl _tmain(int argc, TCHAR *argv[]) 
     24bool svcconfig (TCHAR * argv[]) 
     25{ 
     26        bool result = true; 
    3127    printf("\n"); 
     28        /* 
    3229    if( argc != 3 ) 
    3330    { 
     
    3633        return; 
    3734    } 
    38  
    39     StringCchCopy(szCommand, 10, argv[1]); 
    40     StringCchCopy(szSvcName, 80, argv[2]); 
    41  
    42     if (lstrcmpi( szCommand, TEXT("query")) == 0 ) 
    43         DoQuerySvc(); 
    44     else if (lstrcmpi( szCommand, TEXT("describe")) == 0 ) 
     35        */ 
     36    StringCchCopy(szConfigCommand, TEXT_SIZE, argv[1]); 
     37    StringCchCopy(szConfigSvcName, TEXT_SIZE, argv[2]); 
     38 
     39    if (lstrcmpi( szConfigCommand, TEXT("query")) == 0 ) 
     40        result = DoQuerySvc(); 
     41    else if (lstrcmpi( szConfigCommand, TEXT("describe")) == 0 ) 
    4542        DoUpdateSvcDesc(); 
    46     else if (lstrcmpi( szCommand, TEXT("disable")) == 0 ) 
     43    else if (lstrcmpi( szConfigCommand, TEXT("disable")) == 0 ) 
    4744        DoDisableSvc(); 
    48     else if (lstrcmpi( szCommand, TEXT("enable")) == 0 ) 
     45    else if (lstrcmpi( szConfigCommand, TEXT("enable")) == 0 ) 
    4946        DoEnableSvc(); 
    50     else if (lstrcmpi( szCommand, TEXT("delete")) == 0 ) 
     47    else if (lstrcmpi( szConfigCommand, TEXT("uninstallService")) == 0 ) 
    5148        DoDeleteSvc(); 
    5249    else  
    5350    { 
    54         _tprintf(TEXT("Unknown command (%s)\n\n"), szCommand); 
    55         DisplayUsage(); 
    56     } 
    57 } 
    58  
     51        _tprintf(TEXT("Unknown command (%s)\n\n"), szConfigCommand); 
     52        //DisplayUsage(); 
     53    } 
     54 
     55        return result; 
     56} 
     57/* 
    5958VOID __stdcall DisplayUsage() 
    6059{ 
     
    7069    printf("\t  delete\n"); 
    7170} 
    72  
     71*/ 
    7372// 
    7473// Purpose:  
     
    8180//   None 
    8281// 
    83 VOID __stdcall DoQuerySvc() 
     82bool __stdcall DoQuerySvc() 
    8483{ 
    8584    SC_HANDLE schSCManager; 
     
    9998    { 
    10099        printf("OpenSCManager failed (%d)\n", GetLastError()); 
    101         return; 
     100        return false; 
    102101    } 
    103102 
     
    106105    schService = OpenService(  
    107106        schSCManager,          // SCM database  
    108         szSvcName,             // name of service  
     107        szConfigSvcName,             // name of service  
    109108        SERVICE_QUERY_CONFIG); // need query config access  
    110109  
     
    113112        printf("OpenService failed (%d)\n", GetLastError());  
    114113        CloseServiceHandle(schSCManager); 
    115         return; 
     114        return false; 
    116115    } 
    117116 
     
    133132        { 
    134133            printf("QueryServiceConfig failed (%d)", dwError); 
     134                        return false; 
    135135            goto cleanup;  
    136136        } 
     
    144144    { 
    145145        printf("QueryServiceConfig failed (%d)", GetLastError()); 
     146                return false; 
    146147        goto cleanup; 
    147148    } 
     
    163164        { 
    164165            printf("QueryServiceConfig2 failed (%d)", dwError); 
     166                        return false; 
    165167            goto cleanup;  
    166168        } 
     
    175177    { 
    176178        printf("QueryServiceConfig2 failed (%d)", GetLastError()); 
     179                return false; 
    177180        goto cleanup; 
    178181    } 
     
    180183    // Print the configuration information. 
    181184  
    182     _tprintf(TEXT("%s configuration: \n"), szSvcName); 
     185    _tprintf(TEXT("%s configuration: \n"), szConfigSvcName); 
    183186    _tprintf(TEXT("  Type: 0x%x\n"), lpsc->dwServiceType); 
    184187    _tprintf(TEXT("  Start Type: 0x%x\n"), lpsc->dwStartType); 
     
    198201    LocalFree(lpsc);  
    199202    LocalFree(lpsd); 
    200  
     203        return true; 
    201204cleanup: 
    202205    CloseServiceHandle(schService);  
     
    236239    schService = OpenService(  
    237240        schSCManager,            // SCM database  
    238         szSvcName,               // name of service  
     241        szConfigSvcName,               // name of service  
    239242        SERVICE_CHANGE_CONFIG);  // need change config access  
    240243  
     
    301304    schService = OpenService(  
    302305        schSCManager,            // SCM database  
    303         szSvcName,               // name of service  
     306        szConfigSvcName,               // name of service  
    304307        SERVICE_CHANGE_CONFIG);  // need change config access  
    305308  
     
    368371    schService = OpenService(  
    369372        schSCManager,            // SCM database  
    370         szSvcName,               // name of service  
     373        szConfigSvcName,               // name of service  
    371374        SERVICE_CHANGE_CONFIG);  // need change config access  
    372375  
     
    428431    schService = OpenService(  
    429432        schSCManager,       // SCM database  
    430         szSvcName,          // name of service  
     433        szConfigSvcName,          // name of service  
    431434        DELETE);            // need delete access  
    432435  
  • trunk/sources/win32service/svccontrol/src/svccontrol.cpp

    r73 r78  
    44#include <aclapi.h> 
    55#include <stdio.h> 
    6  
    7 #pragma comment(lib, "advapi32.lib") 
    8  
    9 TCHAR szCommand[10]; 
    10 TCHAR szSvcName[80]; 
     6#include "svccontrol.h" 
     7 
     8TCHAR szControlCommand[TEXT_SIZE]; 
     9TCHAR szControlSvcName[TEXT_SIZE]; 
    1110 
    1211SC_HANDLE schSCManager; 
    1312SC_HANDLE schService; 
    1413 
    15 VOID __stdcall DisplayUsage(void); 
    16  
    17 VOID __stdcall DoStartSvc(void); 
    18 VOID __stdcall DoUpdateSvcDacl(void); 
    19 VOID __stdcall DoStopSvc(void); 
    20  
    21 BOOL __stdcall StopDependentServices(void); 
     14#pragma comment(lib, "advapi32.lib") 
     15 
    2216 
    2317// 
     
    3125//   None 
    3226// 
    33 void _tmain(int argc, TCHAR *argv[]) 
     27//void _tmain(int argc, TCHAR *argv[]) 
     28void svccontrol(TCHAR * argv[]) 
    3429{ 
    35     printf("\n"); 
     30    /*printf("\n"); 
     31         
    3632    if( argc != 3 ) 
    3733    { 
     
    4036        return; 
    4137    } 
    42  
    43     StringCchCopy(szCommand, 10, argv[1]); 
    44     StringCchCopy(szSvcName, 80, argv[2]); 
    45  
    46     if (lstrcmpi( szCommand, TEXT("start")) == 0 ) 
     38        */ 
     39    StringCchCopy(szControlCommand, TEXT_SIZE, argv[1]); 
     40    StringCchCopy(szControlSvcName, TEXT_SIZE, argv[2]); 
     41 
     42    if (lstrcmpi( szControlCommand, TEXT("startService")) == 0 ) 
    4743        DoStartSvc(); 
    48     else if (lstrcmpi( szCommand, TEXT("dacl")) == 0 ) 
     44    else if (lstrcmpi( szControlCommand, TEXT("dacl")) == 0 ) 
    4945        DoUpdateSvcDacl(); 
    50     else if (lstrcmpi( szCommand, TEXT("stop")) == 0 ) 
     46    else if (lstrcmpi( szControlCommand, TEXT("stopService")) == 0 ) 
    5147        DoStopSvc(); 
    5248    else  
    5349    { 
    54         _tprintf(TEXT("Unknown command (%s)\n\n"), szCommand); 
    55         DisplayUsage(); 
     50        _tprintf(TEXT("Unknown command (%s)\n\n"), szControlCommand); 
     51       // DisplayUsage(); 
    5652    } 
    5753} 
    58  
     54/* 
    5955VOID __stdcall DisplayUsage() 
    6056{ 
     
    6864    printf("\t  stop\n"); 
    6965} 
    70  
     66*/ 
    7167// 
    7268// Purpose:  
     
    104100    schService = OpenService(  
    105101        schSCManager,         // SCM database  
    106         szSvcName,            // name of service  
     102        szControlSvcName,            // name of service  
    107103        SERVICE_ALL_ACCESS);  // full access  
    108104  
     
    336332    schService = OpenService(  
    337333        schSCManager,              // SCManager database  
    338         szSvcName,                 // name of service  
     334        szControlSvcName,                 // name of service  
    339335        READ_CONTROL | WRITE_DAC); // access 
    340336  
     
    474470    schService = OpenService(  
    475471        schSCManager,         // SCM database  
    476         szSvcName,            // name of service  
     472        szControlSvcName,            // name of service  
    477473        SERVICE_STOP |  
    478474        SERVICE_QUERY_STATUS |  
  • trunk/sources/win32service/win32service/include/svc/svc.h

    r73 r78  
    1515VOID SvcInit( DWORD, LPTSTR * );  
    1616VOID SvcReportEvent( LPTSTR ); 
     17VOID __stdcall DisplayUsage(); 
     18VOID AddSvcCommand(TCHAR * command); 
    1719 
    1820DWORD WINAPI ThreadProc( __in  LPVOID lpParameter); 
     
    2123 
    2224HANDLE hThread = NULL; 
     25TCHAR * svcCommand[3]; 
  • trunk/sources/win32service/win32service/readme.txt

    r73 r78  
    33Source: http://msdn.microsoft.com/en-us/library/bb540476%28v=VS.85%29.aspx 
    44 
    5 Brief Description: 
    6 win32service is a win32 console application that installs a service named "evo".  
    7 This service can be started and stopped using the Services Administrative Tool seen from the Control Panel. 
     5BRIEF DESCRIPTION: 
     6win32service is a win32 console application that runs the following command: 
     71. Installs a Windows service named "evo" 
     82. Uninstalls the service named "evo" 
     93. Starts the service 
     104. Stops the service 
     115. Starts the rtmpserver normally in the console. 
     12In addition, the service can also be started and stopped using the Services Administrative Tool seen from the Control Panel. 
    813 
    9 Additional Applications: 
    10 There are other applications outside win32service that can be useful in testing: 
    11 1. SvcControl - is a console application that can start and stop the service. More commands can be done here (refer to Source) 
    12 2. SvcConfig - is a consol application that can be used to delete the service. More commands can be done here (refer to Source) 
     14MODULES: 
     151. svcconfig - contains the function that deletes/uninstalls the service. This is also where the application checks if a service is already installed. 
     162. svccontrol - contains the functions that starts and stops the service. 
     173. svc - contains the "main" function. When the service is created, the SvcMain acts as the main function of the thread when the service starts. 
     184. rtmpserver - is the application that runs the server. 
    1319 
    14 Instruction (How to use?): 
     20Instruction (How to use): 
    1521To start/stop service: 
    16221. Compile the application. 
    17232. Open a command prompt as administrator (right click on command prompt, select "Run as Administrator"). 
    18243. cd to directory of win32service. 
    19 4. Type "win32service install" and wait for "Service installed successfuly" to appear. 
     254. Type "win32service installService" and wait for "Service installed successfuly" to appear. 
    20265. Check the Service admin tool and look for "evo" service. 
    21276. Start/Stop evo service. 
    2228 
    23 To delete the service: compile SvcConfig, cd to the dir of svcconfig.exe, type "svcconfig delete evo". 
    24  
    25 To start/stop rtmpserver (Will be improved soon) 
     29To start/stop rtmpserver 
    2630rtmpserver should start when evo service starts. To make sure that it starts successfuly: 
    2731- Put the exact path of rtmpserver.lua in line#-84 of rtmpserver.cpp. 
    2832- Assign a "mediaFolder" parameter in rtmpserver.lua 
    29 Stop the evo service to stop rtmpserver. 
     33Start the server by starting the service using the 'startService' command. 
     34Stop the server by stopping the service using the 'stopService' command. 
     35If the service is not installed, start the server normally by running the application without any command parameter. 
     36 
     37To uninstall the service, enter the 'uninstallService' command. 
     38 
     39SOME TECHNICAL DETAILS: 
     40- The application makes sure that when the service is installed, the user will not be able to start the server normally. 
     41- To check if the service is installed, the application queries information about the service. If the query returns a value, the server will not be started normally. 
     42- Running the application without any command assumes that the user is attempting to start the server in a normal way. 
     43- svcconfig and svccontrol have other functions that can be useful during testing and in future implementations, hence a lot of codes remain intact. 
    3044 
    3145NOTES: 
     46- [as of commit in 08-17-2010] The config file is hard-coded in rtmpserver.cpp 
    3247- Currently, the rtmpserver logs is saved to C:\\RTMPServerlogs.txt. 
    3348- There are limited sample applications in the net that implements this model.  
    34 - In most of the sample apps, they loop a "while()" during the service's RUNNING state, simulating a running thread (since SvcMain acts like a thread). 
     49- In most of the sample apps, they loop a "while()" during the service's RUNNING state, simulating a running thread (since SvcMain acts as a thread). 
  • trunk/sources/win32service/win32service/src/svc/svc.cpp

    r73 r78  
    55#include "svc/Svc.h" 
    66#include "rtmpserver/rtmpserver.h" 
     7#include "svccontrol.h" 
     8#include "svcconfig.h" 
    79 
    810#pragma comment(lib, "advapi32.lib") 
     
    2325void __cdecl _tmain(int argc, TCHAR *argv[])  
    2426{  
    25     // If command-line parameter is "install", install the service.  
    26     // Otherwise, the service is probably being started by the SCM. 
    27  
    28     if( lstrcmpi( argv[1], TEXT("install")) == 0 ) 
    29     { 
    30         SvcInstall(); 
    31         return; 
    32     } 
    33  
    34     // TO_DO: Add any additional services for the process to this table. 
     27         
     28        // Install/uninstall the service.  
     29    if( lstrcmpi( argv[1], TEXT("installService")) == 0 ) 
     30    { 
     31                SvcInstall(); 
     32        return; 
     33    }else if( lstrcmpi( argv[1], TEXT("uninstallService")) == 0) 
     34    { 
     35                AddSvcCommand(argv[1]); 
     36                bool result = svcconfig(svcCommand);//Delete service 
     37        return; 
     38    }  
     39         // TO_DO: Add any additional services for the process to this table. 
    3540    SERVICE_TABLE_ENTRY DispatchTable[] =  
    3641    {  
     
    4651        SvcReportEvent(TEXT("StartServiceCtrlDispatcher"));  
    4752    }  
    48 }  
    49  
     53 
     54        // Control the service 
     55        if( lstrcmpi( argv[1], TEXT("startService")) == 0 || lstrcmpi( argv[1], TEXT("stopService")) == 0) 
     56    { 
     57        AddSvcCommand(argv[1]); 
     58                svccontrol(svcCommand);//Start/Stop service 
     59        return; 
     60    } else { 
     61                if ( argc == 1 ) //if no argument, assume that rtmpserver is started normally 
     62                { 
     63                        AddSvcCommand(TEXT("query")); 
     64                        if (!svcconfig(svcCommand)){ 
     65                                printf("Server will start normally.\n\n"); 
     66                                rtmpserver(); 
     67                                return; 
     68                        }else{ 
     69                                printf("\nERROR: Server cannot be started normally.\n\n"); 
     70                                printf("Service described above is already installed.\n"); 
     71                                printf("Start/Stop the server by starting/stopping the service.\n\n"); 
     72                                printf("To start server normally, enter 'uninstallService' command to delete the service.\n\n"); 
     73                                return; 
     74                        } 
     75                } 
     76        } 
     77 
     78} 
     79 
     80//Populates the svcCommand array 
     81VOID AddSvcCommand(TCHAR * command) 
     82{ 
     83        svcCommand[0] = TEXT(""); 
     84        svcCommand[1] = command; 
     85        svcCommand[2] = SVCNAME; 
     86} 
     87 
     88//Displays usage 
     89VOID __stdcall DisplayUsage() 
     90{ 
     91    printf("Description:\n"); 
     92    printf("\tRun Evostream server as stand-alone or as a service.\n\n"); 
     93    printf("Usage:\n"); 
     94        printf("Install Service:\n"); 
     95        printf("\twin32service installService\n\n"); 
     96        printf("Uninstall Service:\n"); 
     97        printf("\twin32service uninstallService\n\n"); 
     98        printf("Start/Stop Service:\n"); 
     99    printf("\twin32service [command]\n"); 
     100    printf("\t[command]\n"); 
     101    printf("\t  startService\n"); 
     102    printf("\t  stopService\n"); 
     103        printf("Run as stand-alone:\n"); 
     104        printf("\twin32service <configfile path>\n"); 
     105        printf("\twin32service --use-implicit-console-appender\n"); 
     106} 
    50107// 
    51108// Purpose:  
     
    110167    CloseServiceHandle(schService);  
    111168    CloseServiceHandle(schSCManager); 
    112  
    113169/* The lines below works fine. Uncomment them to run rtmpserver upon installing of the service */ 
    114170//      if(Initialize()) 
Note: See TracChangeset for help on using the changeset viewer.