Skip to content

Commit 791585c

Browse files
authored
Merge pull request #205 from alfkil/beta10
Fix return code for spawnvpe/waitpid.
2 parents a828a7d + 2bedab6 commit 791585c

File tree

5 files changed

+103
-21
lines changed

5 files changed

+103
-21
lines changed

library/c.lib_rev.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define REVISION 0
33
#define SUBREVISION 0
44

5-
#define DATE "27.08.2024"
5+
#define DATE "31.08.2024"
66
#define VERS "clib4.library 1.0.0"
7-
#define VSTRING "clib4.library 1.0.0 (27.08.2024)\r\n"
8-
#define VERSTAG "\0$VER: clib4.library 1.0.0 (27.08.2024)"
7+
#define VSTRING "clib4.library 1.0.0 (31.08.2024)\r\n"
8+
#define VERSTAG "\0$VER: clib4.library 1.0.0 (31.08.2024)"

library/include/sys/wait.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ __BEGIN_DECLS
88

99
#define WNOHANG 1
1010
#define WUNTRACED 2
11-
12-
#define WIFEXITED(w) (((w) & 0xff) == 0)
11+
12+
#define WIFEXITED(w) (((w) & 0x80000000) == 0)
1313
#define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
1414
#define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
1515
#define WEXITSTATUS(w) (((w)) & 0x000000ff)

library/misc/children.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ pidChildrenScan(const void *children, void *pid) {
3232
}
3333

