Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / rx / bulk.example / 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 <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include <sys/types.h>
15 #include <netdb.h>
16 #include <netinet/in.h>
17 #include <sys/stat.h>
18 #include <sys/file.h>
19 #include <stdio.h>
20 #include <rx/xdr.h>
21 #include "bulk.h"
22
23 #define N_SECURITY_OBJECTS 1
24
25 void Quit(char *msg);
26
27 int
28 main(int argc, char **argv)
29 {
30 struct rx_securityClass *(securityObjects[N_SECURITY_OBJECTS]);
31 struct rx_service *service;
32
33 /* Initialize Rx, telling it port number this server will use for its single service */
34 if (rx_Init(BULK_SERVER_PORT) < 0)
35 Quit("rx_init");
36
37 /* 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 */
38 securityObjects[RX_SECIDX_NULL] = rxnull_NewServerSecurityObject();
39 if (securityObjects[RX_SECIDX_NULL] == (struct rx_securityClass *)0)
40 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 =
44 rx_NewService(0, BULK_SERVICE_ID, "BULK", securityObjects,
45 N_SECURITY_OBJECTS, BULK_ExecuteRequest);
46 if (service == (struct rx_service *)0)
47 Quit("rx_NewService");
48 rx_SetMaxProcs(service, 2);
49
50 rx_StartServer(1); /* Donate this process to the server process pool */
51 Quit("StartServer returned?");
52 }
53
54 int
55 BULK_FetchFile(struct rx_call *call, int verbose, char *name)
56 {
57 int fd = -1;
58 int error = 0;
59 struct stat status;
60 if (verbose)
61 printf("Fetch file %s\n", name);
62 fd = open(name, O_RDONLY, 0);
63 if (fd < 0 || fstat(fd, &status) < 0) {
64 if (verbose)
65 printf("Failed to open %s\n", name);
66 error = BULK_ERROR;
67 }
68 if (!error)
69 error = bulk_SendFile(fd, call, &status);
70 if (fd >= 0)
71 close(fd);
72 return error;
73 }
74
75 int
76 BULK_StoreFile(struct rx_call *call, int verbose, char *name)
77 {
78 int fd = -1;
79 struct stat status;
80 int error = 0;
81 if (verbose)
82 printf("Store file %s\n", name);
83 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
84 if (fd < 0 || fstat(fd, &status) < 0) {
85 fprintf(stderr, "Could not create %s\n", name);
86 error = BULK_ERROR;
87 }
88 if (!error)
89 error = bulk_ReceiveFile(fd, call, &status);
90 if (fd >= 0)
91 close(fd);
92 return error;
93 }
94
95 void
96 Quit(char *msg)
97 {
98 fprintf(stderr, msg);
99 exit(1);
100 }