testing/mozbase/mozprocess/tests/proclaunch.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include <stdio.h>
michael@0 6 #include <stdlib.h>
michael@0 7 #include "iniparser.h"
michael@0 8
michael@0 9 #ifdef _WIN32
michael@0 10 #include <windows.h>
michael@0 11 #include <tchar.h>
michael@0 12
michael@0 13 extern int iniparser_getint(dictionary *d, char *key, int notfound);
michael@0 14 extern char *iniparser_getstring(dictionary *d, char *key, char *def);
michael@0 15
michael@0 16 // This is the windows launcher function
michael@0 17 int launchWindows(int children, int maxtime) {
michael@0 18 _TCHAR cmdline[50];
michael@0 19 STARTUPINFO startup;
michael@0 20 PROCESS_INFORMATION procinfo;
michael@0 21 BOOL rv = 0;
michael@0 22
michael@0 23 _stprintf(cmdline, _T("proclaunch.exe %d %d"), children, maxtime);
michael@0 24 ZeroMemory(&startup, sizeof(STARTUPINFO));
michael@0 25 startup.cb = sizeof(STARTUPINFO);
michael@0 26
michael@0 27 ZeroMemory(&procinfo, sizeof(PROCESS_INFORMATION));
michael@0 28
michael@0 29 printf("Launching process!\n");
michael@0 30 rv = CreateProcess(NULL,
michael@0 31 cmdline,
michael@0 32 NULL,
michael@0 33 NULL,
michael@0 34 FALSE,
michael@0 35 0,
michael@0 36 NULL,
michael@0 37 NULL,
michael@0 38 &startup,
michael@0 39 &procinfo);
michael@0 40
michael@0 41 if (!rv) {
michael@0 42 DWORD dw = GetLastError();
michael@0 43 printf("error: %d\n", dw);
michael@0 44 }
michael@0 45 CloseHandle(procinfo.hProcess);
michael@0 46 CloseHandle(procinfo.hThread);
michael@0 47 return 0;
michael@0 48 }
michael@0 49 #endif
michael@0 50
michael@0 51 int main(int argc, char **argv) {
michael@0 52 int children = 0;
michael@0 53 int maxtime = 0;
michael@0 54 int passedtime = 0;
michael@0 55 dictionary *dict = NULL;
michael@0 56
michael@0 57 // Command line handling
michael@0 58 if (argc == 1 || (0 == strcmp(argv[1], "-h")) || (0 == strcmp(argv[1], "--help"))) {
michael@0 59 printf("ProcLauncher takes an ini file. Specify the ini file as the only\n");
michael@0 60 printf("parameter of the command line:\n");
michael@0 61 printf("proclauncher my.ini\n\n");
michael@0 62 printf("The ini file has the form:\n");
michael@0 63 printf("[main]\n");
michael@0 64 printf("children=child1,child2 ; These comma separated values are sections\n");
michael@0 65 printf("maxtime=60 ; Max time this process lives\n");
michael@0 66 printf("[child1] ; Here is a child section\n");
michael@0 67 printf("children=3 ; You can have grandchildren: this spawns 3 of them for child1\n");
michael@0 68 printf("maxtime=30 ; Max time, note it's in seconds. If this time\n");
michael@0 69 printf(" ; is > main:maxtime then the child process will be\n");
michael@0 70 printf(" ; killed when the parent exits. Also, grandchildren\n");
michael@0 71 printf("[child2] ; inherit this maxtime and can't change it.\n");
michael@0 72 printf("maxtime=25 ; You can call these sections whatever you want\n");
michael@0 73 printf("children=0 ; as long as you reference them in a children attribute\n");
michael@0 74 printf("....\n");
michael@0 75 return 0;
michael@0 76 } else if (argc == 2) {
michael@0 77 // This is ini file mode:
michael@0 78 // proclauncher <inifile>
michael@0 79 dict = iniparser_load(argv[1]);
michael@0 80
michael@0 81 } else if (argc == 3) {
michael@0 82 // Then we've been called in child process launching mode:
michael@0 83 // proclauncher <children> <maxtime>
michael@0 84 children = atoi(argv[1]);
michael@0 85 maxtime = atoi(argv[2]);
michael@0 86 }
michael@0 87
michael@0 88 if (dict) {
michael@0 89 /* Dict operation */
michael@0 90 char *childlist = iniparser_getstring(dict, "main:children", NULL);
michael@0 91 maxtime = iniparser_getint(dict, (char*)"main:maxtime", 10);;
michael@0 92 if (childlist) {
michael@0 93 int c = 0, m = 10;
michael@0 94 char childkey[50], maxkey[50];
michael@0 95 char cmd[25];
michael@0 96 char *token = strtok(childlist, ",");
michael@0 97
michael@0 98 while (token) {
michael@0 99 // Reset defaults
michael@0 100 memset(childkey, 0, 50);
michael@0 101 memset(maxkey, 0, 50);
michael@0 102 memset(cmd, 0, 25);
michael@0 103 c = 0;
michael@0 104 m = 10;
michael@0 105
michael@0 106 sprintf(childkey, "%s:children", token);
michael@0 107 sprintf(maxkey, "%s:maxtime", token);
michael@0 108 c = iniparser_getint(dict, childkey, 0);
michael@0 109 m = iniparser_getint(dict, maxkey, 10);
michael@0 110
michael@0 111 // Launch the child process
michael@0 112 #ifdef _WIN32
michael@0 113 launchWindows(c, m);
michael@0 114 #else
michael@0 115 sprintf(cmd, "./proclaunch %d %d &", c, m);
michael@0 116 system(cmd);
michael@0 117 #endif
michael@0 118
michael@0 119 // Get the next child entry
michael@0 120 token = strtok(NULL, ",");
michael@0 121 }
michael@0 122 }
michael@0 123 iniparser_freedict(dict);
michael@0 124 } else {
michael@0 125 // Child Process operation - put on your recursive thinking cap
michael@0 126 char cmd[25];
michael@0 127 // This is launching grandchildren, there are no great grandchildren, so we
michael@0 128 // pass in a 0 for the children to spawn.
michael@0 129 #ifdef _WIN32
michael@0 130 while(children > 0) {
michael@0 131 launchWindows(0, maxtime);
michael@0 132 children--;
michael@0 133 }
michael@0 134 #else
michael@0 135 sprintf(cmd, "./proclaunch %d %d &", 0, maxtime);
michael@0 136 printf("Launching child process: %s\n", cmd);
michael@0 137 while (children > 0) {
michael@0 138 system(cmd);
michael@0 139 children--;
michael@0 140 }
michael@0 141 #endif
michael@0 142 }
michael@0 143
michael@0 144 /* Now we have launched all the children. Let's wait for max time before returning
michael@0 145 This does pseudo busy waiting just to appear active */
michael@0 146 while (passedtime < maxtime) {
michael@0 147 #ifdef _WIN32
michael@0 148 Sleep(1000);
michael@0 149 #else
michael@0 150 sleep(1);
michael@0 151 #endif
michael@0 152 passedtime++;
michael@0 153 }
michael@0 154 exit(0);
michael@0 155 return 0;
michael@0 156 }

mercurial