File: server.cc

package info (click to toggle)
dnprogs 2.18-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,896 kB
  • ctags: 3,051
  • sloc: ansic: 18,586; cpp: 9,436; makefile: 669; sh: 502; awk: 13
file content (331 lines) | stat: -rw-r--r-- 8,189 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/******************************************************************************
    (c) 1998-1999 P.J. Caulfield               patrick@tykepenguin.cix.co.uk
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 ******************************************************************************
 */
// server.cc
// Code for a single FAL server process.
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <syslog.h>
#include <ctype.h>
#include <pwd.h>
#include <grp.h>
#include <glob.h>
#include <regex.h>
#include <string.h>
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>

#include "logging.h"
#include "connection.h"
#include "protocol.h"
#include "vaxcrc.h"
#include "params.h"
#include "task.h"
#include "server.h"
#include "directory.h"
#include "open.h"
#include "create.h"
#include "erase.h"
#include "rename.h"
#include "submit.h"

#define min(a,b) (a)<(b)?(a):(b)
#define MAX_BUFSIZE 65535

// Called to close down the FAL server process. Because child processes get 
// deleted this ensures that things are tidied up in the right order.
// We DON'T delete the connection here because it belongs to fal.cc and not us.
void fal_server::closedown()
{
    if (attrib_msg)  delete attrib_msg;
    if (alloc_msg)   delete alloc_msg;
    if (protect_msg) delete protect_msg;
}

// Main loop for FAL server process
bool fal_server::run()
{
    dap_message *m = NULL;
    bool finished = false;

// This makes it easier for me to debug child processes
    if (getenv("FAL_CHILD_DEBUG")) sleep(100000);

    attrib_msg  = NULL;
    alloc_msg   = NULL;
    protect_msg = NULL;

    if (!exchange_config())
    {
	DAPLOG((LOG_ERR, "Did not get CONFIG message\n"));
	return false;
    }
    current_task = NULL;

    do
    {
	m = dap_message::read_message(conn, true);
	if (m)
	{
	    if (verbose > 2)
		DAPLOG((LOG_ERR ,"Next message: %s\n", m->type_name()));

	    // All actions are initiated by ACCESS messages.
	    // If we get an ACCESS message before a task has completed then
	    // we just abandon it and start a new one.
	    if (m->get_type() == dap_message::ACCESS)
	    {
		if (current_task) delete current_task;
		create_access_task(m);
		
		if (!current_task) // Wot??
		{
		    dap_status_message st;
		    
		    st.set_code(020342); // Operation unsupported
		    st.write(conn);
		    return false;
		}
	    }

	    // Deal with messages where we don't have a current task
	    if (!current_task)
	    {
		switch (m->get_type())
		{
		case dap_message::ACCESS:
		    // dealt with above
		    break;

		    // These three are all to do with file attributes. We save
		    // these messages and pass them on to the task if asked.
		case dap_message::ATTRIB:
		    {
			if (attrib_msg) delete attrib_msg;
			attrib_msg = (dap_attrib_message *)m;
			m = NULL; // Do not delete it
		    }
		    break;

		case dap_message::ALLOC:
		    {
			if (alloc_msg) delete alloc_msg;
			alloc_msg = (dap_alloc_message *)m;
			m = NULL; // Do not delete it
		    }
		    break;

		case dap_message::PROTECT:
		    {
			if (protect_msg) delete protect_msg;
			protect_msg = (dap_protect_message *)m;
			m = NULL; // Do not delete it
		    }
		    break;
		    
		    // These we just discard 'cos there's no point to them
		case dap_message::DATE:
		    break;

		    // We get these when the remote end starts a new task
		case dap_message::CONFIG:
		    {
			dap_config_message cfg;
			cfg.write(conn);
		    }
		    break;

		    // Just reply to any ACCOMP messages. Our state machine
                    // obviously is different to DEC's FAL.
		case dap_message::ACCOMP:
		    {
			dap_accomp_message reply;
			reply.set_cmpfunc(dap_accomp_message::RESPONSE);
			reply.write(conn);
		    } 
		    break;

		default:
		    {
			DAPLOG((LOG_ERR, "task type %d not supported\n",
				m->get_type()));
			dap_status_message st;

			st.set_code(020342); // Operation unsupported
			st.write(conn);

			return false;
		    }
		}
	    }

	    // Tell the task to do its work.
	    if (current_task && !current_task->process_message(m))
	    {
		// Task completed
		delete current_task;
		current_task = NULL;
	    }
	}
	else
	{
	    finished = true; // Error on reading. Probably the remote task
                             // closed the connection.
	}

	// Delete the message
	if (m) delete m;

    } while (!finished);

    // If we ended because of a comms error then say so.
    if (conn.get_error())
    {
	if (verbose) DAPLOG((LOG_ERR, "%s\n", conn.get_error()));
	return false;
    }
    return true;
}

