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, John Schultz.
20  *
21  *  Copyright (C) 1993-2012 Spread Concepts LLC <info@spreadconcepts.com>
22  *
23  *  All Rights Reserved.
24  *
25  * Major Contributor(s):
26  * ---------------
27  *    Ryan Caudy           rcaudy@gmail.com - contributions to process groups.
28  *    Claudiu Danilov      claudiu@acm.org - scalable wide area support.
29  *    Cristina Nita-Rotaru crisn@cs.purdue.edu - group communication security.
30  *    Theo Schlossnagle    jesus@omniti.com - Perl, autoconf, old skiplist.
31  *    Dan Schoenblum       dansch@cnds.jhu.edu - Java interface.
32  *
33  */
34 
35 
36 #include <stdio.h>
37 #include "arch.h"
38 
39 /* undef redefined variables under windows */
40 #ifdef ARCH_PC_WIN95
41 #undef EINTR
42 #undef EAGAIN
43 #undef EWOULDBLOCK
44 #undef EINPROGRESS
45 #endif
46 #include <errno.h>
47 
48 #include <string.h>
49 
50 #include "log.h"
51 #include "configuration.h"
52 #include "membership.h"
53 #include "prot_body.h"
54 #include "spu_events.h"
55 #include "spu_alarm.h"
56 
57 static	int	Is_inited = 0;
58 
59 static	sp_time	alive_time;
60 
61 static	FILE	*fd;
62 static	char    My_name[MAX_PROC_NAME];
63 
64 static  void	Log_alive(int dummy, void *dummy_p);
65 
Log_init()66 void	Log_init()
67 {
68 	long	start_file_pos;
69         proc    my;
70 
71 	Is_inited = 1;
72 
73 	my = Conf_my();
74         strncpy( My_name, my.name, MAX_PROC_NAME);
75 
76 	fd = fopen( My_name, "a" );
77 	if( fd == NULL )
78 		Alarm( EXIT, "Log_init: error (%s) could not open file %s\n",strerror(errno), My_name );
79 	start_file_pos = ftell(fd);
80         if (start_file_pos == -1)
81                 Alarm( EXIT, "Log_init: failed to find end of file %s\n", My_name );
82 	fclose(fd);
83 	fd = fopen( My_name, "r+" );
84 	if( fd == NULL )
85 		Alarm( EXIT, "Log_init: error (%s) could not open file %s\n",strerror(errno), My_name );
86 	fseek( fd, start_file_pos, SEEK_SET );
87 
88 	alive_time.sec  = 10;
89 	alive_time.usec =  0;
90 
91 	Log_alive(0, NULL);
92 	fprintf( fd, "B %13ld\n",E_get_time().sec );
93 	fflush( fd );
94 }
95 
Log_alive(int dummy,void * dummy_p)96 static  void	Log_alive(int dummy, void *dummy_p)
97 {
98 	long	file_pos;
99 
100 	if( !Is_inited ) return;
101 #if ( SPREAD_PROTOCOL == 3 )
102 	fprintf( fd, "A %13ld %11d\n",E_get_time().sec, Highest_seq );
103 #else
104 	fprintf( fd, "A %13ld \n",E_get_time().sec );
105 #endif
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 	file_pos = ftell(fd);
109 	if( fseek( fd,  28, SEEK_CUR ) )
110                 Alarm( EXIT, "Log_alive: error (%s) in fseek 28 on %s\n", strerror(errno), My_name);
111 	fclose(fd);
112 	fd = fopen( My_name, "r+" );
113 	if( fd == NULL )
114 		Alarm( EXIT, "Log_alive: error (%s) could not open file %s\n",strerror(errno), My_name );
115 	if( fseek( fd, file_pos, SEEK_SET ) )
116                 Alarm( EXIT, "Log_alive: error (%s) in fseek file_pos (%ld) on %s\n", strerror(errno), file_pos,  My_name);
117 
118 	E_queue( Log_alive, 0, NULL, alive_time );
119 }
120 
Log_membership()121 void 	Log_membership()
122 {
123 	configuration	*Cn;
124 	int32		proc_id;
125 	proc		dummy_p;
126 	int		i,j;
127 	int		found;
128 
129 	if( !Is_inited ) return;
130 #if ( SPREAD_PROTOCOL == 3 )
131 	fprintf( fd, "M %13ld %13d %11d > ",
132 		E_get_time().sec, Memb_id().time, Highest_seq );
133 #else
134 	fprintf( fd, "M %13ld %13d > ",
135 		E_get_time().sec, Memb_id_for_Network().time );
136 #endif
137 	found = -1;
138 	Cn = Conf_ref();
139 	for( i=0; i < Cn->num_segments; i++ )
140 	{
141 	    for( j=0; j < Cn->segments[i].num_procs; j++ )
142 	    {
143 		proc_id = Cn->segments[i].procs[j]->id;
144 		if( Conf_id_in_conf( Memb_active_ptr(), proc_id ) != -1 )
145 		{
146 		    if( found == -1 ) found = Conf_proc_by_id( proc_id, &dummy_p );
147 		    fprintf( fd, " %2d", found );
148 		}else fprintf( fd, " --" );
149 	    }
150 	}
151 
152 	fprintf( fd, "\n");
153 	Log_alive(0, NULL);
154 }
155 
Log_sess_connect(mailbox mbox,int32 address,char * private_group)156 void	Log_sess_connect( mailbox mbox, int32 address, char *private_group )
157 {
158 	char 	ip[16];
159 
160 	if( !Is_inited ) return;
161 
162 	Conf_id_to_str( address, ip );
163 #if ( SPREAD_PROTOCOL == 3 )
164 	fprintf( fd, "C %13ld %11d %3d %-16s %-10s\n",
165 		E_get_time().sec, Highest_seq, mbox, ip, private_group );
166 #else
167 	fprintf( fd, "C %13ld %3d %-16s %-10s\n",
168 		E_get_time().sec, mbox, ip, private_group );
169 #endif
170 	Log_alive(0, NULL);
171 }
172 
Log_sess_disconnect(mailbox mbox,int32 address,char * private_group,int num_mess)173 void	Log_sess_disconnect( mailbox mbox, int32 address, char *private_group, int num_mess )
174 {
175 	char 	ip[16];
176 
177 	if( !Is_inited ) return;
178 
179 	Conf_id_to_str( address, ip );
180 #if ( SPREAD_PROTOCOL == 3 )
181 	fprintf( fd, "D %13ld %11d %3d %-16s %-10s %d\n",
182 		E_get_time().sec, Highest_seq, mbox, ip, private_group, num_mess );
183 #else
184 	fprintf( fd, "D %13ld %3d %-16s %-10s %d\n",
185 		E_get_time().sec, mbox, ip, private_group, num_mess );
186 #endif
187 	Log_alive(0, NULL);
188 }
189