Skip to content

Commit

Permalink
Implement posix_spawn*() with spawn2()
Browse files Browse the repository at this point in the history
This implementation improved the following things over LIBC:

1. Support chdir and fchdir file action.

2. Copy contents of a path of a file action not a value of a pointer.
   This is a requirement of POSIX.

3. Correct behaviors of posix_spawn() and posix_spawnp() which they are
   swapped.

    modified:   Makefile
    modified:   check.cmd
    new file:   include/os2compat/spawn.h
    new file:   process/posix_spawn/posix_spawn.h
    new file:   process/posix_spawn/spawn_faction_addchdir.c
    new file:   process/posix_spawn/spawn_faction_addclose.c
    new file:   process/posix_spawn/spawn_faction_adddup2.c
    new file:   process/posix_spawn/spawn_faction_addfchdir.c
    new file:   process/posix_spawn/spawn_faction_addopen.c
    new file:   process/posix_spawn/spawn_faction_destroy.c
    new file:   process/posix_spawn/spawn_faction_init.c
    new file:   process/posix_spawn/spawn_int.h
    new file:   process/posix_spawn/spawnattr_destroy.c
    new file:   process/posix_spawn/spawnattr_getdefault.c
    new file:   process/posix_spawn/spawnattr_getflags.c
    new file:   process/posix_spawn/spawnattr_getpgroup.c
    new file:   process/posix_spawn/spawnattr_getschedparam.c
    new file:   process/posix_spawn/spawnattr_getschedpolicy.c
    new file:   process/posix_spawn/spawnattr_getsigmask.c
    new file:   process/posix_spawn/spawnattr_init.c
    new file:   process/posix_spawn/spawnattr_setdefault.c
    new file:   process/posix_spawn/spawnattr_setflags.c
    new file:   process/posix_spawn/spawnattr_setpgroup.c
    new file:   process/posix_spawn/spawnattr_setschedparam.c
    new file:   process/posix_spawn/spawnattr_setschedpolicy.c
    new file:   process/posix_spawn/spawnattr_setsigmask.c
    new file:   process/posix_spawn/spawne.c
    new file:   process/posix_spawn/spawni.c
    new file:   process/posix_spawn/spawnpe.c
    modified:   testcase/Makefile
    new file:   testcase/gltests/executable-script
    new file:   testcase/gltests/executable-script.sh
    new file:   testcase/gltests/executable-shell-script
    new file:   testcase/gltests/macros.h
    new file:   testcase/gltests/test-posix_spawn-chdir.c
    new file:   testcase/gltests/test-posix_spawn-dup2-stdin.c
    new file:   testcase/gltests/test-posix_spawn-dup2-stdin.sh
    new file:   testcase/gltests/test-posix_spawn-dup2-stdout.c
    new file:   testcase/gltests/test-posix_spawn-dup2-stdout.sh
    new file:   testcase/gltests/test-posix_spawn-fchdir.c
    new file:   testcase/gltests/test-posix_spawn-inherit0.c
    new file:   testcase/gltests/test-posix_spawn-inherit1.c
    new file:   testcase/gltests/test-posix_spawn-open1.c
    new file:   testcase/gltests/test-posix_spawn-open2.c
    new file:   testcase/gltests/test-posix_spawn-script.c
    new file:   testcase/gltests/test-posix_spawn_file_actions_addchdir.c
    new file:   testcase/gltests/test-posix_spawn_file_actions_addclose.c
    new file:   testcase/gltests/test-posix_spawn_file_actions_adddup2.c
    new file:   testcase/gltests/test-posix_spawn_file_actions_addfchdir.c
    new file:   testcase/gltests/test-posix_spawn_file_actions_addopen.c
    new file:   testcase/gltests/test-posix_spawnp-script.c
  • Loading branch information
komh committed Nov 7, 2024
1 parent 95badf1 commit ce370db
Show file tree
Hide file tree
Showing 51 changed files with 3,678 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ BIN_PROGRAMS :=

BIN_LIBRARIES := os2compat

