Artifact [2782d26fa2]
Not logged in

Artifact 2782d26fa29307246d0de3dfad705667d184849f:


     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
   415
   416
   417
   418
   419
   420
   421
   422
   423
   424
   425
   426
   427
   428
   429
   430
   431
   432
   433
   434
   435
   436
   437
   438
   439
   440
   441
   442
   443
   444
   445
   446
   447
   448
   449
   450
   451
   452
   453
   454
   455
   456
   457
   458
   459
   460
   461
   462
   463
   464
   465
   466
   467
   468
   469
   470
   471
   472
   473
   474
   475
   476
   477
   478
   479
   480
   481
   482
   483
   484
   485
   486
   487
   488
   489
   490
   491
   492
   493
   494
   495
   496
   497
   498
   499
   500
   501
   502
   503
   504
   505
   506
   507
   508
   509
   510
   511
   512
   513
   514
   515
   516
   517
   518
   519
   520
   521
   522
   523
   524
   525
   526
   527
   528
   529
   530
   531
   532
   533
   534
   535
   536
   537
   538
   539
   540
   541
   542
   543
   544
   545
   546
   547
   548
   549
   550
   551
/*	sys2.c	4.7	81/04/13	*/

#include "../h/param.h"
#include "../h/systm.h"
#include "../h/dir.h"
#include "../h/user.h"
#include "../h/reg.h"
#include "../h/file.h"
#include "../h/inode.h"
#include "../h/pte.h"
#include "../h/vm.h"
#include "../h/buf.h"
#include "../h/mtpr.h"
#include "../h/proc.h"
#include "../h/inline.h"

/*
 * READ AND WRITE ARE NEAR DUPLICATES OF EACH OTHER TO SAVE
 * AS MUCH TIME AS POSSIBLE.
 */

/*
 * read system call
 */
read()
{
	register struct file *fp;
	register struct inode *ip;
	register struct a {
		int	fdes;
		char	*cbuf;
		unsigned count;
	} *uap;

	uap = (struct a *)u.u_ap;
	if ((int)uap->count < 0) {
		u.u_error = EINVAL;
		return;
	}
	GETF(fp, uap->fdes);
	if((fp->f_flag&FREAD) == 0) {
		if((fp->f_flag & FHUNGUP) == 0) /* EOF on hungup chans */
			u.u_error = EBADF;
		else if (++u.u_nbadio > 64) {
			if (u.u_nbadio > 128)
				psignal(u.u_procp, SIGKILL);
			else
				psignal(u.u_procp, SIGPIPE);
		}
		return;
	}
	u.u_base = (caddr_t)uap->cbuf;
	u.u_count = uap->count;
	u.u_segflg = 0;
	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
		if (u.u_count == uap->count)
			u.u_eosys = RESTARTSYS;
	} else {
		ip = fp->f_inode;
		u.u_offset = fp->f_offset;
		if((ip->i_mode&(IFCHR&IFBLK)) == 0 && ip->i_sptr==NULL) {
			plock(ip);
			readi(ip);
			prele(ip);
		} else
			readi(ip);
		fp->f_offset += uap->count-u.u_count;
	}
	u.u_r.r_val1 = uap->count-u.u_count;
}

/*
 * write system call
 */
write()
{
	register struct file *fp;
	register struct inode *ip;
	register struct a {
		int	fdes;
		char	*cbuf;
		unsigned count;
	} *uap;

	uap = (struct a *)u.u_ap;
	if ((int)uap->count < 0) {
		u.u_error = EINVAL;
		return;
	}
	GETF(fp, uap->fdes);
	if((fp->f_flag&FWRITE) == 0) {
		if (fp->f_flag&FHUNGUP) {
			u.u_error = EPIPE;
			psignal(u.u_procp, SIGPIPE);
			return;
		}
		u.u_error = EBADF;
		return;
	}
	u.u_base = (caddr_t)uap->cbuf;
	u.u_count = uap->count;
	u.u_segflg = 0;
	if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
		if (u.u_count == uap->count)
			u.u_eosys = RESTARTSYS;
	} else {
		ip = fp->f_inode;
		u.u_offset = fp->f_offset;
		if((ip->i_mode&(IFCHR&IFBLK)) == 0 && ip->i_sptr==0) {
			plock(ip);
			writei(ip);
			prele(ip);
		} else
			writei(ip);
		fp->f_offset += uap->count-u.u_count;
	}
	u.u_r.r_val1 = uap->count-u.u_count;
}

