afsconfig-and-rcsid-all-around-20010705
[openafs.git] / src / venus / cacheout.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/vldbint.h>
11
12 #include <afs/param.h>
13 #include <afsconfig.h>
14
15 RCSID("$Header$");
16
17 #include <stdio.h>
18 #include <string.h>
19
20 #ifdef  AFS_AIX32_ENV
21 #include <signal.h>
22 #endif
23
24 #include <ctype.h>
25 #include <sys/types.h>
26 #include <afs/cmd.h>
27 #include <afs/cellconfig.h>
28 #include <rx/rx.h>
29 #include <rx/xdr.h>
30
31 #include <ubik.h>
32 #include <afs/kauth.h>
33 #include <afs/afsutil.h>
34
35 /*
36 File servers in NW byte order.
37 */
38
39 int server_count=0;
40 afs_int32 server_id[256];
41
42 struct ubik_client *client;
43
44 struct ViceIds
45 {
46         int ViceIds_len;
47         afs_int32 *ViceIds_val;
48 }
49 ;
50
51 struct IPAddrs
52 {
53         int IPAddrs_len;
54         afs_int32 *IPAddrs_val;
55 }
56 ;
57
58 struct ubik_dbase *VL_dbase;
59 struct afsconf_dir *vldb_confdir;
60 struct kadstats dynamic_statistics;
61 struct rx_securityClass *junk;
62
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66
67 extern int VL_GetAddrs();
68
69 afs_int32 InvalidateCache(as)
70 struct cmd_syndesc *as;
71 {
72         afs_int32 code=0;
73         struct cmd_item *u;
74         struct rx_connection *conn;
75         int i;
76         afs_int32 port=7000;
77
78         afs_int32 spare1=0;
79         afs_int32 spare2,spare3;
80
81         afs_int32 id[256];
82         afs_int32 ip[256];
83
84         struct ViceIds vid;
85         struct IPAddrs ipa;
86
87         code=ListServers();
88         if(code)
89                 return code;
90
91
92         /* make sure something there */
93
94         if( !as->parms[0].items && !as->parms[1].items)
95         {
96                 printf("Use -help flag for list of optional argmuments\n");
97                 return 1;
98         }
99
100         /* get user ids */
101
102         for(i=0,u=as->parms[0].items;i<255 && u;++i,u=u->next)
103         {
104                 code=util_GetInt32(u->data,&id[i]);
105                 if(code)
106                 {
107                         printf("Fatal error: bad conversion to long for %s\n",u->data);
108                         return code;
109                 }
110         }
111
112         id[i]=0;
113         vid.ViceIds_len=i;
114         vid.ViceIds_val=id;
115
116         /* get IP addresses, convert to NW byte order */
117
118         for(i=0,u=as->parms[1].items;i<255 && u;++i,u=u->next)
119                 ip[i]=inet_addr(u->data);
120
121         ip[i]=0;
122         ipa.IPAddrs_len=i;
123         ipa.IPAddrs_val=ip;
124
125         for(i=0;i<server_count;++i)
126         {
127                 conn=rx_NewConnection(server_id[i],htonl(port),1,
128                         junk,0);
129                 if(!conn)
130                 {
131                         printf("Informational: could not connect to \
132 file server %lx\n",server_id[i]);
133                         continue;
134                 }
135
136                 /* invalidate the cache */
137
138                 code=RXAFS_FlushCPS(conn,&vid,&ipa,spare1,&spare2,&spare3);
139
140                 /*
141                 May get spurious error codes in case server is
142                 down or is reported by VLDB as a file server
143                 even though it is not configured as such in the
144                 cell.
145                 */
146
147                 if(code)
148                         printf("Informational: failed to invalidate \
149 file server %lx cache code = %ld\n",server_id[i],code);
150
151                 rx_DestroyConnection(conn);
152         }
153         return 0;
154 }
155
156 /*
157 Obtain list of file servers as known to VLDB. These may
158 not actually be configured as file servers in the cell.
159 */
160
161 afs_int32 ListServers()
162 {
163         afs_int32 code;
164         struct rx_connection *conn;
165         struct rx_call *call;
166         int i;
167         int byte_count;
168
169         afs_int32 Handle=0;
170         afs_int32 spare2=0;
171         struct VLCallBack spare3;
172
173         bulkaddrs addrs;
174         afs_uint32 *p;
175
176         /* get list of file servers in NW byte order */
177         bzero(&addrs, sizeof(addrs));
178         bzero(&spare3, sizeof(spare3));
179         code=ubik_Call(VL_GetAddrs,client,0,Handle,spare2,&spare3,
180                 &server_count,&addrs);
181         if(code)
182         {
183                 printf("Fatal error: could not get list of \
184 file servers\n");
185                 return 1;
186         }
187         server_count=ntohl(server_count);
188
189         for(i=0,p=addrs.bulkaddrs_val;i<server_count;++i,++p)
190                 server_id[i] = *p;
191
192         return code;
193
194 }
195
196 afs_int32 GetServerList()
197 {
198         afs_int32 code;
199         int i;
200
201         code=ListServers();
202         if(code)
203                 return(code);
204
205         printf("There are %d file servers in the cell\n\n",server_count);
206         fflush(stdout);
207         for(i=0;i<server_count;++i)
208                 printf("%s\n",
209                         hostutil_GetNameByINet(server_id[i]));
210         fflush(stdout);
211
212         return code;
213 }
214
215 /*
216 User enters lists of:
217
218         1. AFS user ids - say from "pts exam username".
219         2. IP addresses - say from /etc/hosts (no wildcards).
220
221 Command is executed in user's cell.
222 */
223
224 static MyBeforeProc(as, arock)
225 struct cmd_syndesc *as;
226 char *arock; {
227     register char *tcell = (char *)0;
228     char confdir[200];
229     struct afsconf_dir *tdir;
230     struct afsconf_cell info;
231     struct rx_connection *serverconns[MAXSERVERS];
232     register afs_int32 code, i;
233     register afs_int32 sauth;
234
235     sprintf(confdir,"%s",AFSDIR_CLIENT_ETC_DIRPATH);
236     /* setup to talk to servers */
237     code=rx_Init(0);
238     if (code)
239         printf("Warning: could not initialize network communication.\n");
240
241     junk=(struct rx_securityClass *)rxnull_NewClientSecurityObject();
242     tdir=afsconf_Open(confdir);
243     if(!tdir)
244         printf("Warning: could not get cell configuration.\n");
245
246     if (as->parms[2].items) /* if -cell specified */
247         tcell = as->parms[2].items->data;
248     code = afsconf_GetCellInfo(tdir, tcell, AFSCONF_VLDBSERVICE,&info);
249     if (info.numServers > MAXSERVERS)
250         printf("Warning: could not init cell info.\n");
251
252     for(i=0;i<info.numServers;++i)
253         serverconns[i]=rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
254                                         info.hostAddr[i].sin_port,
255                                         USER_SERVICE_ID, junk, 0);
256     for (;i < MAXSERVERS; ++i) {
257         serverconns[i] = (struct rx_connection *)0;
258     }
259     code=ubik_ClientInit(serverconns,&client);
260     if(code)
261         printf("Warning: could not initialize RPC interface.\n");
262 }
263
264
265 int main (argc, argv)
266 int argc;
267 char **argv;
268 {
269         afs_int32 code=0;
270         struct cmd_syndesc *ts;
271         int i;
272
273 #ifdef  AFS_AIX32_ENV
274         struct sigaction nsa;
275         
276         sigemptyset(&nsa.sa_mask);
277         nsa.sa_handler = SIG_DFL;
278         nsa.sa_flags = SA_FULLDUMP;
279         sigaction(SIGSEGV, &nsa, NULL);
280 #endif
281
282         /*
283         Look in /usr/vice/etc (client side database).
284         */
285         cmd_SetBeforeProc(MyBeforeProc,  (char *) 0);
286
287         ts = cmd_CreateSyntax("initcmd"/*"invalidatecache"*/,InvalidateCache,0,
288                 "invalidate server ACL cache");
289         cmd_AddParm(ts,"-id",CMD_LIST,CMD_OPTIONAL,"user identifier");
290         cmd_AddParm(ts,"-ip",CMD_LIST,CMD_OPTIONAL,"IP address");
291         cmd_CreateAlias(ts,"ic");
292         cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
293
294         ts = cmd_CreateSyntax("listservers",GetServerList,0,
295                 "list servers in the cell");
296         cmd_CreateAlias(ts,"ls");
297
298         code = cmd_Dispatch(argc,argv);
299         
300         rx_Finalize();
301
302         exit(code);
303 }
304