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