os2compat_DIRS := io memory network process thread
os2compat_DIRS := io memory network process thread process/posix_spawn
os2compat_SRCS := $(foreach d,$(os2compat_DIRS),$(wildcard $(d)/*.c))
os2compat_LIB := 1
os2compat_DLL := 1
Expand Down Expand Up @@ -177,6 +177,7 @@ HEADER_FILES := include/os2compat/dirent.h \
include/os2compat/poll.h \
include/os2compat/semaphore.h \
include/os2compat/sched.h \
include/os2compat/spawn.h \
include/os2compat/spawn2.h \
include/os2compat/net/if.h \
include/os2compat/sys/mman.h \
Expand All @@ -194,6 +195,7 @@ PRIV_HEADER_FILES := io/scandir.h \
network/socklen_t.h \
network/xpoll.h \
process/spawn2.h \
process/posix_spawn/posix_spawn.h \
thread/semaphore.h \
thread/sched_yield.h

Expand Down
20 changes: 20 additions & 0 deletions check.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ call checkCC 'network/getifaddrs.h', 'network/getifaddrs.c', ,
call checkCC 'process/spawn2.h', 'process/spawn2.c', ,
'spawn2ve( 0, 0, 0, 0, 0, 0 );' || g.sNl ||,
'spawn2vpe( 0, 0, 0, 0, 0, 0 );';
call checkCC 'process/posix_spawn/posix_spawn.h', ,
'process/posix_spawn/spawn_faction_addchdir.c',
'process/posix_spawn/spawn_faction_init.c', ,
'posix_spawn_file_actions_addchdir( 0, 0 );';
call checkCC 'process/posix_spawn/posix_spawn.h', ,
'process/posix_spawn/spawn_faction_addfchdir.c',
'process/posix_spawn/spawn_faction_init.c', ,
'posix_spawn_file_actions_addfchdir( 0, 0 );';
call checkCC 'spawn.h', ,
'process/posix_spawn/spawne.c',
'process/posix_spawn/spawni.c',
'process/spawn2.c', ,
'posix_spawn( 0, 0, 0, 0, 0, 0 );';
call checkCC 'spawn.h', ,
'process/posix_spawn/spawnpe.c',
'process/posix_spawn/spawni.c',
'process/spawn2.c', ,
'posix_spawnp( 0, 0, 0, 0, 0, 0 );';

call checkOS2CompatHeader;

Expand Down Expand Up @@ -136,6 +154,7 @@ checkOS2CompatHeader: procedure expose G.
'include/os2compat/poll.h',
'include/os2compat/semaphore.h',
'include/os2compat/sched.h',
'include/os2compat/spawn.h',
'include/os2compat/spawn2.h',
'include/os2compat/net/if.h',
'include/os2compat/sys/mman.h',
Expand All @@ -152,6 +171,7 @@ checkOS2CompatHeader: procedure expose G.
'network/socklen_t.h',
'network/xpoll.h',
'process/spawn2.h',
'process/posix_spawn/posix_spawn.h',
'thread/semaphore.h',
'thread/sched_yield.h';
sDestDir = 'include/os2compat/priv';
Expand Down
18 changes: 18 additions & 0 deletions include/os2compat/spawn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* spawn.h for os2compat
*
* Copyright (C) 2024 KO Myung-Hun <[email protected]>
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://www.wtfpl.net/ for more details.
*/

#ifndef OS2COMPAT_INCLUDE_SPAWN_H
#define OS2COMPAT_INCLUDE_SPAWN_H

#include "priv/process/posix_spawn/posix_spawn.h"

#endif
39 changes: 39 additions & 0 deletions process/posix_spawn/posix_spawn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* posix_spawn_file_actions_addchdir() and posix_spawn_file_actions_addfchdir()
* implementation for OS/2 kLIBC
*
* Copyright (C) 2024 KO Myung-Hun <[email protected]>
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://www.wtfpl.net/ for more details.
*/

#ifndef OS2COMPAT_POSIX_SPAWN_H
#define OS2COMPAT_POSIX_SPAWN_H

#include <spawn.h>

#ifdef __cplusplus
extern "C" {
#endif

int os2compat_posix_spawn_file_actions_addchdir(posix_spawn_file_actions_t
*restrict file_actions, const char *restrict path);

int os2compat_posix_spawn_file_actions_addfchdir(posix_spawn_file_actions_t
*file_actions, int fildes);

#define posix_spawn_file_actions_addchdir \
os2compat_posix_spawn_file_actions_addchdir

#define posix_spawn_file_actions_addfchdir \
os2compat_posix_spawn_file_actions_addfchdir

#ifdef __cplusplus
}
#endif

#endif
63 changes: 63 additions & 0 deletions process/posix_spawn/spawn_faction_addchdir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Copyright (C) 2018-2024 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */

/* Specification. */
#include <spawn.h>

#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include <emx/umalloc.h>

#include "spawn_int.h"

#include "posix_spawn.h"

/* Add an action to FILE-ACTIONS which tells the implementation to call
'chdir' to the given directory during the 'spawn' call. */
int
posix_spawn_file_actions_addchdir (posix_spawn_file_actions_t *file_actions,
const char *path)
{
/* Copy PATH, because the caller may free it before calling posix_spawn()
or posix_spawnp(). */
char *path_copy = _hstrdup (path);
if (path_copy == NULL)
return ENOMEM;

/* Allocate more memory if needed. */
if (file_actions->__used == file_actions->__allocated
&& __posix_spawn_file_actions_realloc (file_actions) != 0)
{
/* This can only mean we ran out of memory. */
free (path_copy);
return ENOMEM;
}

{
struct __spawn_action *rec;

/* Add the new value. */
rec = &file_actions->__actions[file_actions->__used];
rec->tag = spawn_do_chdir;
rec->action.chdir_action.path = path_copy;

/* Account for the new entry. */
++file_actions->__used;

return 0;
}
}
58 changes: 58 additions & 0 deletions process/posix_spawn/spawn_faction_addclose.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Copyright (C) 2000, 2009-2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */

/* Specification. */
#include <spawn.h>

#include <errno.h>
#include <unistd.h>

#define __sysconf(open_max) getdtablesize ()

#include "spawn_int.h"

/* Add an action to FILE-ACTIONS which tells the implementation to call
'close' for the given file descriptor during the 'spawn' call. */
int
posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *file_actions,
int fd)
{
int maxfd = __sysconf (_SC_OPEN_MAX);

/* Test for the validity of the file descriptor. */
if (fd < 0 || fd >= maxfd)
return EBADF;

/* Allocate more memory if needed. */
if (file_actions->__used == file_actions->__allocated
&& __posix_spawn_file_actions_realloc (file_actions) != 0)
/* This can only mean we ran out of memory. */
return ENOMEM;

{
struct __spawn_action *rec;

/* Add the new value. */
rec = &file_actions->__actions[file_actions->__used];
rec->tag = spawn_do_close;
rec->action.open_action.fd = fd;

/* Account for the new entry. */
++file_actions->__used;

return 0;
}
}
59 changes: 59 additions & 0 deletions process/posix_spawn/spawn_faction_adddup2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Copyright (C) 2000, 2009-2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */

/* Specification. */
#include <spawn.h>

#include <errno.h>
#include <unistd.h>

#define __sysconf(open_max) getdtablesize ()

#include "spawn_int.h"

/* Add an action to FILE-ACTIONS which tells the implementation to call
'dup2' for the given file descriptors during the 'spawn' call. */
int
posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *file_actions,
int fd, int newfd)
{
int maxfd = __sysconf (_SC_OPEN_MAX);

/* Test for the validity of the file descriptor. */
if (fd < 0 || newfd < 0 || fd >= maxfd || newfd >= maxfd)
return EBADF;

/* Allocate more memory if needed. */
if (file_actions->__used == file_actions->__allocated
&& __posix_spawn_file_actions_realloc (file_actions) != 0)
/* This can only mean we ran out of memory. */
return ENOMEM;

{
struct __spawn_action *rec;

/* Add the new value. */
rec = &file_actions->__actions[file_actions->__used];
rec->tag = spawn_do_dup2;
rec->action.dup2_action.fd = fd;
rec->action.dup2_action.newfd = newfd;

/* Account for the new entry. */
++file_actions->__used;

return 0;
}
}
59 changes: 59 additions & 0 deletions process/posix_spawn/spawn_faction_addfchdir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Copyright (C) 2018-2024 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */

/* Specification. */
#include <spawn.h>

#include <errno.h>
#include <unistd.h>

#define __sysconf(open_max) getdtablesize ()

#include "spawn_int.h"

#include "posix_spawn.h"

/* Add an action to FILE-ACTIONS which tells the implementation to call
'fchdir' to the given directory during the 'spawn' call. */
int
posix_spawn_file_actions_addfchdir (posix_spawn_file_actions_t *file_actions,
int fd)
{
int maxfd = __sysconf (_SC_OPEN_MAX);

/* Test for the validity of the file descriptor. */
if (fd < 0 || fd >= maxfd)
return EBADF;

/* Allocate more memory if needed. */
if (file_actions->__used == file_actions->__allocated
&& __posix_spawn_file_actions_realloc (file_actions) != 0)
/* This can only mean we ran out of memory. */
return ENOMEM;

{
struct __spawn_action *rec;

/* Add the new value. */
rec = &file_actions->__actions[file_actions->__used];
rec->tag = spawn_do_fchdir;
rec->action.fchdir_action.fd = fd;

/* Account for the new entry. */
++file_actions->__used;

return 0;
}
}
Loading

0 comments on commit ce370db

Please sign in to comment.