File: mu_op_open.c

package info (click to toggle)
fis-gtm 7.1-006-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,908 kB
  • sloc: ansic: 344,906; asm: 5,184; csh: 4,859; sh: 2,000; awk: 294; makefile: 73; sed: 13
file content (414 lines) | stat: -rw-r--r-- 11,331 bytes parent folder | download | duplicates (2)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/****************************************************************
 *								*
 * Copyright (c) 2001-2023 Fidelity National Information	*
 * Services, Inc. and/or its subsidiaries. All rights reserved.	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

#include "mdef.h"

#include "gtm_fcntl.h"
#include "gtm_stdio.h"
#include "gtm_string.h"
#include "gtm_unistd.h"
#include "gtm_stat.h"
#include "gtm_iconv.h"

#include <errno.h>
#include <sys/ioctl.h>

#include "io.h"
#include "iosp.h"
#include "io_params.h"
#include "cryptdef.h"
#include "iotimer.h"
#include "gt_timer.h"
#include "copy.h"
#include "iottdef.h"
#include "iormdef.h"
#include "mupip_io_dev_dispatch.h"
#include "eintr_wrappers.h"
#include "mmemory.h"
#include "mu_op_open.h"
#include "trans_log_name.h"
#include "gtm_conv.h"
#include "gtm_utf8.h"
#include "gtmimagename.h"

#define  LOGNAME_LEN 255

GBLREF io_desc			*active_device;
GBLREF bool			licensed;
GBLREF dev_dispatch_struct  	io_dev_dispatch_mupip[];
GBLREF int4			lkid,lid;
GBLREF mstr			sys_input, sys_output;

error_def(LP_NOTACQ);			/* bad license 		  */
error_def(ERR_LOGTOOLONG);
error_def(ERR_SYSCALL);

LITREF mstr			chset_names[];
LITREF	unsigned char		io_params_size[];

static boolean_t mu_open_try(io_log_name *, io_log_name *, mval *, mval *);

/*	The third parameter is dummy to keep the inteface same as op_open	*/
int mu_op_open(mval *v, mval *p, mval *t, mval *mspace)
{
	char		buf1[MAX_TRANS_NAME_LEN]; /* buffer to hold translated name */
	int4		stat;		/* status */
	io_log_name	*naml;		/* logical record for passed name */
	io_log_name	*tl;		/* logical record for translated name */
	mstr		tn;			/* translated name 	  */

	MV_FORCE_STR(v);
	MV_FORCE_STR(p);
	if (mspace)
		MV_FORCE_STR(mspace);
	assert((unsigned char)*p->str.addr < n_iops);
	naml = get_log_name(&v->str, INSERT);
	if (0 != naml->iod)
		tl = naml;
	else
	{
#		ifdef	NOLICENSE
		licensed= TRUE ;
#		else
		CRYPT_CHKSYSTEM;
		if (!licensed || LP_CONFIRM(lid,lkid)==LP_NOTACQ)
			licensed= FALSE ;
#		endif
		switch (stat = TRANS_LOG_NAME(&v->str, &tn, &buf1[0], SIZEOF(buf1), dont_sendmsg_on_log2long))
		{
		case SS_NORMAL:
			tl = get_log_name(&tn, INSERT);
			break;
		case SS_NOLOGNAM:
			tl = naml;
			break;
		case SS_LOG2LONG:
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(5) ERR_LOGTOOLONG, 3, v->str.len, v->str.addr, SIZEOF(buf1) - 1);
			GTM_UNREACHABLE();
			break;
		default:
			rts_error_csa(CSA_ARG(NULL) VARLSTCNT(1) stat);
			GTM_UNREACHABLE();
		}
	}
	stat = mu_open_try(naml, tl, p, mspace);
	return (stat);
}

