comparison src/syscalls.cc @ 5453:89f5979e8552

[project @ 2005-09-17 00:50:58 by jwe]
author jwe
date Sat, 17 Sep 2005 00:50:58 +0000
parents ac8d64b9e76a
children ec44bd0917fe
comparison
equal deleted inserted replaced
5452:5a70e4162b25 5453:89f5979e8552
55 #include "oct-map.h" 55 #include "oct-map.h"
56 #include "oct-obj.h" 56 #include "oct-obj.h"
57 #include "oct-stdstrm.h" 57 #include "oct-stdstrm.h"
58 #include "oct-stream.h" 58 #include "oct-stream.h"
59 #include "sysdep.h" 59 #include "sysdep.h"
60 #include "syswait.h"
61 #include "utils.h" 60 #include "utils.h"
62 #include "variables.h" 61 #include "variables.h"
63 62
64 static Octave_map 63 static Octave_map
65 mk_stat_map (const file_stat& fs) 64 mk_stat_map (const file_stat& fs)
855 return retval; 854 return retval;
856 } 855 }
857 856
858 DEFUN (waitpid, args, , 857 DEFUN (waitpid, args, ,
859 "-*- texinfo -*-\n\ 858 "-*- texinfo -*-\n\
860 @deftypefn {Built-in Function} {[@var{pid}, @var{msg}] =} waitpid (@var{pid}, @var{options})\n\ 859 @deftypefn {Built-in Function} {[@var{pid}, @var{status}, @var{msg}] =} waitpid (@var{pid}, @var{options})\n\
861 Wait for process @var{pid} to terminate. The @var{pid} argument can be:\n\ 860 Wait for process @var{pid} to terminate. The @var{pid} argument can be:\n\
862 \n\ 861 \n\
863 @table @asis\n\ 862 @table @asis\n\
864 @item @minus{}1\n\ 863 @item @minus{}1\n\
865 Wait for any child process.\n\ 864 Wait for any child process.\n\
870 \n\ 869 \n\
871 @item > 0\n\ 870 @item > 0\n\
872 Wait for termination of the child process with ID @var{pid}.\n\ 871 Wait for termination of the child process with ID @var{pid}.\n\
873 @end table\n\ 872 @end table\n\
874 \n\ 873 \n\
875 The @var{options} argument can be:\n\ 874 The @var{options} argument can be a bitwise OR of zero or more of\n\
876 \n\ 875 the following constants:\n\
877 @table @asis\n\ 876 \n\
877 @table @code\n\
878 @item 0\n\ 878 @item 0\n\
879 Wait until signal is received or a child process exits (this is the\n\ 879 Wait until signal is received or a child process exits (this is the\n\
880 default if the @var{options} argument is missing).\n\ 880 default if the @var{options} argument is missing).\n\
881 \n\ 881 \n\
882 @item 1\n\ 882 @item WNOHANG\n\
883 Do not hang if status is not immediately available.\n\ 883 Do not hang if status is not immediately available.\n\
884 \n\ 884 \n\
885 @item 2\n\ 885 @item WUNTRACED\n\
886 Report the status of any child processes that are stopped, and whose\n\ 886 Report the status of any child processes that are stopped, and whose\n\
887 status has not yet been reported since they stopped.\n\ 887 status has not yet been reported since they stopped.\n\
888 \n\ 888 \n\
889 @item 3\n\ 889 @item WCONTINUED\n\
890 Implies both 1 and 2.\n\ 890 Return if a stopped child has been resumed by delivery of @code{SIGCONT}.\n\
891 This value may not be meaningful on all systems.\n\
891 @end table\n\ 892 @end table\n\
892 \n\ 893 \n\
893 If the returned value of @var{pid} is greater than 0, it is the process\n\ 894 If the returned value of @var{pid} is greater than 0, it is the process\n\
894 ID of the child process that exited. If an error occurs, @var{pid} will\n\ 895 ID of the child process that exited. If an error occurs, @var{pid} will\n\
895 be less than zero and @var{msg} will contain a system-dependent error\n\ 896 be less than zero and @var{msg} will contain a system-dependent error\n\
896 message.\n\ 897 message. The value of @var{status} contains additional system-depenent\n\
898 information about the subprocess that exited.\n\
899 @seealso{WNOHANG, WUNTRACED, WCONTINUED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WCOREDUMP, WIFSTOPPED, WSTOPSIG, WIFCONTINUED}\n\
897 @end deftypefn") 900 @end deftypefn")
898 { 901 {
899 octave_value_list retval; 902 octave_value_list retval;
900 903
901 retval(1) = std::string (); 904 retval(2) = std::string ();
905 retval(1) = 0;
902 retval(0) = -1; 906 retval(0) = -1;
903 907
904 int nargin = args.length (); 908 int nargin = args.length ();
905 909
906 if (nargin == 1 || nargin == 2) 910 if (nargin == 1 || nargin == 2)
910 if (! error_state) 914 if (! error_state)
911 { 915 {
912 int options = 0; 916 int options = 0;
913 917
914 if (args.length () == 2) 918 if (args.length () == 2)
915 { 919 options = args(1).int_value (true);
916 options = args(1).int_value (true);
917
918 if (! error_state)
919 {
920 if (options < 0 || options > 3)
921 error ("waitpid: invalid OPTIONS value specified");
922 }
923 else
924 error ("waitpid: OPTIONS must be in integer");
925 }
926 920
927 if (! error_state) 921 if (! error_state)
928 { 922 {
929 std::string msg; 923 std::string msg;
930 924
931 pid_t status = octave_syscalls::waitpid (pid, options, msg); 925 int status = 0;
932 926
933 retval(0) = status; 927 pid_t result = octave_syscalls::waitpid (pid, &status, options, msg);
934 retval(1) = msg; 928
929 retval(0) = result;
930 retval(1) = status;
931 retval(2) = msg;
935 } 932 }
933 else
934 error ("waitpid: OPTIONS must be an integer");
936 } 935 }
937 else 936 else
938 error ("waitpid: PID must be an integer value"); 937 error ("waitpid: PID must be an integer value");
939 } 938 }
940 else 939 else
941 print_usage ("waitpid"); 940 print_usage ("waitpid");
941
942 return retval;
943 }
944
945 DEFUNX ("WIFEXITED", FWIFEXITED, args, ,
946 "-*- texinfo -*-\n\
947 @deftypefn {Built-in Function} {} WIFEXITED (@var{status})\n\
948 Given @var{status} from a call to @code{waitpid}, return true if the\n\
949 child terminated normally.\n\
950 @seealso{waitpid, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WCOREDUMP, WIFSTOPPED, WSTOPSIG, WIFCONTINUED}\n\
951 @end deftypefn\n")
952 {
953 octave_value retval = 0.0;
954
955 #if defined (WIFEXITED)
956 if (args.length () == 1)
957 {
958 int status = args(0).int_value ();
959
960 if (! error_state)
961 retval = WIFEXITED (status);
962 else
963 error ("WIFEXITED: expecting integer argument");
964 }
965 #else
966 warning ("WIFEXITED always returns false in this version of Octave")
967 #endif
968
969 return retval;
970 }
971
972 DEFUNX ("WEXITSTATUS", FWEXITSTATUS, args, ,
973 "-*- texinfo -*-\n\
974 @deftypefn {Built-in Function} {} WEXITSTATUS (@var{status})\n\
975 Given @var{status} from a call to @code{waitpid}, return the exit\n\
976 status of the child. This function should only be employed if\n\
977 @code{WIFEXITED} returned true.\n\
978 @seealso{waitpid, WIFEXITED, WIFSIGNALED, WTERMSIG, WCOREDUMP, WIFSTOPPED, WSTOPSIG, WIFCONTINUED}\n\
979 @end deftypefn")
980 {
981 octave_value retval = 0.0;
982
983 #if defined (WEXITSTATUS)
984 if (args.length () == 1)
985 {
986 int status = args(0).int_value ();
987
988 if (! error_state)
989 retval = WEXITSTATUS (status);
990 else
991 error ("WEXITSTATUS: expecting integer argument");
992 }
993 #else
994 warning ("WEXITSTATUS always returns false in this version of Octave")
995 #endif
996
997 return retval;
998 }
999
1000 DEFUNX ("WIFSIGNALED", FWIFSIGNALED, args, ,
1001 "-*- texinfo -*-\n\
1002 @deftypefn {Built-in Function} {} WIFSIGNALED (@var{status})\n\
1003 Given @var{status} from a call to @code{waitpid}, return true if the\n\
1004 child process was terminated by a signal.\n\
1005 @seealso{waitpid, WIFEXITED, WEXITSTATUS, WTERMSIG, WCOREDUMP, WIFSTOPPED, WSTOPSIG, WIFCONTINUED}\n\
1006 @end deftypefn")
1007 {
1008 octave_value retval = 0.0;
1009
1010 #if defined (WIFSIGNALED)
1011 if (args.length () == 1)
1012 {
1013 int status = args(0).int_value ();
1014
1015 if (! error_state)
1016 retval = WIFSIGNALED (status);
1017 else
1018 error ("WIFSIGNALED: expecting integer argument");
1019 }
1020 #else
1021 warning ("WIFSIGNALED always returns false in this version of Octave")
1022 #endif
1023
1024 return retval;
1025 }
1026
1027 DEFUNX ("WTERMSIG", FWTERMSIG, args, ,
1028 "-*- texinfo -*-\n\
1029 @deftypefn {Built-in Function} {} WTERMSIG (@var{status})\n\
1030 Given @var{status} from a call to @code{waitpid}, return the number of\n\
1031 the signal that caused the child process to terminate. This function\n\
1032 should only be employed if @code{WIFSIGNALED} returned true.\n\
1033 @seealso{waitpid, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WCOREDUMP, WIFSTOPPED, WSTOPSIG, WIFCONTINUED}\n\
1034 @end deftypefn")
1035 {
1036 octave_value retval = 0.0;
1037
1038 #if defined (WTERMSIG)
1039 if (args.length () == 1)
1040 {
1041 int status = args(0).int_value ();
1042
1043 if (! error_state)
1044 retval = WTERMSIG (status);
1045 else
1046 error ("WTERMSIG: expecting integer argument");
1047 }
1048 #else
1049 warning ("WTERMSIG always returns false in this version of Octave")
1050 #endif
1051
1052 return retval;
1053 }
1054
1055 DEFUNX ("WCOREDUMP", FWCOREDUMP, args, ,
1056 "-*- texinfo -*-\n\
1057 @deftypefn {Built-in Function} {} WCOREDUMP (@var{status})\n\
1058 Given @var{status} from a call to @code{waitpid}, return true if the\n\
1059 child produced a core dump. This function should only be employed if\n\
1060 @code{WIFSIGNALED} returned true. The macro used to implement this\n\
1061 function is not specified in POSIX.1-2001 and is not available on some\n\
1062 Unix implementations (e.g., AIX, SunOS).\n\
1063 @seealso{waitpid, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WIFSTOPPED, WSTOPSIG, WIFCONTINUED}\n\
1064 @end deftypefn")
1065 {
1066 octave_value retval = 0.0;
1067
1068 #if defined (WCOREDUMP)
1069 if (args.length () == 1)
1070 {
1071 int status = args(0).int_value ();
1072
1073 if (! error_state)
1074 retval = WCOREDUMP (status);
1075 else
1076 error ("WCOREDUMP: expecting integer argument");
1077 }
1078 #else
1079 warning ("WCOREDUMP always returns false in this version of Octave")
1080 #endif
1081
1082 return retval;
1083 }
1084
1085 DEFUNX ("WIFSTOPPED", FWIFSTOPPED, args, ,
1086 "-*- texinfo -*-\n\
1087 @deftypefn {Built-in Function} {} WIFSTOPPED (@var{status})\n\
1088 Given @var{status} from a call to @code{waitpid}, return true if the\n\
1089 child process was stopped by delivery of a signal; this is only\n\
1090 possible if the call was done using @code{WUNTRACED} or when the child\n\
1091 is being traced (see ptrace(2)).\n\
1092 @seealso{waitpid, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WCOREDUMP, WSTOPSIG, WIFCONTINUED}\n\
1093 @end deftypefn")
1094 {
1095 octave_value retval = 0.0;
1096
1097 #if defined (WIFSTOPPED)
1098 if (args.length () == 1)
1099 {
1100 int status = args(0).int_value ();
1101
1102 if (! error_state)
1103 retval = WIFSTOPPED (status);
1104 else
1105 error ("WIFSTOPPED: expecting integer argument");
1106 }
1107 #else
1108 warning ("WIFSTOPPED always returns false in this version of Octave")
1109 #endif
1110
1111 return retval;
1112 }
1113
1114 DEFUNX ("WSTOPSIG", FWSTOPSIG, args, ,
1115 "-*- texinfo -*-\n\
1116 @deftypefn {Built-in Function} {} WSTOPSIG (@var{status})\n\
1117 Given @var{status} from a call to @code{waitpid}, return the number of\n\
1118 the signal which caused the child to stop. This function should only\n\
1119 be employed if @code{WIFSTOPPED} returned true.\n\
1120 @seealso{waitpid, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WCOREDUMP, WIFSTOPPED, WIFCONTINUED}\n\
1121 @end deftypefn")
1122 {
1123 octave_value retval = 0.0;
1124
1125 #if defined (WSTOPSIG)
1126 if (args.length () == 1)
1127 {
1128 int status = args(0).int_value ();
1129
1130 if (! error_state)
1131 retval = WSTOPSIG (status);
1132 else
1133 error ("WSTOPSIG: expecting integer argument");
1134 }
1135 #else
1136 warning ("WSTOPSIG always returns false in this version of Octave")
1137 #endif
1138
1139 return retval;
1140 }
1141
1142 DEFUNX ("WIFCONTINUED", FWIFCONTINUED, args, ,
1143 "-*- texinfo -*-\n\
1144 @deftypefn {Built-in Function} {} WIFCONTINUED (@var{status})\n\
1145 Given @var{status} from a call to @code{waitpid}, return true if the\n\
1146 child process was resumed by delivery of @code{SIGCONT}.\n\
1147 @seealso{waitpid, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WCOREDUMP, WIFSTOPPED, WSTOPSIG}\n\
1148 @end deftypefn")
1149 {
1150 octave_value retval = 0.0;
1151
1152 #if defined (WIFCONTINUED)
1153 if (args.length () == 1)
1154 {
1155 int status = args(0).int_value ();
1156
1157 if (! error_state)
1158 retval = WIFCONTINUED (status);
1159 else
1160 error ("WIFCONTINUED: expecting integer argument");
1161 }
1162 #else
1163 warning ("WIFCONTINUED always returns false in this version of Octave")
1164 #endif
942 1165
943 return retval; 1166 return retval;
944 } 1167 }
945 1168
946 DEFUN (canonicalize_file_name, args, , 1169 DEFUN (canonicalize_file_name, args, ,
1113 @defvr {Built-in Constant} O_WRONLY\n\ 1336 @defvr {Built-in Constant} O_WRONLY\n\
1114 File status flag, file opened for writing only.\n\ 1337 File status flag, file opened for writing only.\n\
1115 @seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC}\n\ 1338 @seealso{fcntl, O_APPEND, O_ASYNC, O_CREAT, O_EXCL, O_NONBLOCK, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC}\n\
1116 @end defvr"); 1339 @end defvr");
1117 #endif 1340 #endif
1341
1342 #if !defined (WNOHANG)
1343 #define WNOHANG 0
1344 #endif
1345
1346 DEFCONSTX ("WNOHANG", SBV_WNOHANG, WNOHANG,
1347 "-*- texinfo -*-\n\
1348 @defvr {Built-in Constant} WNOHANG\n\
1349 Option value for @code{waitpid}.\n\
1350 @seealso{waitpid, WUNTRACED, WCONTINUE}\n\
1351 @end defvr");
1352
1353 #if !defined (WUNTRACED)
1354 #define WUNTRACED 0
1355 #endif
1356
1357 DEFCONSTX ("WUNTRACED", SBV_WUNTRACED, WUNTRACED,
1358 "-*- texinfo -*-\n\
1359 @defvr {Built-in Constant} WUNTRACED\n\
1360 Option value for @code{waitpid}.\n\
1361 @seealso{waitpid, WNOHANG, WCONTINUE}\n\
1362 @end defvr");
1363
1364 #if !defined (WCONTINUE)
1365 #define WCONTINUE 0
1366 #endif
1367
1368 DEFCONSTX ("WCONTINUE", SBV_WCONTINUE, WCONTINUE,
1369 "-*- texinfo -*-\n\
1370 @defvr {Built-in Constant} WCONINTUE\n\
1371 Option value for @code{waitpid}.\n\
1372 @seealso{waitpid, WNOHANG, WUNTRACED}\n\
1373 @end defvr");
1118 1374
1119 } 1375 }
1120 1376
1121 /* 1377 /*
1122 ;;; Local Variables: *** 1378 ;;; Local Variables: ***