1 /*
2  * The Spread Toolkit.
3  *
4  * The contents of this file are subject to the Spread Open-Source
5  * License, Version 1.0 (the ``License''); you may not use
6  * this file except in compliance with the License.  You may obtain a
7  * copy of the License at:
8  *
9  * http://www.spread.org/license/
10  *
11  * or in the file ``license.txt'' found in this distribution.
12  *
13  * Software distributed under the License is distributed on an AS IS basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15  * for the specific language governing rights and limitations under the
16  * License.
17  *
18  * The Creators of Spread are:
19  *  Yair Amir, Michal Miskin-Amir, Jonathan Stanton.
20  *
21  *  Copyright (C) 1993-2004 Spread Concepts LLC <spread@spreadconcepts.com>
22  *
23  *  All Rights Reserved.
24  *
25  * Major Contributor(s):
26  * ---------------
27  *    Cristina Nita-Rotaru crisn@cs.purdue.edu - group communication security.
28  *    Theo Schlossnagle    jesus@omniti.com - Perl, skiplists, autoconf.
29  *    Dan Schoenblum       dansch@cnds.jhu.edu - Java interface.
30  *    John Schultz         jschultz@cnds.jhu.edu - contribution to process group membership.
31  *
32  */
33 
34 
35 #include <stdio.h>
36 #include "arch.h"
37 
38 /* undef redefined variables under windows */
39 #ifdef ARCH_PC_WIN95
40 #undef EINTR
41 #undef EAGAIN
42 #undef EWOULDBLOCK
43 #undef EINPROGRESS
44 #endif
45 #include <errno.h>
46 
47 #include <string.h>
48 
49 #include "log.h"
50 #include "configuration.h"
51 #include "membership.h"
52 #include "prot_body.h"
53 #include "sp_events.h"
54 #include "alarm.h"
55 
56 static	int	Is_inited = 0;
57 
58 static	sp_time	alive_time;
59 
60 static	FILE	*fd;
61 static	proc	My;
62 
63 static  void	Log_alive(int dummy, void *dummy_p);
64 
Log_init()65 void	Log_init()
66 {
67 	long	start_file_pos;
68 
69 	Is_inited = 1;
70 
71 	My = Conf_my();
72 
73 	fd = fopen( My.name, "a" );
74 	if( fd == NULL )
75 		Alarm( EXIT, "Log_init: error (%s) could not open file %s\n",strerror(errno), My.name );
76 	start_file_pos = ftell(fd);
77         if (start_file_pos == -1)
78                 Alarm( EXIT, "Log_init: failed to find end of file %s\n", My.name );
79 	fclose(fd);
80 	fd = fopen( My.name, "r+" );
81 	if( fd == NULL )
82 		Alarm( EXIT, "Log_init: error (%s) could not open file %s\n",strerror(errno), My.name );
83 	fseek( fd, start_file_pos, SEEK_SET );
84 
85 	alive_time.sec  = 10;
86 	alive_time.usec =  0;
87 
88 	Log_alive(0, NULL);
89 	fprintf( fd, "B %13ld\n",E_get_time().sec );
90 	fflush( fd );
91 }
92 
Log_alive(int dummy,void * dummy_p)93 static  void	Log_alive(int dummy, void *dummy_p)
94 {
95 	long	file_pos;
96 
97 	if( !Is_inited ) return;
98 #if ( SPREAD_PROTOCOL == 3 )
99 	fprintf( fd, "A %13ld %11d\n",E_get_time().sec, Highest_seq );
100 #else
101 	fprintf( fd, "A %13ld \n",E_get_time().sec );
102 #endif
103         if( fseek( fd, -28, SEEK_CUR ) )
104                 Alarm( EXIT, "Log_alive: error (%s) in fseek -28 on %s\n", strerror(errno), My.name);
105 	file_pos = ftell(fd);
106 	if( fseek( fd,  28, SEEK_CUR ) )
107                 Alarm( EXIT, "Log_alive: error (%s) in fseek 28 on %s\n", strerror(errno), My.name);
108 	fclose(fd);
109 	fd = fopen( My.name, "r+" );
110 	if( fd == NULL )
111 		Alarm( EXIT, "Log_alive: error (%s) could not open file %s\n",strerror(errno), My.name );
112 	if( fseek( fd, file_pos, SEEK_SET ) )
113                 Alarm( EXIT, "Log_alive: error (%s) in fseek file_pos (%ld) on %s\n", strerror(errno), file_pos,  My.name);
114 
115 	E_queue( Log_alive, 0, NULL, alive_time );
116 }
117 
Log_membership()118 void 	Log_membership()
119 {
120 	configuration	Cn;
121 	int32		proc_id;
122 	proc		dummy_p;
123 	int		i,j;
124 	int		found;
125 
126 	if( !Is_inited ) return;
127 #if ( SPREAD_PROTOCOL == 3 )
128 	fprintf( fd, "M %13ld %13d %11d > ",
129 		E_get_time().sec, Memb_id().time, Highest_seq );
130 #else
131 	fprintf( fd, "M %13ld %13d > ",
132 		E_get_time().sec, Memb_id_for_Network().time );
133 #endif
134 	found = -1;
135 	Cn= Conf();
136 	for( i=0; i < Conf().num_segments; i++ )
137 	{
138 	    for( j=0; j < Cn.segments[i].num_procs; j++ )
139 	    {
140 		proc_id = Cn.segments[i].procs[j]->id;
141 		if( Conf_id_in_conf( Memb_active_ptr(), proc_id ) != -1 )
142 		{
143 		    if( found == -1 ) found = Conf_proc_by_id( proc_id, &dummy_p );
144 		    fprintf( fd, " %2d", found );
145 		}else fprintf( fd, " --" );
146 	    }
147 	}
148 
149 	fprintf( fd, "\n");
150 	Log_alive(0, NULL);
151 }
152 
Log_sess_connect(mailbox mbox,int32 address,char * private_group)153 void	Log_sess_connect( mailbox mbox, int32 address, char *private_group )
154 {
155 	char 	ip[16];
156 
157 	if( !Is_inited ) return;
158 
159 	Conf_id_to_str( address, ip );
160 #if ( SPREAD_PROTOCOL == 3 )
161 	fprintf( fd, "C %13ld %11d %3d %-16s %-10s\n",
162 		E_get_time().sec, Highest_seq, mbox, ip, private_group );
163 #else
164 	fprintf( fd, "C %13ld %3d %-16s %-10s\n",
165 		E_get_time().sec, mbox, ip, private_group );
166 #endif
167 	Log_alive(0, NULL);
168 }
169 
Log_sess_disconnect(mailbox mbox,int32 address,char * private_group,int num_mess)170 void	Log_sess_disconnect( mailbox mbox, int32 address, char *private_group, int num_mess )
171 {
172 	char 	ip[16];
173 
174 	if( !Is_inited ) return;
175 
176 	Conf_id_to_str( address, ip );
177 #if ( SPREAD_PROTOCOL == 3 )
178 	fprintf( fd, "D %13ld %11d %3d %-16s %-10s %d\n",
179 		E_get_time().sec, Highest_seq, mbox, ip, private_group, num_mess );
180 #else
181 	fprintf( fd, "D %13ld %3d %-16s %-10s %d\n",
182 		E_get_time().sec, mbox, ip, private_group, num_mess );
183 #endif
184 	Log_alive(0, NULL);
185 }
186