Comparison

util-src/pposix.c @ 13563:5d4d7ed83f1a

util.pposix: Add pipe() (with support for pipe2() flags on Linux)
author Matthew Wild <mwild1@gmail.com>
date Sat, 16 Nov 2024 12:26:55 +0000
parent 13184:d16845afb3e2
child 13574:f29d15aef6f8
comparison
equal deleted inserted replaced
13562:f77c247258cc 13563:5d4d7ed83f1a
11 /* 11 /*
12 * pposix.c 12 * pposix.c
13 * POSIX support functions for Lua 13 * POSIX support functions for Lua
14 */ 14 */
15 15
16 #define MODULE_VERSION "0.4.0" 16 #define MODULE_VERSION "0.4.1"
17 17
18 18
19 #if defined(__linux__) 19 #if defined(__linux__)
20 #ifndef _GNU_SOURCE 20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 21 #define _GNU_SOURCE
652 (void)L; 652 (void)L;
653 abort(); 653 abort();
654 return 0; 654 return 0;
655 } 655 }
656 656
657 const char *pipe_flag_names[] = {
658 "cloexec",
659 "direct",
660 "nonblock"
661 };
662 const int pipe_flag_values[] = {
663 O_CLOEXEC,
664 O_DIRECT,
665 O_NONBLOCK
666 };
667
668
669 static int lc_pipe(lua_State *L) {
670 int fds[2];
671 int nflags = lua_gettop(L);
672
673 #if defined(__linux__)
674 int flags=0;
675 for(int i = 1; i<=nflags; i++) {
676 int flag_index = luaL_checkoption(L, i, NULL, pipe_flag_names);
677 flags |= pipe_flag_values[flag_index];
678 }
679
680 if(pipe2(fds, flags) == -1) {
681 #else
682 if(nflags != 0) {
683 luaL_argerror(L, 1, "Flags are not supported on this platform");
684 }
685 if(pipe(fds) == -1) {
686 #endif
687 luaL_pushfail(L);
688 lua_pushstring(L, strerror(errno));
689 return 2;
690 }
691
692 lua_pushinteger(L, fds[0]);
693 lua_pushinteger(L, fds[1]);
694 return 2;
695 }
696
657 static int lc_uname(lua_State *L) { 697 static int lc_uname(lua_State *L) {
658 struct utsname uname_info; 698 struct utsname uname_info;
659 699
660 if(uname(&uname_info) != 0) { 700 if(uname(&uname_info) != 0) {
661 luaL_pushfail(L); 701 luaL_pushfail(L);
868 908
869 { "umask", lc_umask }, 909 { "umask", lc_umask },
870 910
871 { "mkdir", lc_mkdir }, 911 { "mkdir", lc_mkdir },
872 912
913 { "pipe", lc_pipe },
914
873 { "setrlimit", lc_setrlimit }, 915 { "setrlimit", lc_setrlimit },
874 { "getrlimit", lc_getrlimit }, 916 { "getrlimit", lc_getrlimit },
875 917
876 { "uname", lc_uname }, 918 { "uname", lc_uname },
877 919