/*
 * open system call
 */
open()
{
	register struct inode *ip;
	register struct a {
		char	*fname;
		int	rwmode;
	} *uap;

	uap = (struct a *)u.u_ap;
	ip = namei(uchar, 0, 1);
	if(ip == NULL)
		return;
	open1(ip, ++uap->rwmode, 0);
}

/*
 * creat system call
 */
creat()
{
	register struct inode *ip;
	register struct a {
		char	*fname;
		int	fmode;
	} *uap;
	struct argnamei nmarg;

	uap = (struct a *)u.u_ap;
	nmarg.flag = NI_CREAT;
	nmarg.mode = uap->fmode & 07777 & ~ISVTX;
	nmarg.ino = 0;
	ip = namei(uchar, &nmarg, 1);
	if(ip == NULL) 
		return;
	open1(ip, FWRITE, nmarg.ino == 0? 1: 2);
}

/*
 * common code for open and creat.
 * Check permissions, allocate an open file structure,
 * and call the device open routine if any.
 */
open1(ip, mode, trf)
register struct inode *ip;
register mode;
{
	register struct file *fp;
	register struct inode *nip;
	int i;
	extern int tty_no, stdio_no;

	/* handle /dev/stdio */
	if ((ip->i_mode&IFMT)==IFCHR && major(ip->i_un.i_rdev) == stdio_no) {
		register struct file *fp;
		i = minor(ip->i_un.i_rdev);
		iput(ip);
		fp = getf(i);
		if (fp == NULL)
			return;
		if ((i = ufalloc()) < 0)
			return;
		u.u_ofile[i] = fp;
		fp->f_count++;
		return;
	}
	if(trf != 2) {
		if(mode&FREAD)
			(void) access(ip, IREAD);
		if(mode&FWRITE) {
			(void) access(ip, IWRITE);
			if((ip->i_mode&IFMT) == IFDIR)
				u.u_error = EISDIR;
		}
	}
	if(u.u_error)
		goto out;
	if(trf == 1)
		itrunc(ip);
	prele(ip);
	if ((fp = falloc()) == NULL)
		goto out;
	fp->f_flag = mode&(FREAD|FWRITE);
	fp->f_inode = ip;
	i = u.u_r.r_val1;
	nip = openi(ip, mode&(FREAD|FWRITE));
	if(u.u_error == 0) {
		if (nip)		/* substitute new inode? */
			fp->f_inode = nip;
		return;
	}
	if (nip)
		panic("nip !=  null in open1");
	u.u_ofile[i] = NULL;
	fp->f_count--;

out:
	iput(ip);
}

/* Mkdir system call */
mkdir()
{	register struct inode *ip;
	register struct a {
		char *fname;
		int fmode;
	} *uap;
	struct argnamei nmarg;
	struct direct x[2];
	int i;

	uap = (struct a *)u.u_ap;
	nmarg.flag = NI_MKDIR;
	nmarg.mode = (uap->fmode & 0777) | IFDIR;
	nmarg.ino = 0;
	ip = namei(uchar, &nmarg, 0);
	if(ip == NULL)
		return;
	if(nmarg.ino == 0) {
		if(u.u_error == 0)
			u.u_error = EEXIST;
		iput(ip);
		return;
	}
	x[0].d_ino = ip->i_number;
	x[1].d_ino = nmarg.ino;
	for(i = 0; i < DIRSIZ; i++)
		x[0].d_name[i] = x[1].d_name[i] = 0;
	x[0].d_name[0] = x[1].d_name[0] = x[1].d_name[1] = '.';
	u.u_count = 2 * sizeof(struct direct);
	u.u_base = (caddr_t)x;
	u.u_offset = 0;
	u.u_segflg = 1;
	iupdat(ip, &time, &time, 1);
	writei(ip);
	iput(ip);
}

