1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00
cde/src/cmd/ksh93/bltins/alarm.c
Martijn Dekker 18529b88c6 Add lots of checks for out of memory (re: 0ce0b671)
Huge typeset -L/-R adjustment length values were still causing
crashses on sytems with not enough memory. They should error out
gracefully instead of crashing.

This commit adds out of memory checks to all malloc/calloc/realloc
calls that didn't have them (which is all but two or three).

The stkalloc/stakalloc calls don't need the checks; it has
automatic checking, which is done by passing a pointer to the
outofspace() function to the stakinstall() call in init.c.

src/lib/libast/include/error.h:
- Change the ERROR_PANIC exit status value from ERROR_LEVEL (255)
  to 77, which is what it is supposed to be according to the libast
  error.3 manual page. Exit statuses > 128 for anything else than
  signals are not POSIX compliant and may cause misbehaviour.

src/cmd/ksh93/include/defs.h,
src/cmd/ksh93/sh/init.c:
- To facilitate consistency, add a simple extern sh_outofmemory()
  function that throws an ERROR_PANIC "out of memory".

src/cmd/ksh93/include/shell.h,
src/cmd/ksh93/data/builtins.c:
- Remove now-redundant e_nospace[] extern message; it is now only
  used in one place so it might as well be a string literal in
  sh_outofmemory().

All other changed files:
- Verify the result of all malloc/calloc/realloc calls and call
  sh_outofmemory() if they fail.
2021-02-21 22:27:28 +00:00

286 lines
6.8 KiB
C