static boolean_t mu_open_try(io_log_name *naml, io_log_name *tl, mval *pp, mval *mspace)
{
	boolean_t	ichset_specified, ochset_specified, filecreated = FALSE;
	char		*buf, namebuf[LOGNAME_LEN + 1];
	d_rm_struct	*d_rm;
	int		char_or_block_special, file_des, fstat_res, oflag, p_offset, save_errno = 0, umask_creat, umask_orig;
	int4		recordsize, status;
	io_desc		*iod;
	mstr		chset_mstr;
	mstr		tn;		/* translated name */
	struct stat	outbuf;
	unsigned char	ch;

	char_or_block_special = FALSE;
	file_des = -2;
	oflag = 0;
	tn.len = tl->len;
	if (tn.len > LOGNAME_LEN)
		tn.len = LOGNAME_LEN;
	tn.addr = tl->dollar_io;
	memcpy(namebuf, tn.addr, tn.len);
	namebuf[tn.len] = '\0';
	buf = namebuf;
	if (0 == naml->iod)
	{
		if (0 == tl->iod)
		{
			iod = (io_desc *)malloc(SIZEOF(io_desc));
			memset((char*)iod, 0, SIZEOF(io_desc));
			iod->pair.in  = iod;
			iod->pair.out = iod;
			iod->trans_name = tl;
			iod->type = n_io_dev_types;
			p_offset = 0;
			while (iop_eol != *(pp->str.addr + p_offset))
			{
				ch = *(pp->str.addr + p_offset++);
				if (iop_sequential == ch)
					iod->type = rm;
				if (IOP_VAR_SIZE == io_params_size[ch])
					p_offset += (int)(unsigned char)*(pp->str.addr + p_offset) + 1;
				else
					p_offset += io_params_size[ch];
			}
			tl->iod = iod;
		} else
			iod = tl->iod;
		if ((n_io_dev_types == iod->type) && mspace && mspace->str.len)
			iod->type = us;
		if (n_io_dev_types == iod->type)
		{
			if (0 == memvcmp(tn.addr, tn.len, sys_input.addr, sys_input.len))
			{
				file_des = 0;
				FSTAT_FILE(file_des, &outbuf, fstat_res);
				if (-1 == fstat_res)
				{
					save_errno = errno;
					RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(8) ERR_SYSCALL, 5,
						RTS_ERROR_LITERAL("fstat()"),
						CALLFROM, save_errno);
				}
			} else
			{
				if (0 == memvcmp(tn.addr, tn.len, sys_output.addr, sys_output.len))
				{
					file_des = 1;
					FSTAT_FILE(file_des, &outbuf, fstat_res);
					if (-1 == fstat_res)
					{
						save_errno = errno;
						RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(8) ERR_SYSCALL, 5,
							RTS_ERROR_LITERAL("fstat()"),
							CALLFROM, save_errno);
					}
				} else if (0 == memvcmp(tn.addr, tn.len, DEVNULL, 9))
					iod->type = nl;
				else if ((-1 == Stat(buf, &outbuf)) && (n_io_dev_types == iod->type))
				{

					if (ENOENT == errno)
					{
						iod->type = rm;
						filecreated = TRUE;
					}
					else
					{
						save_errno = errno;
						RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(8) ERR_SYSCALL, 5,
							RTS_ERROR_LITERAL("fstat()"),
							CALLFROM, save_errno);
					}
				}
			}
		}
		if (n_io_dev_types == iod->type)
		{
			switch (outbuf.st_mode & S_IFMT)
			{
				case S_IFCHR:
				case S_IFBLK:
					char_or_block_special = TRUE;
					break;
				case S_IFIFO:
					iod->type = ff;
					break;
				case S_IFREG:
				case S_IFDIR:
					iod->type = rm;
					break;
				case S_IFSOCK:
				case 0:
					iod->type = ff;
					break;
				default:
					break;
			}
		}
		naml->iod = iod;
	} else
		iod = naml->iod;
	active_device = iod;
	if ((-2 == file_des) && (dev_open != iod->state) && (us != iod->type))
	{
		oflag |= (O_RDWR | O_CREAT | O_NOCTTY);
		p_offset = 0;
		ichset_specified = ochset_specified = FALSE;
		while ((iop_eol != *(pp->str.addr + p_offset)) && (pp->str.len > p_offset))
		{
			ch = *(pp->str.addr + p_offset++);
			assert((params)ch < (params)n_iops);
			assert(pp->str.len > p_offset);
			switch (ch)
			{
				case iop_append:
					if (rm == iod->type)
						oflag |= O_APPEND;
					break;
				case iop_contiguous:
					break;
				case iop_newversion:
					if ((dev_open != iod->state) && (rm == iod->type))
						oflag |= O_TRUNC;
					break;
				case iop_readonly:
					oflag  &=  ~(O_RDWR | O_CREAT | O_WRONLY);
					oflag  |=  O_RDONLY;
					break;
				case iop_writeonly:
					oflag  &= ~(O_RDWR | O_RDONLY);
					oflag  |= O_WRONLY | O_CREAT;
					break;
				case iop_ipchset:
					assert(pp->str.len > (p_offset + 1));
#					ifdef KEEP_zOS_EBCDIC
					if ( (iconv_t)0 != iod->input_conv_cd )
					{
						ICONV_CLOSE_CD(iod->input_conv_cd);
					}
					SET_CODE_SET(iod->in_code_set,
						     (char *)(pp->str.addr + p_offset + 1));
					if (DEFAULT_CODE_SET != iod->in_code_set)
						ICONV_OPEN_CD(iod->input_conv_cd,
							      (char *)(pp->str.addr + p_offset + 1), INSIDE_CH_SET);
					break;
#					endif
					if (gtm_utf8_mode)
					{
						chset_mstr.addr = (char *)(pp->str.addr + p_offset + 1);
						chset_mstr.len = (int)(unsigned char)*(pp->str.addr + p_offset);
						SET_ENCODING(iod->ichset, &chset_mstr);
						ichset_specified = TRUE;
					}
					break;
				case iop_opchset:
					assert(pp->str.len > (p_offset + 1));
#					ifdef KEEP_zOS_EBCDIC
					if ( (iconv_t)0 != iod->output_conv_cd)
					{
						ICONV_CLOSE_CD(iod->output_conv_cd);
					}
					SET_CODE_SET(iod->out_code_set,
						     (char *)(pp->str.addr + p_offset + 1));
					if (DEFAULT_CODE_SET != iod->out_code_set)
						ICONV_OPEN_CD(iod->output_conv_cd, INSIDE_CH_SET,
							      (char *)(pp->str.addr + p_offset + 1));
					break;
#					endif
					if (gtm_utf8_mode)
					{
						chset_mstr.addr = (char *)(pp->str.addr + p_offset + 1);
						chset_mstr.len = (int)(unsigned char)*(pp->str.addr + p_offset);
						SET_ENCODING(iod->ochset, &chset_mstr);
						ochset_specified = TRUE;
					}
					break;
				case iop_m:
				case iop_utf8:
				case iop_utf16:
				case iop_utf16be:
				case iop_utf16le:
					if (gtm_utf8_mode)
					{
						iod->ichset = iod->ochset =
							(iop_m       == ch) ? CHSET_M :
							(iop_utf8    == ch) ? CHSET_UTF8 :
							(iop_utf16   == ch) ? CHSET_UTF16 :
							(iop_utf16be == ch) ? CHSET_UTF16BE : CHSET_UTF16LE;
						ichset_specified = ochset_specified = TRUE;
					}
					break;
				default:
					break;
			}
			p_offset += ((IOP_VAR_SIZE == io_params_size[ch]) ?
				(unsigned char)*(pp->str.addr + p_offset) + 1 : io_params_size[ch]);
		}
		if (!ichset_specified)
			iod->ichset = (gtm_utf8_mode) ? CHSET_UTF8 : CHSET_M;
		if (!ochset_specified)
			iod->ochset = (gtm_utf8_mode) ? CHSET_UTF8 : CHSET_M;
		if (IS_UTF_CHSET(iod->ichset) && CHSET_UTF16 != iod->ichset)
			get_chset_desc(&chset_names[iod->ichset]);
		if (IS_UTF_CHSET(iod->ochset) && CHSET_UTF16 != iod->ochset)
			get_chset_desc(&chset_names[iod->ochset]);
		/* RW permissions for owner and others as determined by umask. */
		umask_orig = umask(000);	/* determine umask (destructive) */
		(void)umask(umask_orig);	/* reset umask */
		umask_creat = 0666 & ~umask_orig;
		/* the check for EINTR below is valid and should not be converte d to an EINTR
		 * wrapper macro, due to the other errno values being checked.
		 */
		while ((-1 == (file_des = OPEN3(buf, oflag, umask_creat))))
		{
			if (   EINTR == errno
			       || ETXTBSY == errno
			       || ENFILE == errno
			       || EBUSY == errno
			       || ((mb == iod->type) && (ENXIO == errno)))
				continue;
			else
				break;
		}
		if (-1 == file_des)
			return FALSE;
	}
