summaryrefslogtreecommitdiff
path: root/usr/src/lib/libast/common/sfio/sfdisc.c
blob: cf390d1160b57d20e002b0d1fd7c80f9d1ede14f (plain)
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
267
268
269
270
271
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1985-2009 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                   Phong Vo <kpv@research.att.com>                    *
*                                                                      *
***********************************************************************/
#include	"sfhdr.h"

/*	Add a new discipline to the discipline stack. Each discipline
**	provides alternative I/O functions that are analogues of the
**	system calls.
**
**	When the application fills or flushes the stream buffer, data
**	will be processed through discipline functions. A case deserving
**	consideration is stacking a discipline onto a read stream. Each
**	discipline operation implies buffer synchronization so the stream
**	buffer should be empty. However, a read stream representing an
**	unseekable device (eg, a pipe) may not be synchronizable. In that
**	case, any buffered data must then be fed to the new discipline
**	to preserve data processing semantics. This is done by creating
**	a temporary discipline to cache such buffered data and feed
**	them to the new discipline when its readf() asks for new data.
**	Care must then be taken to remove this temporary discipline
**	when it runs out of cached data.
**
**	Written by Kiem-Phong Vo
*/

typedef struct _dccache_s
{	Sfdisc_t	disc;
	uchar*		data;
	uchar*		endb;
} Dccache_t;

#if __STD_C
static int _dccaexcept(Sfio_t* f, int type, Void_t* val, Sfdisc_t* disc)
#else
static int _dccaexcept(f,type,val,disc)
Sfio_t*		f;
int		type;
Void_t*		val;
Sfdisc_t*	disc;
#endif
{
	if(disc && type == SF_FINAL)
		free(disc);
	return 0;
}

#if __STD_C
static ssize_t _dccaread(Sfio_t* f, Void_t* buf, size_t size, Sfdisc_t* disc)
#else
static ssize_t _dccaread(f, buf, size, disc)
Sfio_t*		f;
Void_t*		buf;
size_t		size;
Sfdisc_t*	disc;
#endif
{
	ssize_t		sz;
	Sfdisc_t	*prev;
	Dccache_t	*dcca;

	if(!f) /* bad stream */
		return -1;

	/* make sure that this is on the discipline stack */
	for(prev = f->disc; prev; prev = prev->disc)
		if(prev->disc == disc)
			break;
	if(!prev)
		return -1;

	if(size <= 0) /* nothing to do */
		return size;

	/* read from available data */
	dcca = (Dccache_t*)disc;
	if((sz = dcca->endb - dcca->data) > (ssize_t)size)
		sz = (ssize_t)size;
	memcpy(buf, dcca->data, sz);

	if((dcca->data += sz) >= dcca->endb) /* free empty cache */
	{	prev->disc = disc->disc;
		free(disc);
	}

	return sz;
}

