* Copyright (c) 1982, 1986, 1989 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* @(#)kern_exec.c 7.18 (Berkeley) %G%
#include "machine/mtpr.h"
* exec system call, with and without environments.
((struct execa
*)u
.u_ap
)->envp
= NULL
;
register struct execa
*uap
;
char cfname
[MAXCOMLEN
+ 1];
char ex_shell
[MAXINTERP
]; /* #! and interpreter name */
register struct nameidata
*ndp
= &u
.u_nd
;
extern long argdbsize
; /* XXX */
ndp
->ni_nameiop
= LOOKUP
| FOLLOW
| LOCKLEAF
;
ndp
->ni_segflg
= UIO_USERSPACE
;
ndp
->ni_dirp
= ((struct execa
*)u
.u_ap
)->fname
;
if (u
.u_error
= namei(ndp
)) {
if (u
.u_error
= VOP_GETATTR(vp
, &vattr
, u
.u_cred
))
if (vp
->v_mount
->m_flag
& M_NOEXEC
) {
if ((vp
->v_mount
->m_flag
& M_NOSUID
) == 0) {
if (vattr
.va_mode
& VSUID
)
if (vattr
.va_mode
& VSGID
)
if (u
.u_error
= VOP_ACCESS(vp
, VEXEC
, u
.u_cred
))
if ((u
.u_procp
->p_flag
& STRC
) &&
(u
.u_error
= VOP_ACCESS(vp
, VREAD
, u
.u_cred
)))
if (vp
->v_type
!= VREG
||
(vattr
.va_mode
& (VEXEC
|(VEXEC
>>3)|(VEXEC
>>6))) == 0) {
* Read in first few bytes of file for segment sizes, magic number:
* OMAGIC = plain executable
* ZMAGIC = demand paged RO text
* Also an ASCII line beginning with #! is
* the file name of a ``shell'' and arguments may be prepended
* to the argument list if given here.
* SHELL NAMES ARE LIMITED IN LENGTH.
* ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
exdata
.ex_shell
[0] = '\0'; /* for zero length files */
u
.u_error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&exdata
, sizeof (exdata
),
(off_t
)0, UIO_SYSSPACE
, (IO_UNIT
|IO_NODELOCKED
), u
.u_cred
, &resid
);
if (resid
> sizeof(exdata
) - sizeof(exdata
.ex_exec
) &&
exdata
.ex_shell
[0] != '#') {
switch ((int)exdata
.ex_exec
.a_magic
) {
exdata
.ex_exec
.a_data
+= exdata
.ex_exec
.a_text
;
exdata
.ex_exec
.a_text
= 0;
if (exdata
.ex_exec
.a_text
== 0) {
if (exdata
.ex_shell
[0] != '#' ||
exdata
.ex_shell
[1] != '!' ||
for (cp
= &exdata
.ex_shell
[2];; ++cp
) {
if (cp
>= &exdata
.ex_shell
[MAXINTERP
]) {
cp
= &exdata
.ex_shell
[2];
while (*cp
&& *cp
!= ' ')
bcopy((caddr_t
)cp
, (caddr_t
)cfarg
, MAXINTERP
);
ndp
->ni_nameiop
= LOOKUP
| FOLLOW
| LOCKLEAF
;
ndp
->ni_segflg
= UIO_SYSSPACE
;
if (u
.u_error
= namei(ndp
))
if (u
.u_error
= VOP_GETATTR(vp
, &vattr
, u
.u_cred
))
bcopy((caddr_t
)ndp
->ni_dent
.d_name
, (caddr_t
)cfname
,
cfname
[MAXCOMLEN
] = '\0';
uid
= u
.u_cred
->cr_uid
; /* shell scripts can't be setuid */
* Collect arguments on "file" in swap space.
uap
= (struct execa
*)u
.u_ap
;
bno
= rmalloc(argmap
, (clrnd((int)btoc(NCARGS
))) * CLBYTES
/ argdbsize
);
bno
= rmalloc(argmap
, (long)ctod(clrnd((int)btoc(NCARGS
))));
swkill(u
.u_procp
, "exec: no swap space");
* Copy arguments into file in argdev area.
if (uap
->argp
) for (;;) {
uap
->argp
++; /* ignore argv[0] */
} else if (indir
&& (na
== 1 && cfarg
[0])) {
} else if (indir
&& (na
== 1 || na
== 2 && cfarg
[0]))
ap
= fuword((caddr_t
)uap
->argp
);
if (ap
== NULL
&& uap
->envp
) {
if ((ap
= fuword((caddr_t
)uap
->envp
)) != NULL
)
* We depend on NCARGS being a multiple of
* CLBYTES. This way we need only check
* overflow before each buffer allocation.
bp
= getblk(argdev
, bno
+ nc
/ argdbsize
, cc
,
bp
= getblk(argdev_vp
, bno
+ ctod(nc
/NBPG
), cc
);
error
= copystr(sharg
, cp
, (unsigned)cc
, &len
);
error
= copyinstr((caddr_t
)ap
, cp
, (unsigned)cc
,
} while (error
== ENOENT
);
nc
= (nc
+ NBPW
-1) & ~(NBPW
-1);
getxfile(vp
, &exdata
.ex_exec
, nc
+ (na
+4)*NBPW
, uid
, gid
, u
.u_cred
);
for (cc
= 0; cc
< nc
; cc
+= CLBYTES
) {
(void) baddr(argdev_vp
, bno
+ ctod(cc
/NBPG
),
bp
->b_flags
|= B_AGE
; /* throw away */
bp
->b_flags
&= ~B_DELWRI
; /* cancel io */
ucp
= USRSTACK
- nc
- NBPW
;
ap
= ucp
- na
*NBPW
- 3*NBPW
;
(void) suword((caddr_t
)ap
, na
-ne
);
(void) suword((caddr_t
)ap
, 0);
(void) suword((caddr_t
)ap
, ucp
);
bp
= bread(argdev
, bno
+ nc
/ argdbsize
, cc
,
(daddr_t
)(bno
+ ctod(nc
/ NBPG
)), cc
,
bp
->b_flags
|= B_AGE
; /* throw away */
bp
->b_flags
&= ~B_DELWRI
; /* cancel io */
error
= copyoutstr(cp
, (caddr_t
)ucp
, (unsigned)cc
,
} while (error
== ENOENT
);
(void) suword((caddr_t
)ap
, 0);
for (nc
= u
.u_lastfile
; nc
>= 0; --nc
) {
if (u
.u_pofile
[nc
] & UF_EXCLOSE
) {
(void) closef(u
.u_ofile
[nc
]);
u
.u_pofile
[nc
] &= ~UF_MAPPED
;
while (u
.u_lastfile
>= 0 && u
.u_ofile
[u
.u_lastfile
] == NULL
)
setregs(exdata
.ex_exec
.a_entry
);
* Remember file name for accounting.
bcopy((caddr_t
)cfname
, (caddr_t
)u
.u_comm
, MAXCOMLEN
);
if (ndp
->ni_dent
.d_namlen
> MAXCOMLEN
)
ndp
->ni_dent
.d_namlen
= MAXCOMLEN
;
bcopy((caddr_t
)ndp
->ni_dent
.d_name
, (caddr_t
)u
.u_comm
,
(unsigned)(ndp
->ni_dent
.d_namlen
+ 1));
rmfree(argmap
, (clrnd((int)btoc(NCARGS
))) * CLBYTES
/ argdbsize
,
rmfree(argmap
, (long)ctod(clrnd((int) btoc(NCARGS
))), bno
);
* Read in and set up memory for executed file.
getxfile(vp
, ep
, nargc
, uid
, gid
, cred
)
register struct vnode
*vp
;
register struct exec
*ep
;
register struct proc
*p
= u
.u_procp
;
size_t ts
, ds
, ids
, uds
, ss
;
if (ep
->a_magic
== ZMAGIC
)
if (vp
->v_text
&& (vp
->v_text
->x_flag
& XTRC
)) {
if (ep
->a_text
!= 0 && (vp
->v_flag
& VTEXT
) == 0 &&
register struct file
*fp
;
for (fp
= file
; fp
< fileNFILE
; fp
++) {
if (fp
->f_type
== DTYPE_VNODE
&&
(struct vnode
*)fp
->f_data
== vp
&&
* Compute text and data sizes and make sure not too large.
* NB - Check data and bss separately as they may overflow
ts
= clrnd(btoc(ep
->a_text
));
ids
= clrnd(btoc(ep
->a_data
));
uds
= clrnd(btoc(ep
->a_bss
));
ds
= clrnd(btoc(ep
->a_data
+ ep
->a_bss
));
ss
= clrnd(SSIZE
+ btoc(nargc
));
if (chksize((unsigned)ts
, (unsigned)ids
, (unsigned)uds
, (unsigned)ss
))
* Make sure enough space to start process.
if (swpexpand(ds
, ss
, &u
.u_cdmap
, &u
.u_csmap
) == NULL
)
* At this point, we are committed to the new image!
* Release virtual memory resources of old process, and
* initialize the virtual memory of the new process.
* If we resulted from vfork(), instead wakeup our
* parent who will set SVFDONE when he has taken back
if ((p
->p_flag
& SVFORK
) == 0)
while ((p
->p_flag
& SVFDONE
) == 0)
sleep((caddr_t
)p
, PZERO
- 1);
p
->p_flag
&= ~(SVFDONE
|SKEEP
);
p
->p_flag
&= ~(SPAGV
|SSEQL
|SUANOM
);
p
->p_flag
|= pagi
| SEXEC
;
u
.u_error
= vn_rdwr(UIO_READ
, vp
,
(char *)ctob(dptov(u
.u_procp
, 0)),
(off_t
)(sizeof (struct exec
) + ep
->a_text
),
UIO_USERSPACE
, (IO_UNIT
|IO_NODELOCKED
), cred
, (int *)0);
xalloc(vp
, ep
, pagi
, cred
);
if (p
->p_textp
== 0) { /* use existing code key if shared */
p
->p_ckey
= getcodekey();
p
->p_dkey
= getdatakey();
vinifod(u
.u_procp
, (struct fpte
*)dptopte(p
, 0),
PG_FTEXT
, p
->p_textp
->x_vptr
,
(long)(1 + ts
/CLSIZE
), (size_t)btoc(ep
->a_data
));
#if defined(vax) || defined(tahoe)
/* THIS SHOULD BE DONE AT A LOWER LEVEL, IF AT ALL */
* set SUID/SGID protections, if no tracing
if ((p
->p_flag
&STRC
)==0) {
if (uid
!= u
.u_cred
->cr_uid
|| gid
!= u
.u_cred
->cr_gid
)
u
.u_cred
= crcopy(u
.u_cred
);
p
->p_svgid
= u
.u_cred
->cr_gid
;
u
.u_pcb
.pcb_savacc
.faddr
= (float *)NULL
;