windows-misc-20050722
[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         buf_ForceTrace(TRUE);
56
57         if (traceOnPanic) {
58                 /*asm("int 3");*/
59         }
60
61         afs_exit(AFS_EXITCODE_PANIC);
62 }
63
64 /* Init function called when window application starts.  Inits instance and
65  * application together, since in Win32 they're essentially the same.
66  *
67  * Function then goes into a loop handling user interface messages.  Most are
68  * used to handle redrawing the icon.
69  */
70 int main(int argc, char *argv[])
71 {
72     struct cmd_syndesc *ts;
73     
74     fprintf(stderr, "AFS Client for Windows 95.\n");
75     /*fprintf(stderr, "Use Ctrl-C to shut down client.\n\n\n");*/
76     ts = cmd_CreateSyntax((char *) 0, (int (*)()) InitInstance, (char *) 0, "start AFS");
77     cmd_AddParm(ts, "-lanadapt", CMD_SINGLE, CMD_OPTIONAL, "LAN adapter number");
78     cmd_AddParm(ts, "-threads", CMD_SINGLE, CMD_OPTIONAL, "Number of server threads");
79     cmd_AddParm(ts, "-rootvol", CMD_SINGLE, CMD_OPTIONAL, "name of AFS root volume");
80     cmd_AddParm(ts, "-stat", CMD_SINGLE, CMD_OPTIONAL, "number of stat entries");
81     cmd_AddParm(ts, "-memcache", CMD_FLAG, CMD_OPTIONAL, "use memory cache");
82     cmd_AddParm(ts, "-cachedir", CMD_SINGLE, CMD_OPTIONAL, "cache directory");
83     cmd_AddParm(ts, "-mountdir", CMD_SINGLE, CMD_OPTIONAL, "mount location");
84     cmd_AddParm(ts, "-daemons", CMD_SINGLE, CMD_OPTIONAL, "number of daemons to use");
85     cmd_AddParm(ts, "-nosettime", CMD_FLAG, CMD_OPTIONAL, "don't set the time");
86     cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "display lots of information");
87     cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL, "display debug info");
88     cmd_AddParm(ts, "-chunksize", CMD_SINGLE, CMD_OPTIONAL, "log(2) of chunk size");
89     cmd_AddParm(ts, "-dcache", CMD_SINGLE, CMD_OPTIONAL, "number of dcache entries");
90     cmd_AddParm(ts, "-confdir", CMD_SINGLE, CMD_OPTIONAL, "configuration directory");
91     cmd_AddParm(ts, "-logfile", CMD_SINGLE, CMD_OPTIONAL, "Place to keep the CM log");
92     cmd_AddParm(ts, "-waitclose", CMD_FLAG, CMD_OPTIONAL, "make close calls synchronous");
93     cmd_AddParm(ts, "-shutdown", CMD_FLAG, CMD_OPTIONAL, "Shutdown all afs state");
94     cmd_AddParm(ts, "-sysname", CMD_SINGLE, CMD_OPTIONAL, "System name (@sys value)");
95     cmd_AddParm(ts, "-gateway", CMD_FLAG, CMD_OPTIONAL, "machine is a gateway");
96     cmd_AddParm(ts, "-tracebuf", CMD_SINGLE, CMD_OPTIONAL, "trace buffer size");
97     cmd_AddParm(ts, "-startup", CMD_FLAG, CMD_OPTIONAL, "start AFS client");
98     cmd_AddParm(ts, "-diskcache", CMD_SINGLE, CMD_OPTIONAL, "diskcache size");
99     cmd_AddParm(ts, "-afsdb", CMD_FLAG, CMD_OPTIONAL, "use DNS for cell server resolution");
100     cmd_AddParm(ts, "-freelance", CMD_FLAG, CMD_OPTIONAL, "virtual AFS root");
101
102     return (cmd_Dispatch(argc, argv));
103 }
104
105 /* initialize the process.  Reads the init files to get the appropriate
106  * information. */
107 void vxd_Shutdown(void);
108 int afsd_shutdown(int);
109 int shutdown_handler(int);
110
111 BOOL InitInstance(struct cmd_syndesc *as, char *arock)
112 {
113         long code;
114         char *reason;
115
116 #ifdef DJGPP
117         osi_Init();
118 #endif
119  
120 #ifndef DJGPP
121         osi_InitPanic(afsd_notifier);
122 #endif
123
124         /*sleep(10);*/
125         
126         afsi_start();
127
128         code = afsMsg_Init();
129         if (code != 0)
130                 osi_panic("socket failure", __FILE__, __LINE__);
131         
132         code = afsd_InitCM(&reason, as, arock);
133         if (code != 0)
134                 osi_panic(reason, __FILE__, __LINE__);
135
136         code = afsd_InitDaemons(&reason);
137         if (code != 0)
138                 osi_panic(reason, __FILE__, __LINE__);
139
140         code = afsd_InitSMB(&reason);
141         if (code != 0)
142                 osi_panic(reason, __FILE__, __LINE__);
143
144         signal(SIGINT, shutdown_handler);
145
146         thrd_Yield();   /* give new threads a chance to run */
147         
148         /* send message to GUI caller indicating successful init */
149         afs_current_status = AFS_STATUS_RUNNING;
150         afsMsg_StatusChange(afs_current_status, 0, NULL);
151
152 #ifdef DJGPP
153         /* Keep the process from just terminating */
154         while(afs_shutdown == 0)
155         {
156         /*IOMGR_Sleep(180);*/
157           IOMGR_Sleep(8);
158                 /* workaround: WaitForKeystroke(nonzero num) calls 
159                    IOMGR_Select, though Win95 select works only on sockets */
160                 /* so, we poll instead */
161                 /*if (LWP_WaitForKeystroke(0))
162                   break;*/
163         }
164         afsd_shutdown(0);
165 #endif
166         afs_exit(0);
167         
168         return (TRUE);
169 }
170
171 int shutdown_handler(int x)
172 {
173   if (!tried_shutdown)
174   {
175     fprintf(stderr, "This program should not be shut down manually.  It should "
176            "be shut down by the\nWindows AFS Client Control Center.  Press Ctrl-C "
177             "again if you really want to do this.\n");
178     fflush(stderr);
179     tried_shutdown = 1;
180   }
181   else
182   {
183     fprintf(stderr, "Shutting down AFSD...\n");
184     fflush(stderr);
185     afs_shutdown = 1;
186   }
187 }
188
189 int afsd_shutdown(int x)
190 {
191 #ifdef AFS_VXD
192   vxd_Shutdown();
193 #else
194   smb_Shutdown();
195 #endif
196   
197   fprintf(stderr, "AFSD shutdown complete.\n");
198   /*exit(0);*/
199 }
200
201 void afs_exit(int exitCode)
202 {
203   afs_current_status = AFS_STATUS_EXITING;
204   afsMsg_StatusChange(afs_current_status,
205                       exitCode, NULL);
206   afsMsg_Shutdown();
207   exit(exitCode);
208 }