3434
BOOL
35-
insertSpawnedChildren(uint32 pid, uint32 gid) {
35+
insertSpawnedChildren(uint32 pid, uint32 ppid, uint32 gid) {
3636
DECLARE_UTILITYBASE();
3737

3838
struct Clib4Resource *res = (APTR) OpenResource(RESOURCE_NAME);
3939
if (res) {
40-
uint32 parent = GetPID(0, GPID_PARENT);
40+
uint32 parent = ppid; //GetPID(0, GPID_PARENT);
4141
size_t iter = 0;
4242
void *item;
4343

4444
struct Clib4Children children;
4545
children.pid = pid;
46-
children.returnCode = 0;
46+
children.returnCode = 0x80000000; //set this flag for WIFEXITED
4747
children.groupId = gid;
4848

4949
while (hashmap_iter(res->children, &iter, &item)) {
@@ -99,10 +99,12 @@ findSpawnedChildrenByGid(uint32 pid, uint32 gid) {
9999
}
100100

101101
void
102-
spawnedProcessEnter(int32 entry_data UNUSED) {
103-
struct Library *UserGroupBase;
104-
struct UserGroupIFace *IUserGroup;
105-
gid_t groupId = 0;
102+
spawnedProcessEnter(int32 entry_data) {
103+
struct Library *UserGroupBase = 0;
104+
struct UserGroupIFace *IUserGroup = 0;
105+
gid_t groupId = (gid_t)entry_data;
106+
107+
#if 0
106108
UserGroupBase = OpenLibrary("usergroup.library", 0);
107109

108110
if (UserGroupBase != NULL) {
@@ -116,9 +118,11 @@ spawnedProcessEnter(int32 entry_data UNUSED) {
116118
}
117119
CloseLibrary(UserGroupBase);
118120
}
121+
#endif
119122

120-
uint32 pid = ((struct Process *) FindTask(NULL))->pr_ProcessID;
121-
if (insertSpawnedChildren(pid, groupId)) {
123+
uint32 pid = GetPID(0, GPID_PROCESS); //((struct Process *) FindTask(NULL))->pr_ProcessID;
124+
uint32 ppid = GetPID(0, GPID_PARENT);
125+
if (insertSpawnedChildren(pid, ppid, groupId)) {
122126
__CLIB4->__children++;
123127
D(("Children with pid %ld and gid %ld inserted into list\n", pid, groupId));
124128
}
@@ -138,13 +142,18 @@ spawnedProcessExit(int32 rc, int32 data UNUSED) {
138142

139143
while (hashmap_iter(res->children, &iter, &item)) {
140144
const struct Clib4Node *node = item;
145+
146+
// DebugPrintF("[spawneeExit :] *** iteration : pid=%d\n", node->pid);
147+
141148
if (node->pid == parent) {
142149
struct Clib4Children key;
143150
key.pid = me;
144151
struct Clib4Children *item = (struct Clib4Children *) hashmap_get(node->spawnedProcesses, &key);
145152
if (item != NULL) {
146-
SHOWMSG("[spawneeExit :] SUCCESS");
147-
item->returnCode = rc;
153+
// SHOWMSG("[spawneeExit :] SUCCESS");
154+
// DebugPrintF("[spawneeExit :] SUCCESS\n");
155+
item->returnCode = ~0x80000000 & rc;
156+
break;
148157
}
149158
}
150159
}

library/misc/children.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "clib4.h"
99

10-
BOOL insertSpawnedChildren(uint32 pid, uint32 gid);
10+
BOOL insertSpawnedChildren(uint32 pid, uint32 ppid, uint32 gid);
1111
struct Clib4Children *findSpawnedChildrenByPid(uint32 pid);
1212
struct Clib4Children *findSpawnedChildrenByGid(uint32 pid, uint32 gid);
1313
void spawnedProcessExit(int32 rc, int32 data UNUSED);

library/unistd/spawnvpe.c

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,35 @@ get_arg_string_length(char *const argv[]) {
117117

118118
return (result);
119119
}
120+
/* * * * *
121+
Apparently this doesn't work. You cannot delay a child by using signals in the EntryCode,
122+
because - as it seems - SystemTags only returns AFTER executing EntryCode. Which, of course,
123+
means, that the parent is never allowed to send the signal.
124+
* * * * */
125+
// struct EntryData {
126+
// uint8 childSignal;
127+
// uint8 parentSignal;
128+
// struct Task *parent, *child;
129+
// };
130+
// void amiga_entryCode(int32 entry_data);
131+
// void
132+
// amiga_entryCode(int32 entry_data) {
133+
// struct EntryData *ed = (struct EntryData *)entry_data;
134+
// ed->childSignal = AllocSignal(-1);
135+
// ed->child = FindTask(0);
136+
// DebugPrintF("[child :] Signalling parent...\n");
137+
// Signal(ed->parent, 1 << ed->parentSignal);
138+
// DebugPrintF("[child :] Waiting for signal from parent...\n");
139+
// Wait(1 << ed->childSignal);
140+
// DebugPrintF("[child :] Done.");
141+
// FreeSignal(ed->childSignal);
142+
// }
120143
int
121144
spawnvpe(
122145
const char *file,
123146
const char **argv,
124147
char **deltaenv,
125-
const char *cwd,
148+
const char *_cwd,
126149
int fhin,
127150
int fhout,
128151
int fherr
@@ -131,6 +154,7 @@ spawnvpe(
131154
struct name_translation_info nti_name;
132155
const char *name = file;
133156
struct name_translation_info nti_cwd;
157+
const char *cwd = _cwd;
134158
BPTR iofh[3] = {BZERO, BZERO, BZERO};
135159
int closefh[3] = {FALSE, FALSE, FALSE};
136160
BPTR fh;
@@ -156,6 +180,7 @@ spawnvpe(
156180
}
157181

158182
D(("name after conversion: [%s]\n", name));
183+
// printf("[spawnvpe :] name after conversion [%s]\n", name);
159184

160185
#if 0
161186
seglist = LoadSeg(name);
@@ -169,6 +194,20 @@ spawnvpe(
169194
UnLock(fileLock);
170195
}
171196

197+
// printf("[spawnvpe :] cwd before conversion: [%s]\n", cwd);
198+
199+
if(cwd) {
200+
error = __translate_unix_to_amiga_path_name(&cwd, &nti_cwd);
201+
if (error) {
202+
__set_errno(EINVAL);
203+
D(("__translate_unix_to_amiga_path_name failed: %s\n", strerror(error)));
204+
return ret;
205+
}
206+
}
207+
208+
D(("cwd after conversion: [%s]\n", cwd));
209+
// printf("[spawnvpe :] cwd after conversion [%s]\n", cwd);
210+
172211
BPTR cwdLock = cwd ? Lock(cwd, SHARED_LOCK) : 0;
173212

174213
parameter_string_len = get_arg_string_length((char *const *) argv);
@@ -178,6 +217,7 @@ spawnvpe(
178217
}
179218

180219
D(("parameter_string_len: [%ld]\n", parameter_string_len));
220+
// printf("[spawnvpe :] parameter_string_len: [%ld]\n", parameter_string_len);
181221

182222
arg_string = malloc(parameter_string_len + 1);
183223
if (arg_string == NULL) {
@@ -206,6 +246,7 @@ spawnvpe(
206246
D(("File to execute: [%s]\n", finalpath));
207247

208248
// printf("[spawnvpe :] full command == <%s>\n", finalpath);
249+
// printf("[spawnvpe :] processName == <%s>\n", processName);
209250

210251
if (fhin >= 0) {
211252
err = __get_default_file(fhin, &fh);
@@ -252,6 +293,8 @@ spawnvpe(
252293
D(("(*)Calling SystemTags.\n"));
253294

254295
// struct EntryData ed;
296+
// ed.parent = FindTask(0);
297+
// ed.parentSignal = AllocSignal(-1);
255298

256299
struct Task *_me = FindTask(0);
257300
#if 0
@@ -296,6 +339,8 @@ spawnvpe(
296339
);
297340
if (p) ret = 0;
298341
#else
342+
// printf("(*)Calling SystemTags.\n");
343+
299344
ret = SystemTags(finalpath,
300345
NP_NotifyOnDeathSigTask, _me,
301346
SYS_Input, iofh[0],
@@ -309,12 +354,14 @@ spawnvpe(
309354
NP_Name, strdup(processName),
310355

311356
NP_EntryCode, spawnedProcessEnter,
357+
NP_EntryData, getgid(),
358+
// NP_EntryCode, amiga_entryCode,
359+
// NP_EntryData, &ed,
312360
NP_ExitCode, spawnedProcessExit,
313361

314362
TAG_DONE);
315-
#endif
316363

317-
D(("SystemTags completed. return value: [%ld]\n", ret));
364+
#endif
318365

319366
if (ret != 0) {
320367
__set_errno(__translate_io_error_to_errno(IoErr()));
@@ -329,8 +376,34 @@ spawnvpe(
329376
/*
330377
* If mode is set as P_NOWAIT we can retrieve process id calling IoErr()
331378
* just after SystemTags. In this case spawnv will return pid
379+
* IoErr() must be called IMMEDIATELY after SystemTags() == no other DOS calls inbetween
332380
*/
333-
ret = IoErr();
381+
382+
// DebugPrintF("[main :] Child created with success.\n");
383+
384+
pid_t pid = IoErr();
385+
// gid_t groupId = getgid();
386+
// uint32 ppid = ((struct Process *) FindTask(NULL))->pr_ProcessID;
387+
388+
// if (insertSpawnedChildren(pid, ppid, groupId)) {
389+
// __CLIB4->__children++;
390+
// D(("Children with pid %ld and gid %ld inserted into list\n", pid, groupId));
391+
// }
392+
// else {
393+
// D(("Cannot insert children with pid %ld and gid %ld into list\n", pid, groupId));
394+
// }
395+
396+
// DebugPrintF("[parent :] Waiting for signal from child...\n");
397+
// Wait(1 << ed.parentSignal);
398+
// DebugPrintF("[parent :] Signalling child...\n");
399+
// Signal(ed.child, 1 << ed.childSignal);
400+
// DebugPrintF("[parent :] Done.\n");
401+
// FreeSignal(ed.parentSignal);
402+
403+
ret = pid;
334404
}
405+
// D(("SystemTags completed. return value: [%ld]\n", ret));
406+
// printf("SystemTags completed. return value: [%ld]\n", ret);
407+
335408
return ret;
336409
}

0 commit comments

Comments
 (0)