openbsd-20021011
[openafs.git] / src / afs / afs_util.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 /*
11  * afs_util.c - miscellaneous AFS client utility functions
12  *
13  * Implements:
14  */
15 #include <afsconfig.h>
16 #include "../afs/param.h"
17
18 RCSID("$Header$");
19
20 #include "../afs/stds.h"
21 #include "../afs/sysincludes.h" /* Standard vendor system headers */
22
23 #if !defined(UKERNEL)
24 #include <net/if.h>
25 #include <netinet/in.h>
26
27 #ifdef AFS_SGI62_ENV
28 #include "../h/hashing.h"
29 #endif
30 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN60_ENV)
31 #include <netinet/in_var.h>
32 #endif /* ! AFS_HPUX110_ENV */
33 #endif /* !defined(UKERNEL) */
34
35 #include "../afs/afsincludes.h" /* Afs-based standard headers */
36 #include "../afs/afs_stats.h"   /* afs statistics */
37
38 #if     defined(AFS_SUN56_ENV)
39 #include <inet/led.h>
40 #include <inet/common.h>
41 #if     defined(AFS_SUN58_ENV)
42 #include <netinet/ip6.h>
43 #endif
44 #include <inet/ip.h>
45 #endif
46
47 #if     defined(AFS_AIX_ENV)
48 #include <sys/fp_io.h>
49 #endif
50
51 char *afs_cv2string(char *ttp, afs_uint32 aval)
52 {
53     register char *tp = ttp;
54     register int  i;
55     int any;
56     
57     AFS_STATCNT(afs_cv2string);
58     any = 0;
59     *(--tp) = 0;
60     while (aval != 0) {
61         i = aval % 10;
62         *(--tp) = '0' + i;
63         aval /= 10;
64         any = 1;
65     }
66     if (!any)
67         *(--tp) = '0';
68     return tp;
69
70 } /*afs_cv2string*/
71
72 int afs_strcasecmp(char *s1, char *s2)
73 {
74     while (*s1 && *s2) {
75         char c1, c2;
76
77         c1 = *s1++;
78         c2 = *s2++;
79         if (c1 >= 'A' && c1 <= 'Z') c1 += 0x20;
80         if (c2 >= 'A' && c2 <= 'Z') c2 += 0x20;
81         if (c1 != c2)
82             return c1-c2;
83     }
84
85     return *s1 - *s2;
86 }
87
88 char *afs_strchr(char *s, int c)
89 {
90     char *p;
91
92     for (p = s; *p; p++)
93         if (*p == c)
94             return p;
95     return NULL;
96 }
97
98 char *afs_strdup(char *s)
99 {
100     char *n;
101     int cc;
102
103     cc = strlen(s) + 1;
104     n = (char *) afs_osi_Alloc(cc);
105     if (n)
106         memcpy(n, s, cc);
107
108     return n;
109 }
110
111 void print_internet_address(char *preamble, struct srvAddr *sa,
112                             char *postamble, int flag)
113 {
114     register struct server *aserver = sa->server;
115     char *ptr = "\n";
116     afs_uint32 address;
117     
118     AFS_STATCNT(print_internet_address);
119     address = ntohl(sa->sa_ip);
120     if (aserver->flags & SRVR_MULTIHOMED) {
121         if (flag == 1) {        /* server down mesg */
122             if (!(aserver->flags & SRVR_ISDOWN))
123                 ptr = " (multi-homed address; other same-host interfaces maybe up)\n";
124             else
125                 ptr = " (all multi-homed ip addresses down for the server)\n";
126         } else if (flag == 2) { /* server up mesg */
127             ptr = " (multi-homed address; other same-host interfaces may still be down)\n";
128         }
129     }
130     afs_warn("%s%d.%d.%d.%d in cell %s%s%s",
131            preamble, (address >> 24), (address >> 16) & 0xff, (address >> 8) & 0xff, (address) & 0xff,
132            aserver->cell->cellName, postamble, ptr);
133     afs_warnuser("%s%d.%d.%d.%d in cell %s%s%s",
134             preamble, (address >> 24), (address >> 16) & 0xff, (address >> 8) & 0xff, (address) & 0xff,
135             aserver->cell->cellName, postamble, ptr);
136
137 } /*print_internet_address*/
138
139
140
141 /* * * * * * *
142  * this code badly needs to be cleaned up...  too many ugly ifdefs.
143  * XXX
144  */
145 #if 0
146 void afs_warn(char *a, long b, long c, long d, long e, long f, 
147         long g, long h, long i, long j)
148 #else
149 void afs_warn(a,b,c,d,e,f,g,h,i,j)
150 char *a;
151 long b,c,d,e,f,g,h,i,j;
152 #endif
153 {
154     AFS_STATCNT(afs_warn);
155     
156     if (afs_showflags & GAGCONSOLE)
157     {
158 #if defined(AFS_AIX_ENV)
159         struct file *fd;
160
161         /* cf. console_printf() in oncplus/kernext/nfs/serv/shared.c */
162         if (fp_open("/dev/console",O_WRONLY|O_NOCTTY|O_NDELAY,
163                     0666,0,FP_SYS,&fd) == 0) {
164             char buf[1024];
165             ssize_t len;
166             ssize_t count;
167
168             sprintf(buf, a,b,c,d,e,f,g,h,i,j);
169             len = strlen(buf);
170             fp_write(fd, buf, len, 0, UIO_SYSSPACE, &count);
171             fp_close(fd);
172         }
173 #else
174         printf(a,b,c,d,e,f,g,h,i,j);
175 #endif
176     }
177 }
178
179 #if 0
180 void afs_warnuser(char *a, long b, long c, long d, long e, long f, 
181         long g, long h, long i, long j)
182 #else
183 void afs_warnuser(a,b,c,d,e,f,g,h,i,j)
184 char *a;
185 long b,c,d,e,f,g,h,i,j;
186 #endif
187 {
188     AFS_STATCNT(afs_warnuser);
189     if (afs_showflags & GAGUSER)
190     {
191 #ifdef AFS_GLOBAL_SUNLOCK
192         int haveGlock = ISAFS_GLOCK();
193         if (haveGlock)
194             AFS_GUNLOCK();
195 #endif /* AFS_GLOBAL_SUNLOCK */
196
197         uprintf(a,b,c,d,e,f,g,h,i,j);
198
199 #ifdef AFS_GLOBAL_SUNLOCK
200         if (haveGlock)
201             AFS_GLOCK();
202 #endif /* AFS_GLOBAL_SUNLOCK */
203     }
204 }
205
206
207 /* run everywhere, checking locks */
208 void afs_CheckLocks(void)
209 {
210     register int i;
211
212     afs_warn("Looking for locked data structures.\n");
213     afs_warn("conn %x, volume %x, user %x, cell %x, server %x\n",
214             afs_xconn, afs_xvolume, afs_xuser, afs_xcell, afs_xserver);
215     {
216         register struct vcache *tvc;
217         AFS_STATCNT(afs_CheckLocks);
218
219         for(i=0;i<VCSIZE;i++) {
220             for(tvc = afs_vhashT[i]; tvc; tvc=tvc->hnext) {
221 #ifdef  AFS_OSF_ENV
222                 if (VREFCOUNT(tvc) > 1)
223 #else   /* AFS_OSF_ENV */
224                 if (VREFCOUNT(tvc))
225 #endif
226                     afs_warn("Stat cache entry at %x is held\n", tvc);
227                 if (CheckLock(&tvc->lock))
228                     afs_warn("Stat entry at %x is locked\n", tvc);
229             }
230         }
231     }
232     {
233         register struct dcache *tdc;
234         for (i=0;i<afs_cacheFiles;i++) {
235             tdc = afs_indexTable[i];
236             if (tdc) {
237                 if (tdc->refCount)
238                     afs_warn("Disk entry %d at %x is held\n", i, tdc);
239             }
240             if (afs_indexFlags[i] & IFDataMod)
241                 afs_warn("Disk entry %d at %x has IFDataMod flag set.\n", i, tdc);
242         }
243     }
244     {
245        struct srvAddr *sa;
246        struct server *ts;
247        struct conn *tc;
248         for (i=0;i<NSERVERS;i++) {
249             for (ts = afs_servers[i]; ts; ts=ts->next) {
250                 if (ts->flags & SRVR_ISDOWN)
251                     printf("Server entry %x is marked down\n", ts);
252                 for (sa = ts->addr; sa; sa = sa->next_sa) {     
253                     for (tc = sa->conns; tc; tc=tc->next) {
254                        if (tc->refCount)
255                           afs_warn("conn at %x (server %x) is held\n", tc, sa->sa_ip);
256                     }
257                 }
258             }
259         }
260     }
261     {
262        struct volume *tv;
263         for (i=0;i<NVOLS;i++) {
264             for (tv = afs_volumes[i]; tv; tv=tv->next) {
265                 if (CheckLock(&tv->lock))
266                     afs_warn("volume at %x is locked\n", tv);
267                 if (tv->refCount)
268                     afs_warn("volume at %x is held\n", tv);
269             }
270         }
271     }
272     {
273        struct unixuser *tu;
274
275         for (i=0;i<NUSERS;i++) {
276             for (tu = afs_users[i]; tu; tu=tu->next) {
277                 if (tu->refCount) printf("user at %x is held\n", tu);
278             }
279         }
280     }
281     afs_warn("Done.\n");
282 }
283
284
285 int afs_noop(void)
286 {
287     AFS_STATCNT(afs_noop);
288 #ifdef  AFS_OSF30_ENV
289     return (EOPNOTSUPP);
290 #else
291     return EINVAL;
292 #endif
293 }
294
295 int afs_badop(void)
296 {
297     AFS_STATCNT(afs_badop);
298     osi_Panic("afs bad vnode op");
299     return 0;
300 }
301
302 /*
303  * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
304  * given data pointer, without triggering "cast truncates pointer"
305  * warnings.  We use this where we explicitly don't care whether a
306  * pointer is truncated -- it loses information where a pointer is
307  * larger than an afs_int32.
308  */
309
310 afs_int32 afs_data_pointer_to_int32(const void *p)
311 {
312         union {
313                 afs_int32  i32[sizeof(void *)/sizeof(afs_int32)];
314                 const void *p;
315         } ip;
316
317         int i32_sub;    /* subscript of least significant afs_int32 in ip.i32[] */
318
319         /* set i32_sub */
320
321         {
322                 /* used to determine the byte order of the system */
323
324                 union {
325                         char c[sizeof(int)/sizeof(char)];
326                         int  i;
327                 } ci;
328
329                 ci.i = 1;
330                 if (ci.c[0] == 1) {
331                         /* little-endian system */
332                         i32_sub = 0;
333                 } else {
334                         /* big-endian system */
335                         i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
336                 }
337         }
338
339         ip.p = p;
340         return ip.i32[i32_sub];
341 }