de42fbd1b3d83c49e357e46e8e6b56394082c911
[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                 if (tvc->states & CVInit) continue;
274 #ifdef  AFS_OSF_ENV
275                 if (VREFCOUNT(tvc) > 1)
276 #else /* AFS_OSF_ENV */
277 #ifdef AFS_DARWIN80_ENV
278                 if (vnode_isinuse(AFSTOV(tvc), 0))
279 #else
280                 if (VREFCOUNT(tvc))
281 #endif
282 #endif
283                     afs_warn("Stat cache entry at %x is held\n", tvc);
284                 if (CheckLock(&tvc->lock))
285                     afs_warn("Stat entry at %x is locked\n", tvc);
286             }
287         }
288     }
289     {
290         register struct dcache *tdc;
291         for (i = 0; i < afs_cacheFiles; i++) {
292             tdc = afs_indexTable[i];
293             if (tdc) {
294                 if (tdc->refCount)
295                     afs_warn("Disk entry %d at %x is held\n", i, tdc);
296             }
297             if (afs_indexFlags[i] & IFDataMod)
298                 afs_warn("Disk entry %d at %x has IFDataMod flag set.\n", i,
299                          tdc);
300         }
301     }
302     {
303         struct srvAddr *sa;
304         struct server *ts;
305         struct conn *tc;
306         for (i = 0; i < NSERVERS; i++) {
307             for (ts = afs_servers[i]; ts; ts = ts->next) {
308                 if (ts->flags & SRVR_ISDOWN)
309                     printf("Server entry %lx is marked down\n", (unsigned long)ts);
310                 for (sa = ts->addr; sa; sa = sa->next_sa) {
311                     for (tc = sa->conns; tc; tc = tc->next) {
312                         if (tc->refCount)
313                             afs_warn("conn at %x (server %x) is held\n", tc,
314                                      sa->sa_ip);
315                     }
316                 }
317             }
318         }
319     }
320     {
321         struct volume *tv;
322         for (i = 0; i < NVOLS; i++) {
323             for (tv = afs_volumes[i]; tv; tv = tv->next) {
324                 if (CheckLock(&tv->lock))
325                     afs_warn("volume at %x is locked\n", tv);
326                 if (tv->refCount)
327                     afs_warn("volume at %x is held\n", tv);
328             }
329         }
330     }
331     {
332         struct unixuser *tu;
333
334         for (i = 0; i < NUSERS; i++) {
335             for (tu = afs_users[i]; tu; tu = tu->next) {
336                 if (tu->refCount)
337                     printf("user at %lx is held\n", (unsigned long)tu);
338             }
339         }
340     }
341     afs_warn("Done.\n");
342 }
343
344
345 int
346 afs_noop(void)
347 {
348     AFS_STATCNT(afs_noop);
349 #ifdef  AFS_OSF30_ENV
350     return (EOPNOTSUPP);
351 #else
352     return EINVAL;
353 #endif
354 }
355
356 int
357 afs_badop(void)
358 {
359     AFS_STATCNT(afs_badop);
360     osi_Panic("afs bad vnode op");
361     return 0;
362 }
363
364 /*
365  * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
366  * given data pointer, without triggering "cast truncates pointer"
367  * warnings.  We use this where we explicitly don't care whether a
368  * pointer is truncated -- it loses information where a pointer is
369  * larger than an afs_int32.
370  */
371
372 afs_int32
373 afs_data_pointer_to_int32(const void *p)
374 {
375     union {
376         afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
377         const void *p;
378     } ip;
379
380     int i32_sub;                /* subscript of least significant afs_int32 in ip.i32[] */
381
382     /* set i32_sub */
383
384     {
385         /* used to determine the byte order of the system */
386
387         union {
388             char c[sizeof(int) / sizeof(char)];
389             int i;
390         } ci;
391
392         ci.i = 1;
393         if (ci.c[0] == 1) {
394             /* little-endian system */
395             i32_sub = 0;
396         } else {
397             /* big-endian system */
398             i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
399         }
400     }
401
402     ip.p = p;
403     return ip.i32[i32_sub];
404 }