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