/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2012 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* alarm [-r] [varname [+]when]
*
* David Korn
* AT&T Labs
*
*/
/*
* TODO: 2014 email from David Korn cited at <https://bugzilla.redhat.com/1176670>:
*
* > I never documented the alarm builtin because it is problematic. The
* > problem is that traps can't safely be handled asynchronously. What should
* > happen is that the trap is marked for execution (sh_trapnote) and run after
* > the current command completes. The time trap should wake up the shell if
* > it is blocked and it should return and then handle the trap.
*/
#include "defs.h"
#include <error.h>
#include <stak.h>
#include "builtins.h"
#include "FEATURE/time"
#define R_FLAG 1
#define L_FLAG 2
struct tevent
{
Namfun_t fun;
Namval_t *node;
Namval_t *action;
struct tevent *next;
long milli;
int flags;
void *timeout;
Shell_t *sh;
};
static const char ALARM[] = "alarm";
static void trap_timeout(void*);
/*
* insert timeout item on current given list in sorted order
*/
static void *time_add(struct tevent *item, void *list)
{
register struct tevent *tp = (struct tevent*)list;
if(!tp || item->milli < tp->milli)
{
item->next = tp;
list = (void*)item;
}
else
{
while(tp->next && item->milli > tp->next->milli)
tp = tp->next;
item->next = tp->next;
tp->next = item;
}
tp = item;
tp->timeout = (void*)sh_timeradd(tp->milli,tp->flags&R_FLAG,trap_timeout,(void*)tp);
return(list);
}
/*
* delete timeout item from current given list, delete timer
*/
static void *time_delete(register struct tevent *item, void *list)
{
register struct tevent *tp = (struct tevent*)list;
if(item==tp)
list = (void*)tp->next;
else
{
while(tp && tp->next != item)
tp = tp->next;
if(tp)
tp->next = item->next;
}
if(item->timeout)
timerdel((void*)item->timeout);
return(list);
}
static void print_alarms(void *list)
{
register struct tevent *tp = (struct tevent*)list;
while(tp)
{
if(tp->timeout)
{
register char *name = nv_name(tp->node);
if(tp->flags&R_FLAG)
{
double d = tp->milli;
sfprintf(sfstdout,e_alrm1,name,d/1000.);
}
else
sfprintf(sfstdout,e_alrm2,name,nv_getnum(tp->node));
}
tp = tp->next;
}
}
static void trap_timeout(void* handle)
{
register struct tevent *tp = (struct tevent*)handle;
tp->sh->trapnote |= SH_SIGALRM;
if(!(tp->flags&R_FLAG))
tp->timeout = 0;
tp->flags |= L_FLAG;
tp->sh->sigflag[SIGALRM] |= SH_SIGALRM;
if(sh_isstate(SH_TTYWAIT))
sh_timetraps(tp->sh);
}
void sh_timetraps(Shell_t *shp)
{
register struct tevent *tp, *tpnext;
register struct tevent *tptop;
while(1)
{
shp->sigflag[SIGALRM] &= ~SH_SIGALRM;
tptop= (struct tevent*)shp->st.timetrap;
for(tp=tptop;tp;tp=tpnext)
{
tpnext = tp->next;
if(tp->flags&L_FLAG)
{
tp->flags &= ~L_FLAG;
if(tp->action)
sh_fun(tp->action,tp->node,(char**)0);
tp->flags &= ~L_FLAG;
if(!tp->flags)
{
nv_unset(tp->node);
nv_close(tp->node);
}
}
}
if(!(shp->sigflag[SIGALRM]&SH_SIGALRM))
break;
}
}
/*
* This trap function catches "alarm" actions only
*/
static char *setdisc(Namval_t *np, const char *event, Namval_t* action, Namfun_t
*fp)
{
register struct tevent *tp = (struct tevent*)fp;
if(!event)
return(action?"":(char*)ALARM);
if(strcmp(event,ALARM)!=0)
{
/* try the next level */
return(nv_setdisc(np, event, action, fp));
}
if(action==np)
action = tp->action;
else
tp->action = action;
return(action?(char*)action:"");
}
/*
* catch assignments and set alarm traps
*/
static void putval(Namval_t* np, const char* val, int flag, Namfun_t* fp)
{
register struct tevent *tp = (struct tevent*)fp;
register double d;
Shell_t *shp = tp->sh;
if(val)
{
double now;
#ifdef timeofday
struct timeval tmp;
timeofday(&tmp);
now = tmp.tv_sec + 1.e-6*tmp.tv_usec;
#else
now = (double)time(NIL(time_t*));
#endif /* timeofday */
nv_putv(np,val,flag,fp);
d = nv_getnum(np);
if(*val=='+')
{
double x = d + now;
nv_putv(np,(char*)&x,NV_INTEGER|NV_DOUBLE,fp);
}
else
d -= now;
tp->milli = 1000*(d+.0005);
if(tp->timeout)
shp->st.timetrap = time_delete(tp,shp->st.timetrap);
if(tp->milli > 0)
shp->st.timetrap = time_add(tp,shp->st.timetrap);
}
else
{
tp = (struct tevent*)nv_stack(np, (Namfun_t*)0);
shp->st.timetrap = time_delete(tp,shp->st.timetrap);
if(tp->action)
nv_close(tp->action);
nv_unset(np);
free((void*)fp);
}
}
static const Namdisc_t alarmdisc =
{
sizeof(struct tevent),
putval,
0,
0,
setdisc,
};
int b_alarm(int argc,char *argv[],Shbltin_t *context)
{
register int n,rflag=0;
register Namval_t *np;
register struct tevent *tp;
register Shell_t *shp = context->shp;
while (n = optget(argv, sh_optalarm)) switch (n)
{
case 'r':
rflag = R_FLAG;
break;
case ':':
errormsg(SH_DICT,2, "%s", opt_info.arg);
break;
case '?':
errormsg(SH_DICT,ERROR_usage(2), "%s", opt_info.arg);
break;
}
argc -= opt_info.index;
argv += opt_info.index;
if(error_info.errors)
errormsg(SH_DICT,ERROR_usage(2),optusage((char*)0));
if(argc==0)
{
print_alarms(shp->st.timetrap);
return(0);
}
if(argc!=2)
errormsg(SH_DICT,ERROR_usage(2),optusage((char*)0));
np = nv_open(argv[0],shp->var_tree,NV_NOARRAY|NV_VARNAME|NV_NOASSIGN);
if(!nv_isnull(np))
nv_unset(np);
nv_setattr(np, NV_DOUBLE);
if(!(tp = newof(NIL(struct tevent*),struct tevent,1,0)))
sh_outofmemory();
tp->fun.disc = &alarmdisc;
tp->flags = rflag;
tp->node = np;
tp->sh = shp;
nv_stack(np,(Namfun_t*)tp);
nv_putval(np, argv[1], 0);
return(0);
}