Skip to content

Commit

Permalink
Small itimer fixes and code reworking. Fixed a reentrant problem on _…
Browse files Browse the repository at this point in the history
…_res_state
  • Loading branch information
afxgroup committed Oct 2, 2023
1 parent 6bcd209 commit 8c67c2b
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 80 deletions.
3 changes: 3 additions & 0 deletions library/include/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <sys/syslimits.h>
#include <wchar.h>
#include <setjmp.h>
#include <resolv.h>

/* Category name handling variables. */
#define NUM_LOCALES (LC_MAX + 1)
Expand Down Expand Up @@ -503,6 +504,8 @@ struct _clib4 {

/* termcap */
char tgoto_buf[50];

struct __res_state _res_state;
};

#ifndef __getClib4
Expand Down
42 changes: 7 additions & 35 deletions library/posix/raise.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@
#include "stdio_headers.h"
#endif /* _STDIO_HEADERS_H */

#include "pthread/common.h"

static APTR
hook_function(struct Hook *hook, APTR userdata, struct Process *process) {
uint32 pid = (uint32) userdata;
(void) (hook);

if (process->pr_ProcessID == pid) {
return process;
}
#ifndef _UNISTD_HEADERS_H
#include "unistd_headers.h"
#endif /* _UNISTD_HEADERS_H */

return 0;
}
#include "pthread/common.h"

