libafscp: a library for "clientless" operations
[openafs.git] / src / libafscp / afscp_init.c
1 /* AUTORIGHTS
2 Copyright (C) 2003 - 2010 Chaskiel Grundman
3 All rights reserved
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11
12 2. Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <unistd.h>
35 #include <sys/select.h>
36 #include <sys/stat.h>
37
38 #include <afs/param.h>
39 #include <lwp.h>
40 #include <rx/rx_null.h>
41 #include <rx/rx.h>
42
43 #include <com_err.h>
44
45 #include "afscp.h"
46 #include "afscp_internal.h"
47
48 #ifndef AFSCONF_CLIENTNAME
49 #include <afs/dirpath.h>
50 #define AFSCONF_CLIENTNAME AFSDIR_CLIENT_ETC_DIRPATH
51 #endif
52
53 static int init=0;
54 extern int RXAFSCB_ExecuteRequest();
55 static struct rx_securityClass *sc;
56 static struct rx_service *serv;
57 extern PROCESS rx_listenerPid;
58 static int start_cb_server()
59 {
60
61      sc=rxnull_NewServerSecurityObject();
62      serv=rx_NewService(0,1,"afs", &sc, 1, RXAFSCB_ExecuteRequest);
63      if (!serv)
64           return 1;
65      rx_StartServer(0);
66      return 0;
67 }
68
69
70 int afscp_init(const char *cell)
71 {
72      if (init)
73           return 0;
74      if (_rx_InitRandomPort())
75              return -1;
76      init=1;
77      if (start_cb_server()) {
78           printf("Cannot start callback service\n");
79           return -1;
80      }
81      init=2;
82      if (cell)
83           return afs_setdefaultcell(cell);
84      return 0;
85 }
86
87 void afscp_finalize(void) {
88      if (!init)
89           return;
90
91      ReturnAllCallBacks();
92      IOMGR_Sleep(1);
93      rx_Finalize();
94 #if 0
95      LWP_DestroyProcess(rx_listenerPid);
96      close(serv->socket);
97      rxi_FreeService(serv);
98 #endif
99      IOMGR_Finalize();
100      LWP_TerminateProcessSupport();
101 #if 1
102      close(serv->socket);
103 #endif
104 }
105
106