1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

edit/history.c: backport fixes from 93v- beta

src/cmd/ksh93/edit/history.c:
- Call sh_close() and sh_fcntl() instead of close(2) and fcntl(2),
  updating the shell's file descriptor state.
- Mark files close-on-exec on opening them. The history file should
  not remain open if ksh execs another process.
- Another fix for an FD check: < 10 instead of < 2.
This commit is contained in:
Martijn Dekker 2021-03-12 19:56:59 +00:00
parent 59bacfd494
commit d2c1700f63

View file

@ -150,9 +150,9 @@ static History_t *hist_ptr;
(unsigned)acctfd < 10) (unsigned)acctfd < 10)
{ {
int n; int n;
if((n = fcntl(acctfd, F_DUPFD, 10)) >= 0) if((n = sh_fcntl(acctfd, F_dupfd_cloexec, 10)) >= 0)
{ {
close(acctfd); sh_close(acctfd);
acctfd = n; acctfd = n;
} }
} }
@ -178,7 +178,7 @@ static int sh_checkaudit(History_t *hp, const char *name, char *logbuf, size_t l
{ {
char *cp, *last; char *cp, *last;
int id1, id2, r=0, n, fd; int id1, id2, r=0, n, fd;
if((fd=open(name, O_RDONLY)) < 0) if((fd=open(name, O_RDONLY,O_cloexec)) < 0)
return(0); return(0);
if((n = read(fd, logbuf,len-1)) < 0) if((n = read(fd, logbuf,len-1)) < 0)
goto done; goto done;
@ -202,7 +202,7 @@ static int sh_checkaudit(History_t *hp, const char *name, char *logbuf, size_t l
} }
while(*cp==';' || *cp==' '); while(*cp==';' || *cp==' ');
done: done:
close(fd); sh_close(fd);
return(r); return(r);
} }
@ -262,23 +262,23 @@ retry:
cp = path_relative(shp,histname); cp = path_relative(shp,histname);
if(!histinit) if(!histinit)
histmode = S_IRUSR|S_IWUSR; histmode = S_IRUSR|S_IWUSR;
if((fd=open(cp,O_BINARY|O_APPEND|O_RDWR|O_CREAT,histmode))>=0) if((fd=open(cp,O_BINARY|O_APPEND|O_RDWR|O_CREAT|O_cloexec,histmode))>=0)
{ {
hsize=lseek(fd,(off_t)0,SEEK_END); hsize=lseek(fd,(off_t)0,SEEK_END);
} }
if((unsigned)fd <=2) if((unsigned)fd < 10)
{ {
int n; int n;
if((n=fcntl(fd,F_DUPFD,10))>=0) if((n=sh_fcntl(fd,F_dupfd_cloexec,10))>=0)
{ {
close(fd); sh_close(fd);
fd=n; fd=n;
} }
} }
/* make sure that file has history file format */ /* make sure that file has history file format */
if(hsize && hist_check(fd)) if(hsize && hist_check(fd))
{ {
close(fd); sh_close(fd);
hsize = 0; hsize = 0;
if(unlink(cp)>=0) if(unlink(cp)>=0)
goto retry; goto retry;
@ -293,7 +293,7 @@ retry:
{ {
if(!(fname = pathtmp(NIL(char*),0,0,NIL(int*)))) if(!(fname = pathtmp(NIL(char*),0,0,NIL(int*))))
return(0); return(0);
fd = open(fname,O_BINARY|O_APPEND|O_CREAT|O_RDWR,S_IRUSR|S_IWUSR); fd = open(fname,O_BINARY|O_APPEND|O_CREAT|O_RDWR,S_IRUSR|S_IWUSR|O_cloexec);
} }
} }
if(fd<0) if(fd<0)
@ -378,10 +378,10 @@ retry:
hp->auditfp = 0; hp->auditfp = 0;
if(sh_isstate(SH_INTERACTIVE) && (hp->auditmask=sh_checkaudit(hp,SHOPT_AUDITFILE, buff, sizeof(buff)))) if(sh_isstate(SH_INTERACTIVE) && (hp->auditmask=sh_checkaudit(hp,SHOPT_AUDITFILE, buff, sizeof(buff))))
{ {
if((fd=sh_open(buff,O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IRUSR|S_IWUSR))>=0 && fd < 10) if((fd=sh_open(buff,O_BINARY|O_WRONLY|O_APPEND|O_CREAT|O_cloexec,S_IRUSR|S_IWUSR))>=0 && fd < 10)
{ {
int n; int n;
if((n = sh_fcntl(fd,F_DUPFD, 10)) >= 0) if((n = sh_fcntl(fd,F_dupfd_cloexec, 10)) >= 0)
{ {
sh_close(fd); sh_close(fd);
fd = n; fd = n;
@ -420,7 +420,7 @@ void hist_close(register History_t *hp)
#if SHOPT_ACCTFILE #if SHOPT_ACCTFILE
if(acctfd) if(acctfd)
{ {
close(acctfd); sh_close(acctfd);
acctfd = 0; acctfd = 0;
} }
#endif /* SHOPT_ACCTFILE */ #endif /* SHOPT_ACCTFILE */
@ -465,7 +465,7 @@ static History_t* hist_trim(History_t *hp, int n)
/* The unlink can fail on windows 95 */ /* The unlink can fail on windows 95 */
int fd; int fd;
char *last, *name=hist_old->histname; char *last, *name=hist_old->histname;
close(sffileno(hist_old->histfp)); sh_close(sffileno(hist_old->histfp));
tmpname = (char*)sh_malloc(strlen(name)+14); tmpname = (char*)sh_malloc(strlen(name)+14);
if(last = strrchr(name,'/')) if(last = strrchr(name,'/'))
{ {
@ -480,7 +480,7 @@ static History_t* hist_trim(History_t *hp, int n)
free(tmpname); free(tmpname);
tmpname = name; tmpname = name;
} }
fd = open(tmpname,O_RDONLY); fd = open(tmpname,O_RDONLY|O_cloexec);
sfsetfd(hist_old->histfp,fd); sfsetfd(hist_old->histfp,fd);
if(tmpname==name) if(tmpname==name)
tmpname = 0; tmpname = 0;
@ -725,13 +725,13 @@ again:
if(last<0) if(last<0)
{ {
char buff[HIST_MARKSZ]; char buff[HIST_MARKSZ];
int fd = open(hp->histname,O_RDWR); int fd = open(hp->histname,O_RDWR|O_cloexec);
if(fd>=0) if(fd>=0)
{ {
hist_marker(buff,hp->histind); hist_marker(buff,hp->histind);
write(fd,(char*)hist_stamp,2); write(fd,(char*)hist_stamp,2);
write(fd,buff,HIST_MARKSZ); write(fd,buff,HIST_MARKSZ);
close(fd); sh_close(fd);
} }
} }
last = 0; last = 0;
@ -1013,7 +1013,7 @@ int hist_match(register History_t *hp,off_t offset,char *string,int *coffset)
if(!(cp = first = (unsigned char*)sfgetr(hp->histfp,0,0))) if(!(cp = first = (unsigned char*)sfgetr(hp->histfp,0,0)))
return(-1); return(-1);
m = sfvalue(hp->histfp); m = sfvalue(hp->histfp);
n = strlen(string); n = (int)strlen(string);
while(m > n) while(m > n)
{ {
if(*cp==*string && memcmp(cp,string,n)==0) if(*cp==*string && memcmp(cp,string,n)==0)
@ -1186,10 +1186,10 @@ static int hist_exceptf(Sfio_t* fp, int type, Sfdisc_t *handle)
if(errno==ENOSPC || hp->histwfail++ >= 10) if(errno==ENOSPC || hp->histwfail++ >= 10)
return(0); return(0);
/* write failure could be NFS problem, try to re-open */ /* write failure could be NFS problem, try to re-open */
close(oldfd=sffileno(fp)); sh_close(oldfd=sffileno(fp));
if((newfd=open(hp->histname,O_BINARY|O_APPEND|O_CREAT|O_RDWR,S_IRUSR|S_IWUSR)) >= 0) if((newfd=open(hp->histname,O_BINARY|O_APPEND|O_CREAT|O_RDWR|O_cloexec,S_IRUSR|S_IWUSR)) >= 0)
{ {
if(fcntl(newfd, F_DUPFD, oldfd) !=oldfd) if(sh_fcntl(newfd, F_dupfd_cloexec, oldfd) != oldfd)
return(-1); return(-1);
fcntl(oldfd,F_SETFD,FD_CLOEXEC); fcntl(oldfd,F_SETFD,FD_CLOEXEC);
close(newfd); close(newfd);