1 /* win32 version of yaws starup script */
2 /* Author: klacke@hyber.org (Jan 2009) */
3 
4 
5 #include <windows.h>
6 #include <stdio.h>
7 
8 #define BSIZ 16000
9 
10 static unsigned char path[BSIZ];
11 static unsigned char *fpath;
12 
readreg()13 static void readreg() {
14     HKEY hKey;
15     DWORD bsz = BSIZ;
16     // Check where yaws is installed, we need to -pa that path
17     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
18                      "SOFTWARE\\Hyber\\Yaws",
19                      0, KEY_READ, &hKey) != ERROR_SUCCESS)
20         {
21             printf ("Cannot find yaws installation in the registry\n");
22             exit(1);
23         }
24     memset(path, 0, BSIZ);
25     /*int ret = */RegQueryValueEx(hKey, "DIR", NULL, NULL, path, &bsz);
26     fpath = (unsigned char*)malloc(strlen((char*)path));
27     int i;
28     for(i = 0; i<strlen((char*)path); i++ ) {
29         if (path[i] == '\\') {
30             fpath[i] = '/';
31         }
32         else
33             fpath[i] = path[i];
34     }
35     fpath[i] = 0;
36 
37 }
38 
39 
charg(char ** argv,char * s1,char * s2,int p)40 int charg(char **argv, char *s1, char *s2, int p) {
41     if (strcmp(s1, argv[p]) == 0) return 1;
42     if ((s2 != NULL) && strcmp(s2, argv[p]) == 0) return 1;
43     return 0;
44 }
45 
46 
runwait(char * execString)47 int runwait(char *execString) {
48 
49     STARTUPINFO          si = { sizeof(si) };
50     PROCESS_INFORMATION  pi;
51 
52     //printf("execString: %s \n", execString);
53 
54     if(CreateProcess(0, execString, NULL, NULL, FALSE, 0, 0, 0, &si, &pi))
55         {
56             unsigned long ret = 0;
57 
58             // wait for process to finish
59             WaitForSingleObject(pi.hProcess, INFINITE);
60             if (GetExitCodeProcess(pi.hProcess, &ret) == 0)
61                 ret = 1;
62             CloseHandle(pi.hProcess);
63             CloseHandle(pi.hThread);
64             return ret;
65         }
66     printf("Failed to create the process entirely\n");
67     printf("Tried to invoke: <%s> \n", execString);
68     printf("Make sure you have <erl> in your environment PATH \n");
69     return 1;
70 }
71 
72 
73 
nosh(char * s)74 int nosh(char *s) {
75     char buf[BUFSIZ];
76     sprintf (buf, "erl -noshell -pa \"%s/ebin\" %s ", fpath, s);
77     return runwait(buf);
78 }
79 
help()80 int help() {
81     printf("usage:  "
82 ""
83 "       yaws -i | --interactive       -- interactive (no daemon) mode"
84 "       yaws -w | --winteractive      -- interactive (werl) "
85 "       yaws --daemon                 -- daemon mode"
86 ""
87 ""
88 ""
89 "       Auxiliary flags for the daemon: "
90 "            --id Id          --  set system id"
91 "            --debug          --  debug mode "
92 "            --conf File      --  set config file"
93 "            --tracetraf      --  trace traffic"
94 "            --tracehttp      --  trace http traffic"
95 "            --traceout       --  trace output to stdout"
96 "            --version        --  print version"
97 "            --pa path        --  add load path"
98 "            --mnesiadir dir  --  start Mnesia in dir"
99 "            --sname xxx      --  start with sname xxx"
100 "            --name xxx       --  start with name xxx"
101 "            --runmod mod     --  call mod:start/0 at startup"
102 "            --erlarg X       --  pass argument X to $erl"
103 ""
104 "ctl functions ... "
105 "        yaws --hup [--id ID]                 -- hup the daemon, reload conf"
106 "        yaws --stop [--id ID]                -- stop the daemon "
107 "        yaws --debug-dump [--id ID]          -- produce a debug dump "
108 "        yaws --status [--id ID]              -- query the daemon status "
109 "        yaws --load Modules                  -- load modules "
110 "        yaws --ls                            -- list Yaws nodes and their status"
111 "        yaws --ctltrace traffic|http         -- toggle trace of running daemon");
112 
113     return(0);
114 }
115 
116 
117 
main(int argc,char ** argv)118 int main(int argc, char**argv) {
119 
120     int interactive  = 1;
121     int winteractive  = 0;
122     int daemon = 0;
123     char *conf = NULL;
124     char *sname = NULL;
125     char *name = NULL;
126     int debug = 0;
127     char *runmod = NULL;
128     char *trace = NULL;
129     char *traceoutput = NULL;
130     char tbuf[BUFSIZ];
131     char paBuf[BUFSIZ];
132     char execString[BSIZ];
133     char mnesia[255];
134     char erlarg[255];
135     char *id = "default";
136 
137     memset(paBuf, 0, BUFSIZ);
138     memset(tbuf, 0, BUFSIZ);
139     memset(execString, 0, BSIZ);
140 
141     readreg();
142     sprintf(paBuf, " -pa \"%s/ebin\" ", fpath);
143 
144     int p = 1;
145     while (p < argc) {
146         if (charg(argv, "-i", "--interactive", p))   {
147             interactive = 1;
148         }
149         else if (charg(argv, "-w", "--winteractive", p)) {
150             winteractive = 1;
151         }
152         else if (charg(argv, "-D", "--daemon", p)) {
153             interactive = winteractive = 0;
154             daemon = 1;
155         }
156         else if (charg(argv, "--debug", NULL, p)) {
157             debug = 1;
158         }
159         else if (charg(argv, "-t", "--tracetraf", p)) {
160              trace=" -yaws trace traffic ";
161         }
162         else if (charg(argv, "-T", "--tracehttp", p)) {
163              trace=" -yaws trace http ";
164         }
165         else if (charg(argv, "-x", "--traceout", p)) {
166             traceoutput = " -yaws traceoutput ";
167         }
168         else if (charg(argv, "--trace", NULL, p)) {
169             traceoutput = " -yaws traceoutput ";
170             trace = " -yaws trace traffic ";
171         }
172         else if (charg(argv, "--mnesiadir", "-M", p)) {
173             sprintf(mnesia, "-mnesia dir \"%s\" -run mnesia start ",argv[++p]);
174         }
175         else if (charg(argv, "-c", "--conf", p))  {
176             conf = argv[++p];
177         }
178         else if (charg(argv, "--id", NULL, p))  {
179             id = argv[++p];
180         }
181         else if (charg(argv, "-pa", NULL, p))  {
182             sprintf(tbuf, " -pa \"%s\" ", argv[++p]);
183             strcat(paBuf, tbuf);
184         }
185         else if (charg(argv, "--runmod", "-r", p)) {
186             runmod = argv[++p];
187         }
188         else if (charg(argv, "--hup", "-h", p)) {
189             return nosh("-s yaws_ctl hup default");
190         }
191         else if (charg(argv, "--stop", "-s", p)) {
192             return nosh("-s yaws_ctl stop default");
193         }
194         else if (charg(argv, "--status", "-S", p)) {
195             return nosh("-s yaws_ctl status default");
196         }
197         else if (charg(argv, "--debug-dump", NULL, p)) {
198             return nosh("-s yaws_ctl debug_dump default ");
199         }
200         else   if (charg(argv, "--version", "-v", p)) {
201             nosh("-s yaws printversion");
202             exit(0);
203         }
204         else   if (charg(argv, "--sname", "-sname", p)) {
205              sname = argv[++p];
206         }
207         else   if (charg(argv, "--name", "-name", p)) {
208              name = argv[++p];
209         }
210         else   if (charg(argv, "--erlarg", "-erlarg", p)) {
211             sprintf(tbuf, " %s ", argv[++p]);
212             strcat(erlarg, tbuf);
213         }
214         else {
215             return help();
216         }
217         p++;
218     }
219 
220     if (winteractive)
221         sprintf(execString, "werl.exe  %s %s ", erlarg, paBuf);
222     else
223         sprintf(execString, "erl.exe  %s %s ", erlarg, paBuf);
224 
225     if (debug)
226         strcat(execString, " -boot start_sasl -yaws debug ");
227     if ((interactive == 0 && winteractive == 0) || daemon == 1)
228         strcat(execString, " -detached ");
229 
230     if (conf == NULL)
231         sprintf(tbuf, " -conf \"%s\\yaws.conf\" ", path);
232     else
233         sprintf(tbuf, " -conf \"%s\" ", conf);
234     strcat(execString, tbuf);
235 
236     sprintf(tbuf, " -run yaws -yaws id %s ", id);
237     strcat(execString, tbuf);
238 
239     if (mnesia != NULL)
240         strcat(execString, mnesia);
241     if (trace != NULL)
242         strcat(execString, trace);
243     if (traceoutput != NULL)
244         strcat(execString, traceoutput);
245 
246     if (sname != NULL) {
247         sprintf(tbuf," -sname %s ", sname);
248         strcat(execString, tbuf);
249     } else if (name != NULL) {
250         sprintf(tbuf, " -name %s ", name);
251         strcat(execString, tbuf);
252     }
253     if (runmod != NULL) {
254         sprintf(tbuf, " -runmod %s ", runmod);
255         strcat(execString, tbuf);
256     }
257 
258     return runwait(execString);
259 }
260 
261 
262 
263