// Exchange config message
bool fal_server::exchange_config()
{

// Send our config message
    dap_config_message *newcm = new dap_config_message(MAX_BUFSIZE);
    if (!newcm->write(conn)) return false;
    delete newcm;

// Read the client's config message
    dap_message *m=dap_message::read_message(conn, true);
    if (!m) // Comms error
    {
	DAPLOG((LOG_ERR, "%s\n", conn.get_error()));
	return false;
    }

// Check it's OK and set the connection buffer size
    dap_config_message *cm = (dap_config_message *)m;
    if (m->get_type() == dap_message::CONFIG)
    {
	cm = (dap_config_message *)m;

	if (verbose > 1)
	    DAPLOG((LOG_DEBUG, "Remote buffer size is %d\n", 
		    cm->get_bufsize()));
	conn.set_blocksize(min(MAX_BUFSIZE,cm->get_bufsize()));
	if (verbose > 1)
	    DAPLOG((LOG_DEBUG, "Using block size %d\n", 
		    conn.get_blocksize()));

	need_crcs = cm->need_crc();
	if (verbose > 1 && need_crcs)
	    DAPLOG((LOG_DEBUG, "Remote end supports CRCs\n"));

	params.remote_os = cm->get_os();
	if (verbose > 1)
	    DAPLOG((LOG_DEBUG, "Remote OS is %d\n", params.remote_os));

    }
    else
    {
	DAPLOG((LOG_ERR, "Got %s instead of CONFIG\n", m->type_name()));
	return false;
    }
    delete m;

    return true;
}

// Create a task to process an ACCESS message
void fal_server::create_access_task(dap_message *m)
{
    dap_access_message *am = (dap_access_message *)m;

    if (verbose > 1)
    {
	DAPLOG((LOG_DEBUG, "ACCESS: func: %s\n", func_name(am->get_accfunc())));
	DAPLOG((LOG_DEBUG, "ACCESS: file: %s\n", am->get_filespec()));
    }
    
    // Do what we need to do
    switch(am->get_accfunc())
    {
    case dap_access_message::OPEN:
	current_task = new fal_open(conn, verbose, params,
				    attrib_msg, alloc_msg, protect_msg);
	break;
    case dap_access_message::CREATE:
	current_task = new fal_create(conn, verbose, params, 
				      attrib_msg, alloc_msg, protect_msg);
	break;
    case dap_access_message::RENAME:
	current_task = new fal_rename(conn, verbose, params);
	break;
    case dap_access_message::ERASE:
	current_task = new fal_erase(conn, verbose, params);
	break;
    case dap_access_message::DIRECTORY:
	current_task = new fal_directory(conn, verbose, params);
	break;
    case dap_access_message::SUBMIT:
	current_task = new fal_submit(conn, verbose, params);
	break;

    default:
	current_task = NULL;
	return;

    }
    current_task->set_crc(need_crcs);
}


// Return the DAP ACCESS function name
char *fal_server::func_name(int number)
{
    static char num[32];

    switch (number)
    {
    case dap_access_message::OPEN:
        return "OPEN";
        break;
    case dap_access_message::CREATE:
        return "CREATE";
        break;
    case dap_access_message::RENAME:
        return "RENAME";
        break;
    case dap_access_message::ERASE:
        return "ERASE";
        break;
    case dap_access_message::DIRECTORY:
        return "DIRECTORY";
        break;
    case dap_access_message::SUBMIT:
        return "SUBMIT";
        break;
    default:
	sprintf(num, "UNKNOWN: %d", number);
	return num;
    }
}