1ae7d3acd70c3a76809c6e8d856592d7bdf36072
[openafs.git] / src / afs / afs_mariner.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_mariner.c - fetch/store monitoring facility.
12  */
13 /*
14  * Implements:
15  */
16 #include <afsconfig.h>
17 #include "afs/param.h"
18
19 RCSID
20     ("$Header$");
21
22 #include "afs/sysincludes.h"    /*Standard vendor system headers */
23 #include "afsincludes.h"        /*AFS-based standard headers */
24 #include "afs/afs_stats.h"      /* statistics */
25
26 /* Exported variables */
27 struct rx_service *afs_server;
28
29
30 #define SMAR        20          /* size of a mariner name */
31 #define NMAR        10          /* number of mariner names */
32 static char marinerNames[NMAR][SMAR];
33 static struct vcache *marinerVCs[NMAR];
34 static int marinerPtr = 0;      /* pointer to next mariner slot to use */
35
36 /* Exported variables */
37 afs_int32 afs_mariner = 0;
38 afs_int32 afs_marinerHost = 0;
39
40 int
41 afs_AddMarinerName(register char *aname, register struct vcache *avc)
42 {
43     register int i;
44     register char *tp;
45
46     AFS_STATCNT(afs_AddMarinerName);
47     i = marinerPtr++;
48     if (i >= NMAR) {
49         i = 0;
50         marinerPtr = 1;
51     }
52     tp = marinerNames[i];
53     strncpy(tp, aname, SMAR);
54     tp[SMAR - 1] = 0;
55     marinerVCs[i] = avc;
56     return 0;
57 }
58
59 char *
60 afs_GetMariner(register struct vcache *avc)
61 {
62     register int i;
63     AFS_STATCNT(afs_GetMariner);
64     for (i = 0; i < NMAR; i++) {
65         if (marinerVCs[i] == avc) {
66             return marinerNames[i];
67         }
68     }
69     return "a file";
70 }
71
72 void
73 afs_MarinerLogFetch(register struct vcache *avc, register afs_int32 off,
74                     register afs_int32 bytes, register afs_int32 idx)
75 {
76     struct sockaddr_in taddr;
77     register char *tp, *tp1, *tp2;
78     struct iovec dvec;
79     int len;
80
81
82     AFS_STATCNT(afs_MarinerLog);
83     taddr.sin_family = AF_INET;
84     taddr.sin_addr.s_addr = afs_marinerHost;
85     taddr.sin_port = htons(2106);
86 #ifdef  STRUCT_SOCKADDR_HAS_SA_LEN
87     taddr.sin_len = sizeof(taddr);
88 #endif /* AFS_OSF_ENV */
89     tp = tp1 = (char *)osi_AllocSmallSpace(AFS_SMALLOCSIZ);
90     strcpy(tp, "fetch$Fetching ");
91     tp += 15;                   /* change it if string changes */
92     tp2 = afs_GetMariner(avc);
93     strcpy(tp, tp2);
94     tp += strlen(tp2);
95     *tp++ = '\n';
96     /* note, console doesn't want a terminating null */
97     len = strlen(tp1) - 1;
98     /* I don't care if mariner packets fail to be sent */
99     dvec.iov_base = tp1;
100     dvec.iov_len = len;
101     AFS_GUNLOCK();
102     (void)osi_NetSend(afs_server->socket, &taddr, sizeof(taddr), &dvec, 1,
103                       len, 0);
104     AFS_GLOCK();
105     osi_FreeSmallSpace(tp1);
106 }                               /*afs_MarinerLogFetch */
107
108 void
109 afs_MarinerLog(register char *astring, register struct vcache *avc)
110 {
111     struct sockaddr_in taddr;
112     register char *tp, *tp1, *buf;
113     struct iovec dvec;
114
115     AFS_STATCNT(afs_MarinerLog);
116     taddr.sin_family = AF_INET;
117     taddr.sin_addr.s_addr = afs_marinerHost;
118     taddr.sin_port = htons(2106);
119 #ifdef  STRUCT_SOCKADDR_HAS_SA_LEN
120     taddr.sin_len = sizeof(taddr);
121 #endif /* AFS_OSF_ENV */
122     tp = buf = (char *)osi_AllocSmallSpace(AFS_SMALLOCSIZ);
123
124     strcpy(tp, astring);
125     tp += strlen(astring);
126     *tp++ = ' ';
127     tp1 = afs_GetMariner(avc);
128     strcpy(tp, tp1);
129     tp += strlen(tp1);
130     *tp++ = '\n';
131     /* note, console doesn't want a terminating null */
132     /* I don't care if mariner packets fail to be sent */
133     dvec.iov_base = buf;
134     dvec.iov_len = tp - buf;
135     AFS_GUNLOCK();
136     (void)osi_NetSend(afs_server->socket, &taddr, sizeof(taddr), &dvec, 1,
137                       tp - buf, 0);
138     AFS_GLOCK();
139     osi_FreeSmallSpace(buf);
140 }                               /*afs_MarinerLog */
141
142 void
143 shutdown_mariner(void)
144 {
145     int i;
146
147     marinerPtr = 0;
148     afs_mariner = 0;
149
150     for (i = 0; i < NMAR; i++)
151         marinerVCs[i] = 0;
152 }