#ifdef KEEP_zOS_EBCDIC
	SET_CODE_SET(iod->in_code_set, OUTSIDE_CH_SET);
	if (DEFAULT_CODE_SET != iod->in_code_set)
		ICONV_OPEN_CD(iod->input_conv_cd, OUTSIDE_CH_SET, INSIDE_CH_SET);
	SET_CODE_SET(iod->out_code_set, OUTSIDE_CH_SET);
	if (DEFAULT_CODE_SET != iod->out_code_set)
		ICONV_OPEN_CD(iod->output_conv_cd, INSIDE_CH_SET, OUTSIDE_CH_SET);
#endif
	assert(-1 != file_des);
	if (n_io_dev_types == iod->type)
	{
		if (isatty(file_des))
			iod->type = tt;
		else if (char_or_block_special && (2 < file_des))
		{	/* assume mag tape */
			iod->type = mt;
			assert(FALSE);
		}else
			iod->type = rm;
	}
	assert(iod->type < n_io_dev_types);
	iod->disp_ptr = &io_dev_dispatch_mupip[iod->type];
	active_device = iod;
	if (dev_never_opened == iod->state)
	{
		iod->wrap = DEFAULT_IOD_WRAP;
		iod->width = DEFAULT_IOD_WIDTH;
		iod->length = DEFAULT_IOD_LENGTH;
		iod->write_filter = 0; /* MUPIP should not use FILTER */
	}
	if (dev_open != iod->state)
	{
		iod->dollar.x = 0;
		iod->dollar.y = 0;
		iod->dollar.za = 0;
		iod->dollar.zb[0] = 0;
	}
	iod->newly_created = filecreated;
	status = (iod->disp_ptr->open)(naml, pp, file_des, mspace, NO_M_TIMEOUT);
	if (TRUE == status)
	{
		iod->state = dev_open;
		if (rm == iod->type)
		{
			d_rm = (d_rm_struct *)iod->dev_sp;
			if (!d_rm->def_recsize && d_rm->def_width)
			{
				iod->width = d_rm->recordsize;
				d_rm->def_width = FALSE;
			}
		}
	}
	else if (dev_open == iod->state)
		iod->state = dev_closed;
	if (1 == file_des)
		iod->dollar.zeof = TRUE;
	active_device = 0;
	iod->newly_created = FALSE;
	if (IS_MCODE_RUNNING)
		return (status);
	return TRUE;
}