/* Rmdir system call */
rmdir()
{	struct a {
		char *fname;
	};
	struct argnamei nmarg;

	nmarg.flag = NI_RMDIR;
	(void) namei(uchar, &nmarg, 0);
}

/*
 * Select system call.
 */
int	nselcoll;
int	selwait;

select()
{
	register struct uap  {
		int	nfd;
		fd_set	*rp, *wp;
		int	timo;
	} *ap = (struct uap *)u.u_ap;
	fd_set rd, wr, readable, writeable;
	int nfds = 0;
	time_t t = time;
	int i, s, tsel, ncoll, rem;
	unsigned len;
	fd_set selscan();

	if (ap->nfd > NOFILE)
		ap->nfd = NOFILE;
	if (ap->nfd < 0) {
		u.u_error = EBADF;
		return;
	}

	/* read in only as many words as we need */
	len = ((ap->nfd+NBPW*NBBY-1)/(NBPW*NBBY))*NBPW;
	if (ap->rp && copyin((caddr_t)ap->rp, (caddr_t)&rd, len))
		return;
	if (ap->wp && copyin((caddr_t)ap->wp, (caddr_t)&wr, len))
		return;
retry:
	ncoll = nselcoll;
	u.u_procp->p_flag |= SSEL;
	if (ap->rp)
		readable = selscan(ap->nfd, &rd, &nfds, FREAD);
	if (ap->wp)
		writeable = selscan(ap->nfd, &wr, &nfds, FWRITE);
	if (u.u_error)
		goto done;
	if (nfds)
		goto done;
	rem = (ap->timo+999)/1000 - (time - t);
	if (ap->timo == 0 || rem <= 0)
		goto done;
	s = spl6();
	if ((u.u_procp->p_flag & SSEL) == 0 || nselcoll != ncoll) {
		u.u_procp->p_flag &= ~SSEL;
		splx(s);
		goto retry;
	}
	u.u_procp->p_flag &= ~SSEL;
	tsel = tsleep((caddr_t)&selwait, PZERO+1, rem);
	splx(s);
	switch (tsel) {

	case TS_OK:
		goto retry;

	case TS_SIG:
		u.u_error = EINTR;
		return;

	case TS_TIME:
		break;
	}
done:
	u.u_r.r_val1 = nfds;
	if (ap->rp)
		(void)copyout((caddr_t)&readable, (caddr_t)ap->rp, len);
	if (ap->wp)
		(void)copyout((caddr_t)&writeable, (caddr_t)ap->wp, len);
}

fd_set
selscan(nfd, fdsp, nfdp, flag)
int nfd;
fd_set *fdsp;
int *nfdp, flag;
{
	struct file *fp;
	struct inode *ip;
	unsigned int off, i, fd, able;
	unsigned long bits;
	fd_set	res;

	/* loop through all words of the set */
	for (off=0; off<FDWORDS; off++) {

		/* loop once for each bit on in a word of the set */
		res.fds_bits[off] = 0;
		bits = fdsp->fds_bits[off];
		while (i = ffs(bits)) {
			fd = off*(NBPW*NBBY)+i-1;
			if (fd >= nfd)
				return res;
			bits &= ~(1<<(i-1));
			fp = u.u_ofile[fd];
			if (fp == NULL) {
				u.u_error = EBADF;
				return res;
			}
			ip = fp->f_inode;
			able = 1;
			if (ip->i_sptr)
				able = stselect(ip->i_sptr, flag, *nfdp);
			if (able) {
				res.fds_bits[off] |= (1<<(i-1));
				(*nfdp)++;
			}
		}
	}
	return res;
}

