Standardize License information
[openafs.git] / src / rx / bulktest / bulk_server.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 <sys/types.h>
11 #include <netdb.h>
12 #include <netinet/in.h>
13 #include <sys/stat.h>
14 #include <sys/file.h>
15 #include <signal.h>
16 #include <stdio.h>
17 #include <rx/xdr.h>
18 #include "bulk.h"
19
20 #define N_SECURITY_OBJECTS 1
21
22 extern BULK_ExecuteRequest();
23
24 InterruptSignal() {
25     rx_PrintStats(stdout);
26     exit(0);
27 }
28
29 main() {
30     struct rx_securityClass *(securityObjects[N_SECURITY_OBJECTS]);
31     struct rx_service *service;
32
33     signal(SIGINT, InterruptSignal);
34
35     /* Initialize Rx, telling it port number this server will use for its single service */
36     if (rx_Init(BULK_SERVER_PORT) < 0) Quit("rx_init");
37
38     /* Create a single security object, in this case the null security object, for unauthenticated connections, which will be used to control security on connections made to this server */
39     securityObjects[BULK_NULL] = rxnull_NewServerSecurityObject();
40     if (securityObjects[BULK_NULL] == (struct rx_securityClass *) 0) Quit("rxnull_NewServerSecurityObject");
41
42     /* Instantiate a single BULK service.  The rxgen-generated procedure which is called to decode requests is passed in here (BULK_ExecuteRequest). */
43     service = rx_NewService(0, BULK_SERVICE_ID, "BULK", securityObjects, N_SECURITY_OBJECTS, BULK_ExecuteRequest);
44     if (service == (struct rx_service *) 0) Quit("rx_NewService");
45     rx_SetMaxProcs(service, 5);
46
47     rx_StartServer(1); /* Donate this process to the server process pool */
48     Quit("StartServer returned?");
49 }
50
51 int bulk_operationNumber = 0;
52
53 int BULK_FetchFile(call, verbose, name)
54     struct rx_call *call;
55     char *name;
56 {
57     int fd = -1;
58     int error = 0;
59     int opnum = ++bulk_operationNumber;
60     struct stat status;
61     if (verbose) printf("%d: fetch %s\n", opnum, name);
62     fd = open(name, O_RDONLY, 0);
63     if (fd < 0 || fstat(fd, &status) < 0) {
64         if (verbose) printf("%d: failed to open %s\n", opnum, name);
65         error = BULK_ERROR;
66         goto fail;
67     }
68     error = bulk_SendFile(fd, call, &status);
69     if (error) printf("%d: fetch of %s failed, error %d\n", opnum, name, error);
70     else if (verbose) printf("%d: fetch of %s complete\n", opnum, name);
71 fail:
72     if (fd >= 0) close(fd);
73     return error;
74 }
75
76 int BULK_StoreFile(call, verbose, name)
77     struct rx_call *call;
78     int verbose;
79     char *name;
80 {
81     int opnum = ++bulk_operationNumber;
82     int fd = -1;
83     struct stat status;
84     int error = 0;
85     if (verbose) printf("%d: store file %s\n", opnum, name);
86     fd = open(name, O_CREAT|O_TRUNC|O_WRONLY, 0666);
87     if (fd < 0 || fstat(fd, &status) < 0) {
88         fprintf(stderr, "%d: could not create %s\n", opnum, name);
89         error = BULK_ERROR;
90         goto fail;
91     }
92     error = bulk_ReceiveFile(fd, call, &status);
93     if (error) printf("%d: store of %s failed, error %d\n", opnum, name, error);
94     else if (verbose) printf("%d: store of %s complete\n", opnum, name);
95     
96 fail:
97     if (fd >= 0) close(fd);
98     return error;
99 }
100
101 Quit(msg, a, b)
102     char *msg;
103 {
104     fprintf(stderr, msg, a, b);
105     exit(1);
106 }
107