[go: up one dir, main page]

File: expire.c

package info (click to toggle)
autodir 0.99.8-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 1,920 kB
  • ctags: 775
  • sloc: sh: 8,544; ansic: 7,297; xml: 431; makefile: 104
file content (266 lines) | stat: -rw-r--r-- 5,519 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

/*

Copyright (C) (2004 - 2006) (Venkata Ramana Enaganti) <ramana@intraperson.com>

This program is free software; you can redistribute it and/or 
modify it under the terms of the GNU General Public License 
as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later 
version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/


#include <stdio.h>
#include <pthread.h>
#include <limits.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include "auto_fs4.h"

#include "msg.h"
#include "thread.h"
#include "expire.h"

#define EXPIRE_MAX		500

#define EXPIRE_MAX_THREADS	10

static struct {
	pthread_t main;
	pthread_t threads[ EXPIRE_MAX_THREADS ];
	pthread_mutex_t lock;
	int cur;
	int stop;
	int ioctlfd;
        int *shutdown;
} expire;

#define DEFAULT_LIFE	5

#include <stdlib.h>

#ifdef TEST

/*rand is not thread safe. this is just for testing*/
#define EXPIRE_MULTI_EXTRA() (r = (rand() % 1) ? 0 : -1)
#define EXPIRE_MULTI() (r = (rand() % 1017) ? 0 : -1)

#else

#define EXPIRE_MULTI_EXTRA() \
	(r = ioctl( expire.ioctlfd, AUTOFS_IOC_EXPIRE_MULTI, NULL ))
#define EXPIRE_MULTI() \
	(r = ioctl( expire.ioctlfd, AUTOFS_IOC_EXPIRE_MULTI, NULL ))
#endif

/*extra thread to initiate directory expires*/
static void *extra_expire_mounts( void *x )
{
	int i, r, life = DEFAULT_LIFE;
	int j;
        pthread_t *ptr = ( pthread_t * ) x;

	pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL );

#ifdef TEST
	printf("\nextra_expire_mounts started\n");
#endif

	if( ! *ptr )
	{
		pthread_mutex_lock( &expire.lock );
		pthread_mutex_unlock( &expire.lock );
	}

	for( j = 0 ; j < 100 ; j++ )
	{
		for( i = 0, r = 0 ; ! r ; i++ )
		{
			EXPIRE_MULTI_EXTRA();
		}

		life = i < 2 ?  life - 1 : DEFAULT_LIFE;
		if( ! life )
		{
			pthread_mutex_lock( &expire.lock );
                        if( expire.stop ) {
                                pthread_mutex_unlock( &expire.lock );
                                return NULL;
                        }
                        pthread_detach( *ptr );
			*ptr = 0;
			pthread_mutex_unlock( &expire.lock );

#ifdef TEST
			printf("\nextra_expire_mounts end\n");
#endif
			return NULL;
		}
		sleep( 1 );
	}

	pthread_mutex_lock( &expire.lock );
        if( expire.stop ) {

#ifdef TEST
	printf("\nextra_expire_mounts end\n");
#endif
                pthread_mutex_unlock( &expire.lock );
                return NULL;
        }
        pthread_detach( *ptr );
	*ptr = 0;
	pthread_mutex_unlock( &expire.lock );

#ifdef TEST
	printf("\nextra_expire_mounts end\n");
#endif
	return NULL;
}

static void start_extra_expire_thread( void )
{
	int i;

	for( i = 0 ; i < EXPIRE_MAX_THREADS ; i++ )
	{
		if( ! expire.threads[ expire.cur ] )
		{
			/*not joinable*/
			pthread_mutex_lock( &expire.lock );
			if( thread_new_joinable( extra_expire_mounts,
					&expire.threads[ expire.cur ],
					&expire.threads[ expire.cur ] ) )
			{
				expire.cur = ( expire.cur + 1 )
						% EXPIRE_MAX_THREADS;
			}
			pthread_mutex_unlock( &expire.lock );
			return;
		}
		expire.cur = ( expire.cur + 1 ) % EXPIRE_MAX_THREADS;
	}
	msglog( MSG_NOTICE, "could not start extra expire thread" );
}

static void wait_extra_expirethread( void )
{
        int i;

        pthread_mutex_lock( &expire.lock );
        pthread_mutex_unlock( &expire.lock );
        /*if there are any remaining*/
        for( i = 0 ; i < EXPIRE_MAX_THREADS ; i++ )
        {
                if( expire.threads[ i ] )
                        pthread_join( expire.threads[ i ], NULL );
        }
}

/*separate thread to initiate directory expires*/
static void *main_expire_mounts( void *x )
{
	int i, r;

	pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL );

	while( 1 )
	{
		for( i = 0, r = 0 ; i < EXPIRE_MAX && ! r ; i++ )
		{
			EXPIRE_MULTI();
		}

		if( expire.stop ) {
			if( i == EXPIRE_MAX )
				continue;
                        wait_extra_expirethread();
                        *expire.shutdown = 1;
			return NULL;
                }

		if( i == EXPIRE_MAX )
			start_extra_expire_thread();
		else if( i < 2 )
			sleep( 1 );
	}
	return NULL;
}

/*Under graceful exit, shutdown gloable var is set by expire mechanism.
  That way as many as expires can be done before autodir exit.
*/
void expire_start( int time_out, int ioctlfd, int *shutdown )
{
  	int i;
	
	expire.stop = 0;
	expire.cur = 1;
	expire.ioctlfd = ioctlfd;
        expire.shutdown = shutdown;
        thread_mutex_init(&expire.lock);

	if( ! time_out )
		return;

	for( i = 0; i < EXPIRE_MAX_THREADS; i++ )
		expire.threads[ i ] = 0;

	/*IMP: We start first main thread with NULL data value*/
	if( ! thread_new_joinable( main_expire_mounts, NULL, &expire.main ) )
		msglog( MSG_FATAL, "could not start expire thread" );
}

void expire_stop_set(void)
{
	pthread_mutex_lock( &expire.lock );
	expire.stop = 1;
	pthread_mutex_unlock( &expire.lock );
}

void expire_stop( void )
{
	pthread_join( expire.main, NULL );
}













#ifdef TEST

char *autodir_name(void)
{
    return "test autodir";
}

int main(void)
{
	int shut = 0;

	msg_init();
	expire_start(1, -1, &shut);
	expire_stop();
}

#endif