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