add-missing-include-file-afsd-windows-20031203
[openafs.git] / src / WINNT / afsd / afsd95.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 /*#include <windows.h>*/
14 #include <string.h>
15 /*#include <nb30.h>*/
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include <osi.h>
21 #include <signal.h>
22 #include <afs/cmd.h>
23 /*#include <winsock2.h>*/
24 #include "afsd.h"
25 #include "afsd_init.h"
26
27
28 char main_statusText[100];
29 osi_log_t *afsd_logp;
30
31 extern int traceOnPanic;
32 BOOL InitInstance(struct cmd_syndesc *as, char *arock);
33 extern int afs_shutdown;
34 int tried_shutdown=0;
35
36 int afs_current_status = AFS_STATUS_INIT;
37
38 /*
39  * Notifier function for use by osi_panic
40  */
41 void afsd_notifier(char *msgp, char *filep, long line)
42 {
43         char tbuffer[100];
44         if (filep)
45                 sprintf(tbuffer, "Error at file %s, line %d", filep, line);
46         else
47                 strcpy(tbuffer, "Error at unknown location");
48
49         if (!msgp)
50                 msgp = "Assertion failure";
51
52         /*MessageBox(NULL, tbuffer, msgp, MB_OK|MB_ICONSTOP|MB_SETFOREGROUND);*/
53
54         afsd_ForceTrace(TRUE);
55
56         if (traceOnPanic) {
57                 /*asm("int 3");*/
58         }
59
60         afs_exit(AFS_EXITCODE_PANIC);
61 }
62
63 /* Init function called when window application starts.  Inits instance and
64  * application together, since in Win32 they're essentially the same.
65  *
66  * Function then goes into a loop handling user interface messages.  Most are
67  * used to handle redrawing the icon.
68  */
69 int main(int argc, char *argv[])
70 {
71     struct cmd_syndesc *ts;
72     
73     fprintf(stderr, "AFS Client for Windows 95.\n");
74     /*fprintf(stderr, "Use Ctrl-C to shut down client.\n\n\n");*/
75     ts = cmd_CreateSyntax((char *) 0, (int (*)()) InitInstance, (char *) 0, "start AFS");
76     cmd_AddParm(ts, "-lanadapt", CMD_SINGLE, CMD_OPTIONAL, "LAN adapter number");
77     cmd_AddParm(ts, "-threads", CMD_SINGLE, CMD_OPTIONAL, "Number of server threads");
78     cmd_AddParm(ts, "-rootvol", CMD_SINGLE, CMD_OPTIONAL, "name of AFS root volume");
79     cmd_AddParm(ts, "-stat", CMD_SINGLE, CMD_OPTIONAL, "number of stat entries");
80     cmd_AddParm(ts, "-memcache", CMD_FLAG, CMD_OPTIONAL, "use memory cache");
81     cmd_AddParm(ts, "-cachedir", CMD_SINGLE, CMD_OPTIONAL, "cache directory");
82     cmd_AddParm(ts, "-mountdir", CMD_SINGLE, CMD_OPTIONAL, "mount location");
83     cmd_AddParm(ts, "-daemons", CMD_SINGLE, CMD_OPTIONAL, "number of daemons to use");
84     cmd_AddParm(ts, "-nosettime", CMD_FLAG, CMD_OPTIONAL, "don't set the time");
85     cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "display lots of information");
86     cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL, "display debug info");
87     cmd_AddParm(ts, "-chunksize", CMD_SINGLE, CMD_OPTIONAL, "log(2) of chunk size");
88     cmd_AddParm(ts, "-dcache", CMD_SINGLE, CMD_OPTIONAL, "number of dcache entries");
89     cmd_AddParm(ts, "-confdir", CMD_SINGLE, CMD_OPTIONAL, "configuration directory");
90     cmd_AddParm(ts, "-logfile", CMD_SINGLE, CMD_OPTIONAL, "Place to keep the CM log");
91     cmd_AddParm(ts, "-waitclose", CMD_FLAG, CMD_OPTIONAL, "make close calls synchronous");
92     cmd_AddParm(ts, "-shutdown", CMD_FLAG, CMD_OPTIONAL, "Shutdown all afs state");
93     cmd_AddParm(ts, "-sysname", CMD_SINGLE, CMD_OPTIONAL, "System name (@sys value)");
94     cmd_AddParm(ts, "-gateway", CMD_FLAG, CMD_OPTIONAL, "machine is a gateway");
95     cmd_AddParm(ts, "-tracebuf", CMD_SINGLE, CMD_OPTIONAL, "trace buffer size");
96     cmd_AddParm(ts, "-startup", CMD_FLAG, CMD_OPTIONAL, "start AFS client");
97     cmd_AddParm(ts, "-diskcache", CMD_SINGLE, CMD_OPTIONAL, "diskcache size");
98     cmd_AddParm(ts, "-afsdb", CMD_FLAG, CMD_OPTIONAL, "use DNS for cell server resolution");
99     cmd_AddParm(ts, "-freelance", CMD_FLAG, CMD_OPTIONAL, "virtual AFS root");
100
101     return (cmd_Dispatch(argc, argv));
102 }
103
104 /* initialize the process.  Reads the init files to get the appropriate
105  * information. */
106 void vxd_Shutdown(void);
107 int afsd_shutdown(int);
108 int shutdown_handler(int);
109
110 BOOL InitInstance(struct cmd_syndesc *as, char *arock)
111 {
112         long code;
113         char *reason;
114
115 #ifdef DJGPP
116         osi_Init();
117 #endif
118  
119 #ifndef DJGPP
120         osi_InitPanic(afsd_notifier);
121 #endif
122
123         /*sleep(10);*/
124         
125         afsi_start();
126
127         code = afsMsg_Init();
128         if (code != 0)
129                 osi_panic("socket failure", __FILE__, __LINE__);
130         
131         code = afsd_InitCM(&reason, as, arock);
132         if (code != 0)
133                 osi_panic(reason, __FILE__, __LINE__);
134
135         code = afsd_InitDaemons(&reason);
136         if (code != 0)
137                 osi_panic(reason, __FILE__, __LINE__);
138
139         code = afsd_InitSMB(&reason);
140         if (code != 0)
141                 osi_panic(reason, __FILE__, __LINE__);
142
143         signal(SIGINT, shutdown_handler);
144
145         thrd_Yield();   /* give new threads a chance to run */
146         
147         /* send message to GUI caller indicating successful init */
148         afs_current_status = AFS_STATUS_RUNNING;
149         afsMsg_StatusChange(afs_current_status, 0, NULL);
150
151 #ifdef DJGPP
152         /* Keep the process from just terminating */
153         while(afs_shutdown == 0)
154         {
155         /*IOMGR_Sleep(180);*/
156           IOMGR_Sleep(8);
157                 /* workaround: WaitForKeystroke(nonzero num) calls 
158                    IOMGR_Select, though Win95 select works only on sockets */
159                 /* so, we poll instead */
160                 /*if (LWP_WaitForKeystroke(0))
161                   break;*/
162         }
163         afsd_shutdown(0);
164 #endif
165         afs_exit(0);
166         
167         return (TRUE);
168 }
169
170 int shutdown_handler(int x)
171 {
172   if (!tried_shutdown)
173   {
174     fprintf(stderr, "This program should not be shut down manually.  It should "
175            "be shut down by the\nWindows AFS Client Control Center.  Press Ctrl-C "
176             "again if you really want to do this.\n");
177     fflush(stderr);
178     tried_shutdown = 1;
179   }
180   else
181   {
182     fprintf(stderr, "Shutting down AFSD...\n");
183     fflush(stderr);
184     afs_shutdown = 1;
185   }
186 }
187
188 int afsd_shutdown(int x)
189 {
190 #ifdef AFS_VXD
191   vxd_Shutdown();
192 #else
193   smb_Shutdown();
194 #endif
195   
196   fprintf(stderr, "AFSD shutdown complete.\n");
197   /*exit(0);*/
198 }
199
200 void afs_exit(int exitCode)
201 {
202   afs_current_status = AFS_STATUS_EXITING;
203   afsMsg_StatusChange(afs_current_status,
204                       exitCode, NULL);
205   afsMsg_Shutdown();
206   exit(exitCode);
207 }