#if __STD_C
Sfdisc_t* sfdisc(Sfio_t* f, Sfdisc_t* disc)
#else
Sfdisc_t* sfdisc(f,disc)
Sfio_t*		f;
Sfdisc_t*	disc;
#endif
{
	Sfdisc_t	*d, *rdisc;
	Sfread_f	oreadf;
	Sfwrite_f	owritef;
	Sfseek_f	oseekf;
	ssize_t		n;
	Dccache_t	*dcca = NIL(Dccache_t*);
	SFMTXDECL(f);

	SFMTXENTER(f, NIL(Sfdisc_t*));

	if((Sfio_t*)disc == f) /* special case to get the top discipline */
		SFMTXRETURN(f,f->disc);

	if((f->flags&SF_READ) && f->proc && (f->mode&SF_WRITE) )
	{	/* make sure in read mode to check for read-ahead data */
		if(_sfmode(f,SF_READ,0) < 0)
			SFMTXRETURN(f, NIL(Sfdisc_t*));
	}
	else
	{	if((f->mode&SF_RDWR) != f->mode && _sfmode(f,0,0) < 0)
			SFMTXRETURN(f, NIL(Sfdisc_t*));
	}

	SFLOCK(f,0);
	rdisc = NIL(Sfdisc_t*);

	/* disallow popping while there is cached data */
	if(!disc && f->disc && f->disc->disc && f->disc->disc->readf == _dccaread )
		goto done;

	/* synchronize before switching to a new discipline */
	if(!(f->flags&SF_STRING))
	{	(void)SFSYNC(f); /* do a silent buffer synch */
		if((f->mode&SF_READ) && (f->mode&SF_SYNCED) )
		{	f->mode &= ~SF_SYNCED;
			f->endb = f->next = f->endr = f->endw = f->data;
		}

		/* if there is buffered data, ask app before proceeding */
		if(((f->mode&SF_WRITE) && (n = f->next-f->data) > 0) ||
		   ((f->mode&SF_READ) && (n = f->endb-f->next) > 0) )
		{	int	rv = 0;
			if(rv == 0 && f->disc && f->disc->exceptf) /* ask current discipline */
			{	SFOPEN(f,0);
				rv = (*f->disc->exceptf)(f, SF_DBUFFER, &n, f->disc);
				SFLOCK(f,0);
			}
			if(rv == 0 && disc && disc->exceptf) /* ask discipline being pushed */
			{	SFOPEN(f,0);
				rv = (*disc->exceptf)(f, SF_DBUFFER, &n, disc);
				SFLOCK(f,0);
			}
			if(rv < 0)
				goto done;
		}

		/* trick the new discipline into processing already buffered data */
		if((f->mode&SF_READ) && n > 0 && disc && disc->readf )
		{	if(!(dcca = (Dccache_t*)malloc(sizeof(Dccache_t)+n)) )
				goto done;
			memclear(dcca, sizeof(Dccache_t));

			dcca->disc.readf = _dccaread;
			dcca->disc.exceptf = _dccaexcept;

			/* move buffered data into the temp discipline */
			dcca->data = ((uchar*)dcca) + sizeof(Dccache_t);
			dcca->endb = dcca->data + n;
			memcpy(dcca->data, f->next, n);
			f->endb = f->next = f->endr = f->endw = f->data;
		}
	}

	/* save old readf, writef, and seekf to see if stream need reinit */
#define GETDISCF(func,iof,type) \
	{ for(d = f->disc; d && !d->iof; d = d->disc) ; \
	  func = d ? d->iof : NIL(type); \
	}
	GETDISCF(oreadf,readf,Sfread_f);
	GETDISCF(owritef,writef,Sfwrite_f);
	GETDISCF(oseekf,seekf,Sfseek_f);

	if(disc == SF_POPDISC)
	{	/* popping, warn the being popped discipline */
		if(!(d = f->disc) )
			goto done;
		disc = d->disc;
		if(d->exceptf)
		{	SFOPEN(f,0);
			if((*(d->exceptf))(f,SF_DPOP,(Void_t*)disc,d) < 0 )
				goto done;
			SFLOCK(f,0);
		}
		f->disc = disc;
		rdisc = d;
	}
	else
	{	/* pushing, warn being pushed discipline */
		do
		{	/* loop to handle the case where d may pop itself */
			d = f->disc;
			if(d && d->exceptf)
			{	SFOPEN(f,0);
				if( (*(d->exceptf))(f,SF_DPUSH,(Void_t*)disc,d) < 0 )
					goto done;
				SFLOCK(f,0);
			}
		} while(d != f->disc);

		/* make sure we are not creating an infinite loop */
		for(; d; d = d->disc)
			if(d == disc)
				goto done;

		/* set new disc */
		if(dcca) /* insert the discipline with cached data */
		{	dcca->disc.disc = f->disc;
			disc->disc = &dcca->disc;
		}
		else	disc->disc = f->disc;
		f->disc = disc;
		rdisc = disc;
	}

	if(!(f->flags&SF_STRING) )
	{	/* this stream may have to be reinitialized */
		reg int	reinit = 0;
#define DISCF(dst,iof,type)	(dst ? dst->iof : NIL(type)) 
#define REINIT(oiof,iof,type) \
		if(!reinit) \
		{	for(d = f->disc; d && !d->iof; d = d->disc) ; \
			if(DISCF(d,iof,type) != oiof) \
				reinit = 1; \
		}

		REINIT(oreadf,readf,Sfread_f);
		REINIT(owritef,writef,Sfwrite_f);
		REINIT(oseekf,seekf,Sfseek_f);

		if(reinit)
		{	SETLOCAL(f);
			f->bits &= ~SF_NULL;	/* turn off /dev/null handling */
			if((f->bits&SF_MMAP) || (f->mode&SF_INIT))
				sfsetbuf(f,NIL(Void_t*),(size_t)SF_UNBOUND);
			else if(f->data == f->tiny)
				sfsetbuf(f,NIL(Void_t*),0);
			else
			{	int	flags = f->flags;
				sfsetbuf(f,(Void_t*)f->data,f->size);
				f->flags |= (flags&SF_MALLOC);
			}
		}
	}

done :
	SFOPEN(f,0);
	SFMTXRETURN(f, rdisc);
}