selwakeup(p, coll)
register struct proc *p;
int coll;
{
	int s;

	if (coll) {
		nselcoll++;
		wakeup((caddr_t)&selwait);
	}
	if (p) {
		s = spl6();
		if (p->p_wchan == (caddr_t)&selwait)
			setrun(p);
		else
			p->p_flag &= ~SSEL;
		splx(s);
	}
}

/*
 * close system call
 */
close()
{
	register struct file *fp;
	register struct a {
		int	fdes;
	} *uap;

	uap = (struct a *)u.u_ap;
	fp = getf(uap->fdes);
	if(fp == NULL)
		return;
	u.u_ofile[uap->fdes] = NULL;
	closef(fp);
}

/*
 * seek system call
 */
seek()
{
	register struct file *fp;
	register struct a {
		int	fdes;
		off_t	off;
		int	sbase;
	} *uap;

	uap = (struct a *)u.u_ap;
	fp = getf(uap->fdes);
	if(fp == NULL)
		return;
	if(fp->f_inode->i_sptr) {
		u.u_error = ESPIPE;
		return;
	}
	if(uap->sbase == 1)
		uap->off += fp->f_offset;
	else if(uap->sbase == 2)
		uap->off += fp->f_inode->i_size;
	fp->f_offset = uap->off;
	u.u_r.r_off = uap->off;
}


/*
 * link system call
 */
link()
{
	register struct inode *ip;
	register struct a {
		char	*target;
		char	*linkname;
	} *uap;
	struct argnamei nmarg;

	uap = (struct a *)u.u_ap;
	ip = namei(uchar, 0, 0);   /* well, this routine is doomed anyhow */
	if(ip == NULL)
		return;
	if((ip->i_mode&IFMT)==IFDIR && !suser())
		goto out1;
	ip->i_nlink++;
	ip->i_flag |= ICHG;
	iupdat(ip, &time, &time, 1);
	prele(ip);
	u.u_dirp = (caddr_t)uap->linkname;
	nmarg.flag = NI_LINK;
	nmarg.idev = ip->i_dev;
	nmarg.ino = ip->i_number;
	(void) namei(uchar, &nmarg, 0);
	if(u.u_error) {
		ip->i_nlink--;
		ip->i_flag |= ICHG;
	}
out1:
	iput(ip);
}

/*
 * mknod system call
 */
mknod()
{
	register struct inode *ip;
	register struct a {
		char	*fname;
		int	fmode;
		int	dev;
	} *uap;
	struct argnamei nmarg;

	uap = (struct a *)u.u_ap;
	if(suser()) {
		nmarg.flag = NI_NXCREAT;
		nmarg.mode = uap->fmode;
		ip = namei(uchar, &nmarg, 0);
	}
	if(u.u_error)
		return;
	if (uap->dev) {
		/*
		 * Want to be able to use this to make badblock
		 * inodes, so don't truncate the dev number.
		 */
		ip->i_un.i_rdev = uap->dev;
		ip->i_flag |= IACC|IUPD|ICHG;
	}
	iput(ip);
}

/*
 * access system call
 */
saccess()
{
	register svuid, svgid;
	register struct inode *ip;
	register struct a {
		char	*fname;
		int	fmode;
	} *uap;

	uap = (struct a *)u.u_ap;
	svuid = u.u_uid;
	svgid = u.u_gid;
	u.u_uid = u.u_ruid;
	u.u_gid = u.u_rgid;
	ip = namei(uchar, 0, 1);
	if (ip != NULL) {
		if (uap->fmode&(IREAD>>6))
			(void) access(ip, IREAD);
		if (uap->fmode&(IWRITE>>6))
			(void) access(ip, IWRITE);
		if (uap->fmode&(IEXEC>>6))
			(void) access(ip, IEXEC);
		iput(ip);
	}
	u.u_uid = svuid;
	u.u_gid = svgid;
}