Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

svc.c

Go to the documentation of this file.
00001 /*Tests all of the svcctl calls (at least at time of writing)*/
00002 
00003 #include "libmsrpc.h"
00004 #include "test_util.h"
00005 
00006 int main(int argc, char **argv) {
00007    CacServerHandle *hnd = NULL;
00008    TALLOC_CTX *mem_ctx = NULL;
00009             
00010 
00011    struct SvcOpenScm      sos;
00012    struct SvcEnumServices es;
00013    struct SvcOpenService  os;
00014    struct SvcGetStatus    gs;
00015    struct SvcStartService start;
00016    struct SvcStopService  stop;
00017    struct SvcPauseService pause;
00018    struct SvcContinueService res;
00019    struct SvcGetDisplayName gdn;
00020    struct SvcGetServiceConfig sgc;
00021   
00022    POLICY_HND *svc_hnd = NULL;
00023 
00024    fstring tmp;
00025    fstring input;
00026    
00027    int i;
00028 
00029    mem_ctx = talloc_init("cac_samgroup");
00030 
00031    hnd = cac_NewServerHandle(True);
00032 
00033    cac_SetAuthDataFn(hnd, cactest_GetAuthDataFn);
00034 
00035    cac_parse_cmd_line(argc, argv, hnd);
00036 
00037    if(!cac_Connect(hnd, NULL)) {
00038       fprintf(stderr, "Could not connect to server %s. Error: %s\n", hnd->server, nt_errstr(hnd->status));
00039       exit(-1);
00040    }
00041 
00042    /*open a handle to the scm*/
00043    ZERO_STRUCT(sos);
00044 
00045    sos.in.access = SC_MANAGER_ALL_ACCESS;
00046 
00047    if(!cac_SvcOpenScm(hnd, mem_ctx, &sos)) {
00048       fprintf(stderr, "Could not open SCM. Error: %s\n", nt_errstr(hnd->status));
00049       goto done;
00050    }
00051 
00052    printf("Opened SCM\n");
00053 
00054    tmp[0] = 0x00;
00055    while(tmp[0] != 'q') {
00056       printf("\n");
00057       printf("[e] Enum Services\n");
00058       printf("[o] Open Service\n");
00059       printf("[x] Close Service\n");
00060       printf("[g] Get service status\n");
00061       printf("[s] Start service\n");
00062       printf("[t] Stop service\n");
00063       printf("[p] Pause service\n");
00064       printf("[r] Resume service\n");
00065       printf("[c] Get service config\n");
00066 
00067       printf("[d] Get display name\n");
00068 
00069       printf("[q]uit\n\n");
00070       printf("Enter option: ");
00071       cactest_readline(stdin, tmp);
00072 
00073       printf("\n");
00074 
00075       switch(tmp[0]) {
00076          case 'e': /*enum services*/
00077             ZERO_STRUCT(es);
00078             es.in.scm_hnd = sos.out.scm_hnd;
00079 
00080             if(!cac_SvcEnumServices(hnd, mem_ctx, &es)) {
00081                printf("Could not enumerate services. Error: %s\n", nt_errstr(hnd->status));
00082                break;
00083             }
00084 
00085             for(i = 0; i < es.out.num_services; i++) {
00086                print_cac_service(es.out.services[i]);
00087             }
00088             printf("Enumerated %d services:\n", es.out.num_services);
00089 
00090             break;
00091 
00092          case 'o': /*Open service*/
00093             ZERO_STRUCT(os);
00094 
00095             printf("Enter service name: ");
00096             cactest_readline(stdin, tmp);
00097 
00098             os.in.name = talloc_strdup(mem_ctx, tmp);
00099             os.in.scm_hnd = sos.out.scm_hnd;
00100             os.in.access = SERVICE_ALL_ACCESS;
00101 
00102             if(!cac_SvcOpenService(hnd, mem_ctx, &os)) {
00103                printf("Could not open service. Error: %s\n", nt_errstr(hnd->status));
00104                break;
00105             }
00106             
00107             printf("Opened service.\n");
00108             svc_hnd = os.out.svc_hnd;
00109 
00110             break;
00111          case 'x': /*close service*/
00112             if(!svc_hnd) {
00113                printf("Must open service first!\n");
00114                break;
00115             }
00116 
00117             cac_SvcClose(hnd, mem_ctx, svc_hnd);
00118             svc_hnd = NULL;
00119             break;
00120          case 'g': /*get svc status*/
00121             
00122             if(!svc_hnd) {
00123                printf("Must open service first!\n");
00124                break;
00125             }
00126 
00127             ZERO_STRUCT(gs);
00128 
00129             gs.in.svc_hnd = svc_hnd;
00130 
00131             if(!cac_SvcGetStatus(hnd, mem_ctx, &gs)) {
00132                printf("Could not get status. Error: %s\n", nt_errstr(hnd->status));
00133                break;
00134             }
00135 
00136             print_service_status(gs.out.status);
00137             break;
00138          case 's': /*start service*/
00139             if(!svc_hnd) {
00140                printf("Must open service first!\n");
00141                break;
00142             }
00143 
00144             ZERO_STRUCT(start);
00145 
00146             start.in.svc_hnd = svc_hnd;
00147 
00148             printf("Enter number of parameters: ");
00149             scanf("%d", &start.in.num_parms);
00150 
00151             start.in.parms = talloc_array(mem_ctx, char *, start.in.num_parms);
00152 
00153             for(i = 0; i < start.in.num_parms; i++) {
00154                printf("Parm %d: ", i);
00155                cactest_readline(stdin, tmp);
00156                start.in.parms[i] = talloc_strdup(mem_ctx, tmp);
00157             }
00158 
00159             printf("Timeout (seconds): ");
00160             scanf("%d", &start.in.timeout);
00161 
00162             if(!cac_SvcStartService(hnd, mem_ctx, &start)) {
00163                printf("Could not start service. Error: %s\n", nt_errstr(hnd->status));
00164             }
00165             else {
00166                printf("Started service.\n");
00167             }
00168 
00169             break;
00170          case 't': /*stop service*/
00171             if(!svc_hnd) {
00172                printf("Must open service first!\n");
00173                break;
00174             }
00175 
00176             ZERO_STRUCT(stop);
00177             stop.in.svc_hnd = svc_hnd;
00178 
00179             printf("Timeout (seconds): ");
00180             scanf("%d", &stop.in.timeout);
00181 
00182             if(!cac_SvcStopService(hnd, mem_ctx, &stop)) {
00183                if(CAC_OP_FAILED(hnd->status)) {
00184                   printf("Error occured: %s\n", nt_errstr(hnd->status));
00185                }
00186                else {
00187                   printf("Service was not stopped within %d seconds.\n", stop.in.timeout);
00188                   print_service_status(stop.out.status);
00189                }
00190             }
00191             else {
00192                printf("Done.\n");
00193                print_service_status(stop.out.status);
00194             }
00195             break;
00196          case 'd': /*get display name*/
00197             if(!svc_hnd) {
00198                printf("Must open service first!\n");
00199                break;
00200             }
00201 
00202             ZERO_STRUCT(gdn);
00203             gdn.in.svc_hnd = svc_hnd;
00204 
00205             if(!cac_SvcGetDisplayName(hnd, mem_ctx, &gdn)) {
00206                printf("Could not get display name. Error: %s\n", nt_errstr(hnd->status));
00207             }
00208             else {
00209                printf("\tDisplay Name: %s\n", gdn.out.display_name);
00210             }
00211             break;
00212 
00213          case 'p': /*pause service*/
00214             if(!svc_hnd) {
00215                printf("Must open service first!\n");
00216                break;
00217             }
00218 
00219             ZERO_STRUCT(pause);
00220             pause.in.svc_hnd = svc_hnd;
00221 
00222             printf("Timeout (seconds): ");
00223             scanf("%d", &pause.in.timeout);
00224 
00225             if(!cac_SvcPauseService(hnd, mem_ctx, &pause)) {
00226                if(CAC_OP_FAILED(hnd->status)) {
00227                   printf("Error occured: %s\n", nt_errstr(hnd->status));
00228                }
00229                else {
00230                   printf("Service was not paused within %d seconds.\n", pause.in.timeout);
00231                   print_service_status(pause.out.status);
00232                }
00233             }
00234             else {
00235                printf("Done.\n");
00236                print_service_status(pause.out.status);
00237             }
00238 
00239             break;
00240             
00241          case 'r': /*resume service*/
00242             if(!svc_hnd) {
00243                printf("Must open service first!\n");
00244                break;
00245             }
00246 
00247             ZERO_STRUCT(res);
00248             res.in.svc_hnd = svc_hnd;
00249 
00250             printf("Timeout (seconds): ");
00251             scanf("%d", &res.in.timeout);
00252 
00253             if(!cac_SvcContinueService(hnd, mem_ctx, &res)) {
00254                if(CAC_OP_FAILED(hnd->status)) {
00255                   printf("Error occured: %s\n", nt_errstr(hnd->status));
00256                }
00257                else {
00258                   printf("Service was not resumed within %d seconds.\n", res.in.timeout);
00259                   print_service_status(res.out.status);
00260                }
00261             }
00262             else {
00263                printf("Done.\n");
00264                print_service_status(res.out.status);
00265             }
00266 
00267             break;
00268 
00269          case 'c': /*get service config*/
00270             if(!svc_hnd) {
00271                printf("Must open service first!\n");
00272                break;
00273             }
00274 
00275             ZERO_STRUCT(sgc);
00276 
00277             sgc.in.svc_hnd = svc_hnd;
00278 
00279             if(!cac_SvcGetServiceConfig(hnd, mem_ctx, &sgc)) {
00280                printf("Could not get service config. Error: %s\n", nt_errstr(hnd->status));
00281             }
00282             else {
00283                print_service_config(&sgc.out.config);
00284             }
00285             break;
00286 
00287          case 'q': /*quit*/
00288             break;
00289          default:
00290             printf("Invalid command\n");
00291       }
00292    }
00293 
00294    cac_SvcClose(hnd, mem_ctx, sos.out.scm_hnd);
00295 
00296    done:
00297    cac_FreeHandle(hnd);
00298 
00299    talloc_destroy(mem_ctx);
00300 
00301    return 0;
00302 }
00303 

© sourcejam.com 2005-2008