testing/mozbase/mozprocess/tests/proclaunch.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mozbase/mozprocess/tests/proclaunch.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,156 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <stdio.h>
     1.9 +#include <stdlib.h>
    1.10 +#include "iniparser.h"
    1.11 +
    1.12 +#ifdef _WIN32
    1.13 +#include <windows.h>
    1.14 +#include <tchar.h>
    1.15 +
    1.16 +extern int iniparser_getint(dictionary *d, char *key, int notfound);
    1.17 +extern char *iniparser_getstring(dictionary *d, char *key, char *def);
    1.18 +
    1.19 +// This is the windows launcher function
    1.20 +int launchWindows(int children, int maxtime) {
    1.21 +  _TCHAR cmdline[50];
    1.22 +  STARTUPINFO startup;
    1.23 +  PROCESS_INFORMATION procinfo;
    1.24 +  BOOL rv = 0;
    1.25 +  
    1.26 +  _stprintf(cmdline, _T("proclaunch.exe %d %d"), children, maxtime);
    1.27 +  ZeroMemory(&startup, sizeof(STARTUPINFO));
    1.28 +  startup.cb = sizeof(STARTUPINFO);
    1.29 +  
    1.30 +  ZeroMemory(&procinfo, sizeof(PROCESS_INFORMATION));
    1.31 +  
    1.32 +  printf("Launching process!\n");
    1.33 +  rv = CreateProcess(NULL,
    1.34 +                cmdline,
    1.35 +                NULL,
    1.36 +                NULL,
    1.37 +                FALSE,
    1.38 +                0,
    1.39 +                NULL,
    1.40 +                NULL,
    1.41 +                &startup,
    1.42 +                &procinfo);
    1.43 +
    1.44 +  if (!rv) {
    1.45 +    DWORD dw = GetLastError(); 
    1.46 +    printf("error: %d\n", dw); 
    1.47 +  }
    1.48 +  CloseHandle(procinfo.hProcess);
    1.49 +  CloseHandle(procinfo.hThread);
    1.50 +  return 0;
    1.51 +}
    1.52 +#endif
    1.53 +
    1.54 +int main(int argc, char **argv) {
    1.55 +  int children = 0;
    1.56 +  int maxtime = 0;
    1.57 +  int passedtime = 0;
    1.58 +  dictionary *dict = NULL;
    1.59 +
    1.60 +  // Command line handling
    1.61 +  if (argc == 1 || (0 == strcmp(argv[1], "-h")) || (0 == strcmp(argv[1], "--help"))) {
    1.62 +    printf("ProcLauncher takes an ini file.  Specify the ini file as the only\n");
    1.63 +    printf("parameter of the command line:\n");
    1.64 +    printf("proclauncher my.ini\n\n");
    1.65 +    printf("The ini file has the form:\n");
    1.66 +    printf("[main]\n");
    1.67 +    printf("children=child1,child2  ; These comma separated values are sections\n");
    1.68 +    printf("maxtime=60              ; Max time this process lives\n");
    1.69 +    printf("[child1]                ; Here is a child section\n");
    1.70 +    printf("children=3              ; You can have grandchildren: this spawns 3 of them for child1\n");
    1.71 +    printf("maxtime=30              ; Max time, note it's in seconds. If this time\n");
    1.72 +    printf("                        ; is > main:maxtime then the child process will be\n");
    1.73 +    printf("                        ; killed when the parent exits. Also, grandchildren\n");
    1.74 +    printf("[child2]                ; inherit this maxtime and can't change it.\n");
    1.75 +    printf("maxtime=25              ; You can call these sections whatever you want\n");
    1.76 +    printf("children=0              ; as long as you reference them in a children attribute\n");
    1.77 +    printf("....\n");
    1.78 +    return 0;
    1.79 +  } else if (argc == 2) {
    1.80 +    // This is ini file mode:
    1.81 +    // proclauncher <inifile>
    1.82 +    dict = iniparser_load(argv[1]);
    1.83 +    
    1.84 +  } else if (argc == 3) {
    1.85 +    // Then we've been called in child process launching mode:
    1.86 +    // proclauncher <children> <maxtime> 
    1.87 +    children = atoi(argv[1]);
    1.88 +    maxtime = atoi(argv[2]);
    1.89 +  }
    1.90 +
    1.91 +  if (dict) {
    1.92 +    /* Dict operation */
    1.93 +    char *childlist = iniparser_getstring(dict, "main:children", NULL);
    1.94 +    maxtime = iniparser_getint(dict, (char*)"main:maxtime", 10);;
    1.95 +	if (childlist) {
    1.96 +      int c = 0, m = 10;
    1.97 +      char childkey[50], maxkey[50];
    1.98 +      char cmd[25];
    1.99 +      char *token = strtok(childlist, ",");
   1.100 +
   1.101 +      while (token) {
   1.102 +        // Reset defaults
   1.103 +        memset(childkey, 0, 50);
   1.104 +        memset(maxkey, 0, 50);
   1.105 +        memset(cmd, 0, 25);
   1.106 +        c = 0;
   1.107 +        m = 10;
   1.108 +
   1.109 +        sprintf(childkey, "%s:children", token);
   1.110 +        sprintf(maxkey, "%s:maxtime", token);
   1.111 +        c = iniparser_getint(dict, childkey, 0);
   1.112 +        m = iniparser_getint(dict, maxkey, 10);
   1.113 +        
   1.114 +        // Launch the child process
   1.115 +        #ifdef _WIN32
   1.116 +          launchWindows(c, m);
   1.117 +        #else
   1.118 +          sprintf(cmd, "./proclaunch %d %d &", c, m);
   1.119 +          system(cmd);
   1.120 +        #endif
   1.121 +
   1.122 +        // Get the next child entry
   1.123 +        token = strtok(NULL, ",");
   1.124 +      }
   1.125 +    }
   1.126 +    iniparser_freedict(dict);
   1.127 +  } else {
   1.128 +    // Child Process operation - put on your recursive thinking cap
   1.129 +    char cmd[25];
   1.130 +    // This is launching grandchildren, there are no great grandchildren, so we
   1.131 +    // pass in a 0 for the children to spawn.
   1.132 +    #ifdef _WIN32
   1.133 +      while(children > 0) {
   1.134 +        launchWindows(0, maxtime);
   1.135 +        children--;
   1.136 +      }
   1.137 +    #else
   1.138 +      sprintf(cmd, "./proclaunch %d %d &", 0, maxtime); 
   1.139 +      printf("Launching child process: %s\n", cmd);
   1.140 +      while (children  > 0) {
   1.141 +        system(cmd);
   1.142 +        children--;
   1.143 +      }
   1.144 +    #endif
   1.145 +  }
   1.146 +
   1.147 +  /* Now we have launched all the children.  Let's wait for max time before returning
   1.148 +     This does pseudo busy waiting just to appear active */
   1.149 +  while (passedtime < maxtime) {
   1.150 +#ifdef _WIN32
   1.151 +		Sleep(1000);
   1.152 +#else
   1.153 +	    sleep(1);
   1.154 +#endif
   1.155 +    passedtime++;
   1.156 +  }
   1.157 +  exit(0);
   1.158 +  return 0;
   1.159 +}

mercurial