int
raise(int sig) {
Expand Down Expand Up @@ -68,28 +60,10 @@ raise(int sig) {
if (sig == SIGINT || sig == SIGTERM || sig == SIGKILL) {
/* Check ig we have timer terminal running. If so let's kill it */
if (__clib4->tmr_real_task != NULL) {
struct Hook h = {{NULL, NULL}, (HOOKFUNC) hook_function, NULL, NULL};
int32 pid, process;

/* Block SIGALRM signal from raise */
sigblock(SIGALRM);
/* Get itimer process ID */
pid = __clib4->tmr_real_task->pr_ProcessID;

Forbid();
/* Scan for process */
process = ProcessScan(&h, (CONST_APTR) pid, 0);
/* If we find the process send a signal to kill it */
while (process > 0) {
/* Send a SIGBREAKF_CTRL_F signal until the timer task return to Wait state
* and can get the signal */
Signal((struct Task *) __clib4->tmr_real_task, SIGBREAKF_CTRL_F);
process = ProcessScan(&h, (CONST_APTR) pid, 0);
usleep(100);
}
Permit();
WaitForChildExit(pid);
__clib4->tmr_real_task = NULL;
/* Kill itimer */
killitimer();
}

char break_string[80];
Expand All @@ -108,10 +82,8 @@ raise(int sig) {
land us in _exit(). */
__abort();
}
/* If we have a SIGALRM without associated handler don't call abort but exit directly */
/* If we have a SIGALRM without associated handler send the SIGBREAKF_CTRL_E signal */
if (sig == SIGALRM) {
__print_termination_message("Alarm Clock");

/* Block SIGALRM signal from raise again */
sigblock(SIGALRM);

Expand Down
2 changes: 1 addition & 1 deletion library/resolv/lookup_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static int name_from_dns(struct address buf[static MAXADDRS], char canon[static
int qlens[2], alens[2];
int i, nq = 0;
struct dpc_ctx ctx = {.addrs = buf, .canon = canon};
static const struct {
const struct {
int af;
int rr;
} afrr[2] = {
Expand Down
6 changes: 3 additions & 3 deletions library/resolv/res_state.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* $Id: socket_res_state.c,v 1.0 2022-03-14 10:44:15 clib4devs Exp $
*/

#include <dos.h>
#include <resolv.h>

/* This is completely unused, and exists purely to satisfy broken apps. */

struct __res_state *__res_state() {
static struct __res_state res;
return &res;
struct _clib4 *__clib4 = __CLIB4;
return &__clib4->_res_state;
}
4 changes: 0 additions & 4 deletions library/stdio/lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@

void
__stdio_lock(struct _clib4 *__clib4) {
assert(__clib4->stdio_lock != NULL);

if (__clib4->stdio_lock != NULL)
ObtainSemaphore(__clib4->stdio_lock);
}

void
__stdio_unlock(struct _clib4 *__clib4) {
assert(__clib4->stdio_lock != NULL);

if (__clib4->stdio_lock != NULL)
ReleaseSemaphore(__clib4->stdio_lock);
}
Expand Down
36 changes: 6 additions & 30 deletions library/stdlib/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@
#include "stdio_headers.h"
#endif /* _STDIO_HEADERS_H */

#include "pthread/common.h"

static APTR
hook_function(struct Hook *hook, APTR userdata, struct Process *process) {
uint32 pid = (uint32) userdata;
(void) (hook);

if (process->pr_ProcessID == pid) {
return process;
}
#ifndef _UNISTD_HEADERS_H
#include "unistd_headers.h"
#endif /* _UNISTD_HEADERS_H */

return 0;
}
#include "pthread/common.h"

void
_exit(int return_code) {
Expand All @@ -46,26 +38,10 @@ _exit(int return_code) {
SHOWMSG("IN MAIN TASK");
/* If we have a previous timer running task stop it before raise SIGINT */
if (__clib4->tmr_real_task) {
struct Hook h = {{NULL, NULL}, (HOOKFUNC) hook_function, NULL, NULL};
int32 pid, process;

/* Block SIGALRM signal from raise */
sigblock(SIGALRM);
/* Get itimer process ID */
pid = __clib4->tmr_real_task->pr_ProcessID;

Forbid();
/* Scan for process */
process = ProcessScan(&h, (CONST_APTR) pid, 0);
while (process > 0) {
/* Send a SIGBREAKF_CTRL_F signal until the timer task return in Wait and can get the signal */
Signal((struct Task *) __clib4->tmr_real_task, SIGBREAKF_CTRL_F);
process = ProcessScan(&h, (CONST_APTR) pid, 0);
usleep(100);
}
Permit();
WaitForChildExit(pid);
__clib4->tmr_real_task = NULL;
/* Kill itimer */
killitimer();
}

/* Dump all currently unwritten data, especially to the console. */
Expand Down
6 changes: 4 additions & 2 deletions library/time/itimer_tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ int itimer_real_task() {

out:
/* Free itimer objects */
SHOWMSG("FreeSysObject ASOT_PORT");
FreeSysObject(ASOT_PORT, tmr_real_mp);
if (tmr_real_mp) {
SHOWMSG("FreeSysObject ASOT_PORT");
FreeSysObject(ASOT_PORT, tmr_real_mp);
}

if (tmr_real_tr) {
if (tmr_real_tr->Request.io_Device != NULL) {
Expand Down
39 changes: 35 additions & 4 deletions library/time/setitimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@
#include "time_headers.h"
#endif /* _TIME_HEADERS_H */

static APTR
hook_function(struct Hook *hook, APTR userdata, struct Process *process) {
uint32 pid = (uint32) userdata;
(void) (hook);

if (process->pr_ProcessID == pid) {
return process;
}

return 0;
}

void killitimer(void) {
struct _clib4 *__clib4 = __CLIB4;
struct Hook h = {{NULL, NULL}, (HOOKFUNC) hook_function, NULL, NULL};
int32 pid, process;
pid = __clib4->tmr_real_task->pr_ProcessID;
/* Scan for process */
process = ProcessScan(&h, (CONST_APTR) pid, 0);
while (process > 0) {
/* Send a SIGBREAKF_CTRL_F signal until the timer task return in Wait and can get the signal */
Signal((struct Task *) __clib4->tmr_real_task, SIGBREAKF_CTRL_F);
process = ProcessScan(&h, (CONST_APTR) pid, 0);
Delay(10);
}
WaitForChildExit(pid);
__clib4->tmr_real_task = NULL;

};

int
setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value) {
ENTER();
Expand Down Expand Up @@ -50,7 +80,7 @@ setitimer(int which, const struct itimerval *new_value, struct itimerval *old_va
}
else if (__clib4->tmr_real_task == NULL) {
/* Create timer tasks */
if ((new_value->it_value.tv_sec != 0 || new_value->it_value.tv_usec != 0)) {
if (new_value->it_value.tv_sec != 0 || new_value->it_value.tv_usec != 0) {
__clib4->tmr_real_task = CreateNewProcTags(
NP_Name, "ITIMER_TASK",
NP_Entry, itimer_real_task,
Expand All @@ -66,9 +96,10 @@ setitimer(int which, const struct itimerval *new_value, struct itimerval *old_va
}
}
else {
int pid = __clib4->tmr_real_task->pr_ProcessID;
Signal((struct Task *)__clib4->tmr_real_task, SIGBREAKF_CTRL_F);
WaitForChildExit(pid);
/* Block SIGALRM signal from raise */
sigblock(SIGALRM);
/* Kill itimer */
killitimer();
}

break;
Expand Down
1 change: 1 addition & 0 deletions library/unistd/unistd_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ extern int __strip_double_slash(char * file_name,int len);
extern int __time_delay(ULONG timercmd, struct timeval *tv);
extern long __pathconf(struct MsgPort *port,int name);
extern int itimer_real_task(void);
extern void killitimer(void);

#endif /* _UNISTD_HEADERS_H */
2 changes: 1 addition & 1 deletion library/usergroup/usergroup_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
struct UserGroupIFace *IUserGroup = __CLIB4->__IUserGroup


#ifndef _PATH_SERVICES
#ifndef _PATH_USERS
#define _PATH_USERS "Devs:Internet/users"
#endif

Expand Down

0 comments on commit 8c67c2b

Please sign in to comment.