3aa6bc7d0c0a9563cd6dbe374568ac246ffe577a
[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
99     return (cmd_Dispatch(argc, argv));
100 }
101
102 /* initialize the process.  Reads the init files to get the appropriate
103  * information. */
104 void vxd_Shutdown(void);
105 int afsd_shutdown(int);
106 int shutdown_handler(int);
107
108 BOOL InitInstance(struct cmd_syndesc *as, char *arock)
109 {
110         long code;
111         char *reason;
112
113 #ifdef DJGPP
114         osi_Init();
115 #endif
116  
117 #ifndef DJGPP
118         osi_InitPanic(afsd_notifier);
119 #endif
120
121         /*sleep(10);*/
122         
123         afsi_start();
124
125         code = afsMsg_Init();
126         if (code != 0)
127                 osi_panic("socket failure", __FILE__, __LINE__);
128         
129         code = afsd_InitCM(&reason, as, arock);
130         if (code != 0)
131                 osi_panic(reason, __FILE__, __LINE__);
132
133         code = afsd_InitDaemons(&reason);
134         if (code != 0)
135                 osi_panic(reason, __FILE__, __LINE__);
136
137         code = afsd_InitSMB(&reason);
138         if (code != 0)
139                 osi_panic(reason, __FILE__, __LINE__);
140
141         signal(SIGINT, shutdown_handler);
142
143         thrd_Yield();   /* give new threads a chance to run */
144         
145         /* send message to GUI caller indicating successful init */
146         afs_current_status = AFS_STATUS_RUNNING;
147         afsMsg_StatusChange(afs_current_status, 0, NULL);
148
149 #ifdef DJGPP
150         /* Keep the process from just terminating */
151         while(afs_shutdown == 0)
152         {
153         /*IOMGR_Sleep(180);*/
154           IOMGR_Sleep(8);
155                 /* workaround: WaitForKeystroke(nonzero num) calls 
156                    IOMGR_Select, though Win95 select works only on sockets */
157                 /* so, we poll instead */
158                 /*if (LWP_WaitForKeystroke(0))
159                   break;*/
160         }
161         afsd_shutdown(0);
162 #endif
163         afs_exit(0);
164         
165         return (TRUE);
166 }
167
168 int shutdown_handler(int x)
169 {
170   if (!tried_shutdown)
171   {
172     fprintf(stderr, "This program should not be shut down manually.  It should "
173            "be shut down by the\nWindows AFS Client Control Center.  Press Ctrl-C "
174             "again if you really want to do this.\n");
175     fflush(stderr);
176     tried_shutdown = 1;
177   }
178   else
179   {
180     fprintf(stderr, "Shutting down AFSD...\n");
181     fflush(stderr);
182     afs_shutdown = 1;
183   }
184 }
185
186 int afsd_shutdown(int x)
187 {
188 #ifdef AFS_VXD
189   vxd_Shutdown();
190 #else
191   smb_Shutdown();
192 #endif
193   
194   fprintf(stderr, "AFSD shutdown complete.\n");
195   /*exit(0);*/
196 }
197
198 void afs_exit(int exitCode)
199 {
200   afs_current_status = AFS_STATUS_EXITING;
201   afsMsg_StatusChange(afs_current_status,
202                       exitCode, NULL);
203   afsMsg_Shutdown();
204   exit(exitCode);
205 }