root/ext/posix/posix.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. PHP_MINFO_FUNCTION
  2. PHP_GINIT_FUNCTION
  3. PHP_MINIT_FUNCTION
  4. PHP_FUNCTION
  5. PHP_FUNCTION
  6. PHP_FUNCTION
  7. PHP_FUNCTION
  8. PHP_FUNCTION
  9. PHP_FUNCTION
  10. PHP_FUNCTION
  11. PHP_FUNCTION
  12. PHP_FUNCTION
  13. PHP_FUNCTION
  14. PHP_FUNCTION
  15. PHP_FUNCTION
  16. PHP_FUNCTION
  17. PHP_FUNCTION
  18. PHP_FUNCTION
  19. PHP_FUNCTION
  20. PHP_FUNCTION
  21. PHP_FUNCTION
  22. PHP_FUNCTION
  23. PHP_FUNCTION
  24. PHP_FUNCTION
  25. php_posix_stream_get_fd
  26. PHP_FUNCTION
  27. PHP_FUNCTION
  28. PHP_FUNCTION
  29. PHP_FUNCTION
  30. PHP_FUNCTION
  31. php_posix_group_to_array
  32. PHP_FUNCTION
  33. PHP_FUNCTION
  34. PHP_FUNCTION
  35. php_posix_passwd_to_array
  36. PHP_FUNCTION
  37. PHP_FUNCTION
  38. posix_addlimit
  39. PHP_FUNCTION
  40. PHP_FUNCTION
  41. PHP_FUNCTION
  42. PHP_FUNCTION
  43. PHP_FUNCTION

   1 /*
   2    +----------------------------------------------------------------------+
   3    | PHP Version 7                                                        |
   4    +----------------------------------------------------------------------+
   5    | Copyright (c) 1997-2016 The PHP Group                                |
   6    +----------------------------------------------------------------------+
   7    | This source file is subject to version 3.01 of the PHP license,      |
   8    | that is bundled with this package in the file LICENSE, and is        |
   9    | available through the world-wide-web at the following url:           |
  10    | http://www.php.net/license/3_01.txt                                  |
  11    | If you did not receive a copy of the PHP license and are unable to   |
  12    | obtain it through the world-wide-web, please send a note to          |
  13    | license@php.net so we can mail you a copy immediately.               |
  14    +----------------------------------------------------------------------+
  15    | Author: Kristian Koehntopp <kris@koehntopp.de>                       |
  16    +----------------------------------------------------------------------+
  17  */
  18 
  19 /* $Id: e1f4ef62622ef222ff6df750254ece3184fd53ef $ */
  20 
  21 #ifdef HAVE_CONFIG_H
  22 #include "config.h"
  23 #endif
  24 
  25 #include "php.h"
  26 #include <unistd.h>
  27 #include "ext/standard/info.h"
  28 #include "ext/standard/php_string.h"
  29 #include "php_posix.h"
  30 
  31 
  32 #if HAVE_POSIX
  33 
  34 #ifdef HAVE_SYS_TIME_H
  35 #include <sys/time.h>
  36 #endif
  37 
  38 #include <sys/resource.h>
  39 
  40 #if defined(_GNU_SOURCE) && !defined(__USE_GNU)
  41 # define __USE_GNU
  42 #endif
  43 
  44 #include <sys/utsname.h>
  45 #include <sys/types.h>
  46 #include <sys/stat.h>
  47 #include <signal.h>
  48 #include <sys/times.h>
  49 #include <errno.h>
  50 #include <grp.h>
  51 #include <pwd.h>
  52 #if HAVE_SYS_MKDEV_H
  53 # include <sys/mkdev.h>
  54 #endif
  55 
  56 ZEND_DECLARE_MODULE_GLOBALS(posix)
  57 static PHP_MINFO_FUNCTION(posix);
  58 
  59 /* {{{ arginfo */
  60 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_kill, 0, 0, 2)
  61         ZEND_ARG_INFO(0, pid)
  62         ZEND_ARG_INFO(0, sig)
  63 ZEND_END_ARG_INFO()
  64 
  65 ZEND_BEGIN_ARG_INFO(arginfo_posix_getpid, 0)
  66 ZEND_END_ARG_INFO()
  67 
  68 ZEND_BEGIN_ARG_INFO(arginfo_posix_getppid, 0)
  69 ZEND_END_ARG_INFO()
  70 
  71 ZEND_BEGIN_ARG_INFO(arginfo_posix_getuid, 0)
  72 ZEND_END_ARG_INFO()
  73 
  74 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_setuid, 0, 0, 1)
  75         ZEND_ARG_INFO(0, uid)
  76 ZEND_END_ARG_INFO()
  77 
  78 ZEND_BEGIN_ARG_INFO(arginfo_posix_geteuid, 0)
  79 ZEND_END_ARG_INFO()
  80 
  81 #ifdef HAVE_SETEUID
  82 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_seteuid, 0, 0, 1)
  83         ZEND_ARG_INFO(0, uid)
  84 ZEND_END_ARG_INFO()
  85 #endif
  86 
  87 ZEND_BEGIN_ARG_INFO(arginfo_posix_getgid, 0)
  88 ZEND_END_ARG_INFO()
  89 
  90 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_setgid, 0, 0, 1)
  91         ZEND_ARG_INFO(0, gid)
  92 ZEND_END_ARG_INFO()
  93 
  94 ZEND_BEGIN_ARG_INFO(arginfo_posix_getegid, 0)
  95 ZEND_END_ARG_INFO()
  96 
  97 #ifdef HAVE_SETEGID
  98 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_setegid, 0, 0, 1)
  99         ZEND_ARG_INFO(0, gid)
 100 ZEND_END_ARG_INFO()
 101 #endif
 102 
 103 #ifdef HAVE_GETGROUPS
 104 ZEND_BEGIN_ARG_INFO(arginfo_posix_getgroups, 0)
 105 ZEND_END_ARG_INFO()
 106 #endif
 107 
 108 #ifdef HAVE_GETLOGIN
 109 ZEND_BEGIN_ARG_INFO(arginfo_posix_getlogin, 0)
 110 ZEND_END_ARG_INFO()
 111 #endif
 112 
 113 ZEND_BEGIN_ARG_INFO(arginfo_posix_getpgrp, 0)
 114 ZEND_END_ARG_INFO()
 115 
 116 #ifdef HAVE_SETSID
 117 ZEND_BEGIN_ARG_INFO(arginfo_posix_setsid, 0)
 118 ZEND_END_ARG_INFO()
 119 #endif
 120 
 121 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_setpgid, 0, 0, 2)
 122         ZEND_ARG_INFO(0, pid)
 123         ZEND_ARG_INFO(0, pgid)
 124 ZEND_END_ARG_INFO()
 125 
 126 #ifdef HAVE_GETPGID
 127 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_getpgid, 0, 0, 1)
 128         ZEND_ARG_INFO(0, pid)
 129 ZEND_END_ARG_INFO()
 130 #endif
 131 
 132 #ifdef HAVE_GETSID
 133 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_getsid, 0, 0, 1)
 134         ZEND_ARG_INFO(0, pid)
 135 ZEND_END_ARG_INFO()
 136 #endif
 137 
 138 ZEND_BEGIN_ARG_INFO(arginfo_posix_uname, 0)
 139 ZEND_END_ARG_INFO()
 140 
 141 ZEND_BEGIN_ARG_INFO(arginfo_posix_times, 0)
 142 ZEND_END_ARG_INFO()
 143 
 144 #ifdef HAVE_CTERMID
 145 ZEND_BEGIN_ARG_INFO(arginfo_posix_ctermid, 0)
 146 ZEND_END_ARG_INFO()
 147 #endif
 148 
 149 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_ttyname, 0, 0, 1)
 150         ZEND_ARG_INFO(0, fd)
 151 ZEND_END_ARG_INFO()
 152 
 153 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_isatty, 0, 0, 1)
 154         ZEND_ARG_INFO(0, fd)
 155 ZEND_END_ARG_INFO()
 156 
 157 ZEND_BEGIN_ARG_INFO(arginfo_posix_getcwd, 0)
 158 ZEND_END_ARG_INFO()
 159 
 160 #ifdef HAVE_MKFIFO
 161 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_mkfifo, 0, 0, 2)
 162         ZEND_ARG_INFO(0, pathname)
 163         ZEND_ARG_INFO(0, mode)
 164 ZEND_END_ARG_INFO()
 165 #endif
 166 
 167 #ifdef HAVE_MKNOD
 168 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_mknod, 0, 0, 2)
 169         ZEND_ARG_INFO(0, pathname)
 170         ZEND_ARG_INFO(0, mode)
 171         ZEND_ARG_INFO(0, major)
 172         ZEND_ARG_INFO(0, minor)
 173 ZEND_END_ARG_INFO()
 174 #endif
 175 
 176 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_access, 0, 0, 1)
 177         ZEND_ARG_INFO(0, file)
 178         ZEND_ARG_INFO(0, mode)
 179 ZEND_END_ARG_INFO()
 180 
 181 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_getgrnam, 0, 0, 1)
 182         ZEND_ARG_INFO(0, name)
 183 ZEND_END_ARG_INFO()
 184 
 185 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_getgrgid, 0, 0, 1)
 186         ZEND_ARG_INFO(0, gid)
 187 ZEND_END_ARG_INFO()
 188 
 189 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_getpwnam, 0, 0, 1)
 190         ZEND_ARG_INFO(0, username)
 191 ZEND_END_ARG_INFO()
 192 
 193 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_getpwuid, 0, 0, 1)
 194         ZEND_ARG_INFO(0, uid)
 195 ZEND_END_ARG_INFO()
 196 
 197 #ifdef HAVE_GETRLIMIT
 198 ZEND_BEGIN_ARG_INFO(arginfo_posix_getrlimit, 0)
 199 ZEND_END_ARG_INFO()
 200 #endif
 201 
 202 #ifdef HAVE_SETRLIMIT
 203 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_setrlimit, 0, 0, 3)
 204         ZEND_ARG_INFO(0, resource)
 205         ZEND_ARG_INFO(0, softlimit)
 206         ZEND_ARG_INFO(0, hardlimit)
 207 ZEND_END_ARG_INFO()
 208 #endif
 209 
 210 ZEND_BEGIN_ARG_INFO(arginfo_posix_get_last_error, 0)
 211 ZEND_END_ARG_INFO()
 212 
 213 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_strerror, 0, 0, 1)
 214         ZEND_ARG_INFO(0, errno)
 215 ZEND_END_ARG_INFO()
 216 
 217 #ifdef HAVE_INITGROUPS
 218 ZEND_BEGIN_ARG_INFO_EX(arginfo_posix_initgroups, 0, 0, 2)
 219         ZEND_ARG_INFO(0, name)
 220         ZEND_ARG_INFO(0, base_group_id)
 221 ZEND_END_ARG_INFO()
 222 #endif
 223 /* }}} */
 224 
 225 /* {{{ posix_functions[]
 226  */
 227 const zend_function_entry posix_functions[] = {
 228     /* POSIX.1, 3.3 */
 229         PHP_FE(posix_kill,              arginfo_posix_kill)
 230 
 231         /* POSIX.1, 4.1 */
 232         PHP_FE(posix_getpid,    arginfo_posix_getpid)
 233         PHP_FE(posix_getppid,   arginfo_posix_getppid)
 234 
 235         /* POSIX.1,  4.2 */
 236         PHP_FE(posix_getuid,    arginfo_posix_getuid)
 237         PHP_FE(posix_setuid,    arginfo_posix_setuid)
 238         PHP_FE(posix_geteuid,   arginfo_posix_geteuid)
 239 #ifdef HAVE_SETEUID
 240         PHP_FE(posix_seteuid,   arginfo_posix_seteuid)
 241 #endif
 242         PHP_FE(posix_getgid,    arginfo_posix_getgid)
 243         PHP_FE(posix_setgid,    arginfo_posix_setgid)
 244         PHP_FE(posix_getegid,   arginfo_posix_getegid)
 245 #ifdef HAVE_SETEGID
 246         PHP_FE(posix_setegid,   arginfo_posix_setegid)
 247 #endif
 248 #ifdef HAVE_GETGROUPS
 249         PHP_FE(posix_getgroups, arginfo_posix_getgroups)
 250 #endif
 251 #ifdef HAVE_GETLOGIN
 252         PHP_FE(posix_getlogin,  arginfo_posix_getlogin)
 253 #endif
 254 
 255         /* POSIX.1, 4.3 */
 256         PHP_FE(posix_getpgrp,   arginfo_posix_getpgrp)
 257 #ifdef HAVE_SETSID
 258         PHP_FE(posix_setsid,    arginfo_posix_setsid)
 259 #endif
 260         PHP_FE(posix_setpgid,   arginfo_posix_setpgid)
 261         /* Non-Posix functions which are common */
 262 #ifdef HAVE_GETPGID
 263         PHP_FE(posix_getpgid,   arginfo_posix_getpgid)
 264 #endif /* HAVE_GETPGID */
 265 #ifdef HAVE_GETSID
 266         PHP_FE(posix_getsid,    arginfo_posix_getsid)
 267 #endif /* HAVE_GETSID */
 268 
 269         /* POSIX.1, 4.4 */
 270         PHP_FE(posix_uname,             arginfo_posix_uname)
 271 
 272         /* POSIX.1, 4.5 */
 273         PHP_FE(posix_times,             arginfo_posix_times)
 274 
 275         /* POSIX.1, 4.7 */
 276 #ifdef HAVE_CTERMID
 277         PHP_FE(posix_ctermid,   arginfo_posix_ctermid)
 278 #endif
 279         PHP_FE(posix_ttyname,   arginfo_posix_ttyname)
 280         PHP_FE(posix_isatty,    arginfo_posix_isatty)
 281 
 282     /* POSIX.1, 5.2 */
 283         PHP_FE(posix_getcwd,    arginfo_posix_getcwd)
 284 
 285         /* POSIX.1, 5.4 */
 286 #ifdef HAVE_MKFIFO
 287         PHP_FE(posix_mkfifo,    arginfo_posix_mkfifo)
 288 #endif
 289 #ifdef HAVE_MKNOD
 290         PHP_FE(posix_mknod,             arginfo_posix_mknod)
 291 #endif
 292 
 293         /* POSIX.1, 5.6 */
 294         PHP_FE(posix_access,    arginfo_posix_access)
 295         /* POSIX.1, 9.2 */
 296         PHP_FE(posix_getgrnam,  arginfo_posix_getgrnam)
 297         PHP_FE(posix_getgrgid,  arginfo_posix_getgrgid)
 298         PHP_FE(posix_getpwnam,  arginfo_posix_getpwnam)
 299         PHP_FE(posix_getpwuid,  arginfo_posix_getpwuid)
 300 
 301 #ifdef HAVE_GETRLIMIT
 302         PHP_FE(posix_getrlimit, arginfo_posix_getrlimit)
 303 #endif
 304 #ifdef HAVE_SETRLIMIT
 305         PHP_FE(posix_setrlimit, arginfo_posix_setrlimit)
 306 #endif
 307 
 308         PHP_FE(posix_get_last_error,                                    arginfo_posix_get_last_error)
 309         PHP_FALIAS(posix_errno, posix_get_last_error,   arginfo_posix_get_last_error)
 310         PHP_FE(posix_strerror,                                                  arginfo_posix_strerror)
 311 #ifdef HAVE_INITGROUPS
 312         PHP_FE(posix_initgroups,        arginfo_posix_initgroups)
 313 #endif
 314 
 315         PHP_FE_END
 316 };
 317 /* }}} */
 318 
 319 /* {{{ PHP_MINFO_FUNCTION
 320  */
 321 static PHP_MINFO_FUNCTION(posix)
 322 {
 323         php_info_print_table_start();
 324         php_info_print_table_row(2, "Revision", "$Id: e1f4ef62622ef222ff6df750254ece3184fd53ef $");
 325         php_info_print_table_end();
 326 }
 327 /* }}} */
 328 
 329 static PHP_GINIT_FUNCTION(posix) /* {{{ */
 330 {
 331         posix_globals->last_error = 0;
 332 }
 333 /* }}} */
 334 
 335 /* {{{ PHP_MINIT_FUNCTION(posix)
 336  */
 337 static PHP_MINIT_FUNCTION(posix)
 338 {
 339         REGISTER_LONG_CONSTANT("POSIX_F_OK", F_OK, CONST_CS | CONST_PERSISTENT);
 340         REGISTER_LONG_CONSTANT("POSIX_X_OK", X_OK, CONST_CS | CONST_PERSISTENT);
 341         REGISTER_LONG_CONSTANT("POSIX_W_OK", W_OK, CONST_CS | CONST_PERSISTENT);
 342         REGISTER_LONG_CONSTANT("POSIX_R_OK", R_OK, CONST_CS | CONST_PERSISTENT);
 343 #ifdef S_IFREG
 344         REGISTER_LONG_CONSTANT("POSIX_S_IFREG", S_IFREG, CONST_CS | CONST_PERSISTENT);
 345 #endif
 346 #ifdef S_IFCHR
 347         REGISTER_LONG_CONSTANT("POSIX_S_IFCHR", S_IFCHR, CONST_CS | CONST_PERSISTENT);
 348 #endif
 349 #ifdef S_IFBLK
 350         REGISTER_LONG_CONSTANT("POSIX_S_IFBLK", S_IFBLK, CONST_CS | CONST_PERSISTENT);
 351 #endif
 352 #ifdef S_IFIFO
 353         REGISTER_LONG_CONSTANT("POSIX_S_IFIFO", S_IFIFO, CONST_CS | CONST_PERSISTENT);
 354 #endif
 355 #ifdef S_IFSOCK
 356         REGISTER_LONG_CONSTANT("POSIX_S_IFSOCK", S_IFSOCK, CONST_CS | CONST_PERSISTENT);
 357 #endif
 358 #ifdef RLIMIT_AS
 359         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_AS", RLIMIT_AS, CONST_CS | CONST_PERSISTENT);
 360 #endif
 361 #ifdef RLIMIT_CORE
 362         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_CORE", RLIMIT_CORE, CONST_CS | CONST_PERSISTENT);
 363 #endif
 364 #ifdef RLIMIT_CPU
 365         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_CPU", RLIMIT_CPU, CONST_CS | CONST_PERSISTENT);
 366 #endif
 367 #ifdef RLIMIT_DATA
 368         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_DATA", RLIMIT_DATA, CONST_CS | CONST_PERSISTENT);
 369 #endif
 370 #ifdef RLIMIT_FSIZE
 371         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_FSIZE", RLIMIT_FSIZE, CONST_CS | CONST_PERSISTENT);
 372 #endif
 373 #ifdef RLIMIT_LOCKS
 374         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_LOCKS", RLIMIT_LOCKS, CONST_CS | CONST_PERSISTENT);
 375 #endif
 376 #ifdef RLIMIT_MEMLOCK
 377         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_MEMLOCK", RLIMIT_MEMLOCK, CONST_CS | CONST_PERSISTENT);
 378 #endif
 379 #ifdef RLIMIT_MSGQUEUE
 380         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_MSGQUEUE", RLIMIT_MSGQUEUE, CONST_CS | CONST_PERSISTENT);
 381 #endif
 382 #ifdef RLIMIT_NICE
 383         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_NICE", RLIMIT_NICE, CONST_CS | CONST_PERSISTENT);
 384 #endif
 385 #ifdef RLIMIT_NOFILE
 386         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_NOFILE", RLIMIT_NOFILE, CONST_CS | CONST_PERSISTENT);
 387 #endif
 388 #ifdef RLIMIT_NPROC
 389         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_NPROC", RLIMIT_NPROC, CONST_CS | CONST_PERSISTENT);
 390 #endif
 391 #ifdef RLIMIT_RSS
 392         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_RSS", RLIMIT_RSS, CONST_CS | CONST_PERSISTENT);
 393 #endif
 394 #ifdef RLIMIT_RTPRIO
 395         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_RTPRIO", RLIMIT_RTPRIO, CONST_CS | CONST_PERSISTENT);
 396 #endif
 397 #ifdef RLIMIT_RTTIME
 398         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_RTTIME", RLIMIT_RTTIME, CONST_CS | CONST_PERSISTENT);
 399 #endif
 400 #ifdef RLIMIT_SIGPENDING
 401         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_SIGPENDING", RLIMIT_SIGPENDING, CONST_CS | CONST_PERSISTENT);
 402 #endif
 403 #ifdef RLIMIT_STACK
 404         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_STACK", RLIMIT_STACK, CONST_CS | CONST_PERSISTENT);
 405 #endif
 406 #ifdef HAVE_SETRLIMIT
 407         REGISTER_LONG_CONSTANT("POSIX_RLIMIT_INFINITY", RLIM_INFINITY, CONST_CS | CONST_PERSISTENT);
 408 #endif
 409         return SUCCESS;
 410 }
 411 /* }}} */
 412 
 413 /* {{{ posix_module_entry
 414  */
 415 zend_module_entry posix_module_entry = {
 416         STANDARD_MODULE_HEADER,
 417         "posix",
 418         posix_functions,
 419         PHP_MINIT(posix),
 420         NULL,
 421         NULL,
 422         NULL,
 423         PHP_MINFO(posix),
 424         PHP_POSIX_VERSION,
 425         PHP_MODULE_GLOBALS(posix),
 426         PHP_GINIT(posix),
 427         NULL,
 428         NULL,
 429         STANDARD_MODULE_PROPERTIES_EX
 430 };
 431 /* }}} */
 432 
 433 #ifdef COMPILE_DL_POSIX
 434 ZEND_GET_MODULE(posix)
 435 #endif
 436 
 437 #define PHP_POSIX_NO_ARGS       if (zend_parse_parameters_none() == FAILURE) return;
 438 
 439 #define PHP_POSIX_RETURN_LONG_FUNC(func_name)   \
 440         PHP_POSIX_NO_ARGS       \
 441         RETURN_LONG(func_name());
 442 
 443 #define PHP_POSIX_SINGLE_ARG_FUNC(func_name)    \
 444         zend_long val;  \
 445         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &val) == FAILURE) RETURN_FALSE; \
 446         if (func_name(val) < 0) {       \
 447                 POSIX_G(last_error) = errno;    \
 448                 RETURN_FALSE;   \
 449         }       \
 450         RETURN_TRUE;
 451 
 452 /* {{{ proto bool posix_kill(int pid, int sig)
 453    Send a signal to a process (POSIX.1, 3.3.2) */
 454 
 455 PHP_FUNCTION(posix_kill)
 456 {
 457         zend_long pid, sig;
 458 
 459         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &pid, &sig) == FAILURE) {
 460                 RETURN_FALSE;
 461         }
 462 
 463         if (kill(pid, sig) < 0) {
 464                 POSIX_G(last_error) = errno;
 465                 RETURN_FALSE;
 466         }
 467 
 468         RETURN_TRUE;
 469 }
 470 /* }}} */
 471 
 472 /* {{{ proto int posix_getpid(void)
 473    Get the current process id (POSIX.1, 4.1.1) */
 474 PHP_FUNCTION(posix_getpid)
 475 {
 476         PHP_POSIX_RETURN_LONG_FUNC(getpid);
 477 }
 478 /* }}} */
 479 
 480 /* {{{ proto int posix_getppid(void)
 481    Get the parent process id (POSIX.1, 4.1.1) */
 482 PHP_FUNCTION(posix_getppid)
 483 {
 484         PHP_POSIX_RETURN_LONG_FUNC(getppid);
 485 }
 486 /* }}} */
 487 
 488 /* {{{ proto int posix_getuid(void)
 489    Get the current user id (POSIX.1, 4.2.1) */
 490 PHP_FUNCTION(posix_getuid)
 491 {
 492         PHP_POSIX_RETURN_LONG_FUNC(getuid);
 493 }
 494 /* }}} */
 495 
 496 /* {{{ proto int posix_getgid(void)
 497    Get the current group id (POSIX.1, 4.2.1) */
 498 PHP_FUNCTION(posix_getgid)
 499 {
 500         PHP_POSIX_RETURN_LONG_FUNC(getgid);
 501 }
 502 /* }}} */
 503 
 504 /* {{{ proto int posix_geteuid(void)
 505    Get the current effective user id (POSIX.1, 4.2.1) */
 506 PHP_FUNCTION(posix_geteuid)
 507 {
 508         PHP_POSIX_RETURN_LONG_FUNC(geteuid);
 509 }
 510 /* }}} */
 511 
 512 /* {{{ proto int posix_getegid(void)
 513    Get the current effective group id (POSIX.1, 4.2.1) */
 514 PHP_FUNCTION(posix_getegid)
 515 {
 516         PHP_POSIX_RETURN_LONG_FUNC(getegid);
 517 }
 518 /* }}} */
 519 
 520 /* {{{ proto bool posix_setuid(long uid)
 521    Set user id (POSIX.1, 4.2.2) */
 522 PHP_FUNCTION(posix_setuid)
 523 {
 524         PHP_POSIX_SINGLE_ARG_FUNC(setuid);
 525 }
 526 /* }}} */
 527 
 528 /* {{{ proto bool posix_setgid(int uid)
 529    Set group id (POSIX.1, 4.2.2) */
 530 PHP_FUNCTION(posix_setgid)
 531 {
 532         PHP_POSIX_SINGLE_ARG_FUNC(setgid);
 533 }
 534 /* }}} */
 535 
 536 /* {{{ proto bool posix_seteuid(long uid)
 537    Set effective user id */
 538 #ifdef HAVE_SETEUID
 539 PHP_FUNCTION(posix_seteuid)
 540 {
 541         PHP_POSIX_SINGLE_ARG_FUNC(seteuid);
 542 }
 543 #endif
 544 /* }}} */
 545 
 546 /* {{{ proto bool posix_setegid(long uid)
 547    Set effective group id */
 548 #ifdef HAVE_SETEGID
 549 PHP_FUNCTION(posix_setegid)
 550 {
 551         PHP_POSIX_SINGLE_ARG_FUNC(setegid);
 552 }
 553 #endif
 554 /* }}} */
 555 
 556 /* {{{ proto array posix_getgroups(void)
 557    Get supplementary group id's (POSIX.1, 4.2.3) */
 558 #ifdef HAVE_GETGROUPS
 559 PHP_FUNCTION(posix_getgroups)
 560 {
 561         gid_t  gidlist[NGROUPS_MAX];
 562         int    result;
 563         int    i;
 564 
 565         PHP_POSIX_NO_ARGS;
 566 
 567         if ((result = getgroups(NGROUPS_MAX, gidlist)) < 0) {
 568                 POSIX_G(last_error) = errno;
 569                 RETURN_FALSE;
 570         }
 571 
 572         array_init(return_value);
 573 
 574         for (i=0; i<result; i++) {
 575                 add_next_index_long(return_value, gidlist[i]);
 576         }
 577 }
 578 #endif
 579 /* }}} */
 580 
 581 /* {{{ proto string posix_getlogin(void)
 582    Get user name (POSIX.1, 4.2.4) */
 583 #ifdef HAVE_GETLOGIN
 584 PHP_FUNCTION(posix_getlogin)
 585 {
 586         char *p;
 587 
 588         PHP_POSIX_NO_ARGS;
 589 
 590         if (NULL == (p = getlogin())) {
 591                 POSIX_G(last_error) = errno;
 592                 RETURN_FALSE;
 593         }
 594 
 595         RETURN_STRING(p);
 596 }
 597 #endif
 598 /* }}} */
 599 
 600 /* {{{ proto int posix_getpgrp(void)
 601    Get current process group id (POSIX.1, 4.3.1) */
 602 PHP_FUNCTION(posix_getpgrp)
 603 {
 604         PHP_POSIX_RETURN_LONG_FUNC(getpgrp);
 605 }
 606 /* }}} */
 607 
 608 /* {{{ proto int posix_setsid(void)
 609    Create session and set process group id (POSIX.1, 4.3.2) */
 610 #ifdef HAVE_SETSID
 611 PHP_FUNCTION(posix_setsid)
 612 {
 613         PHP_POSIX_RETURN_LONG_FUNC(setsid);
 614 }
 615 #endif
 616 /* }}} */
 617 
 618 /* {{{ proto bool posix_setpgid(int pid, int pgid)
 619    Set process group id for job control (POSIX.1, 4.3.3) */
 620 PHP_FUNCTION(posix_setpgid)
 621 {
 622         zend_long pid, pgid;
 623 
 624         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &pid, &pgid) == FAILURE) {
 625                 RETURN_FALSE;
 626         }
 627 
 628         if (setpgid(pid, pgid) < 0) {
 629                 POSIX_G(last_error) = errno;
 630                 RETURN_FALSE;
 631         }
 632 
 633         RETURN_TRUE;
 634 }
 635 /* }}} */
 636 
 637 /* {{{ proto int posix_getpgid(void)
 638    Get the process group id of the specified process (This is not a POSIX function, but a SVR4ism, so we compile conditionally) */
 639 #ifdef HAVE_GETPGID
 640 PHP_FUNCTION(posix_getpgid)
 641 {
 642         zend_long val;
 643         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &val) == FAILURE) {
 644                 RETURN_FALSE;
 645         }
 646 
 647         if ((val = getpgid(val)) < 0) {
 648                 POSIX_G(last_error) = errno;
 649                 RETURN_FALSE;
 650         }
 651         RETURN_LONG(val);
 652 }
 653 #endif
 654 /* }}} */
 655 
 656 /* {{{ proto int posix_getsid(void)
 657    Get process group id of session leader (This is not a POSIX function, but a SVR4ism, so be compile conditionally) */
 658 #ifdef HAVE_GETSID
 659 PHP_FUNCTION(posix_getsid)
 660 {
 661         zend_long val;
 662         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &val) == FAILURE) {
 663                 RETURN_FALSE;
 664         }
 665 
 666         if ((val = getsid(val)) < 0) {
 667                 POSIX_G(last_error) = errno;
 668                 RETURN_FALSE;
 669         }
 670         RETURN_LONG(val);
 671 }
 672 #endif
 673 /* }}} */
 674 
 675 /* {{{ proto array posix_uname(void)
 676    Get system name (POSIX.1, 4.4.1) */
 677 PHP_FUNCTION(posix_uname)
 678 {
 679         struct utsname u;
 680 
 681         PHP_POSIX_NO_ARGS;
 682 
 683         if (uname(&u) < 0) {
 684                 POSIX_G(last_error) = errno;
 685                 RETURN_FALSE;
 686         }
 687 
 688         array_init(return_value);
 689 
 690         add_assoc_string(return_value, "sysname",  u.sysname);
 691         add_assoc_string(return_value, "nodename", u.nodename);
 692         add_assoc_string(return_value, "release",  u.release);
 693         add_assoc_string(return_value, "version",  u.version);
 694         add_assoc_string(return_value, "machine",  u.machine);
 695 
 696 #if defined(_GNU_SOURCE) && !defined(DARWIN) && defined(HAVE_UTSNAME_DOMAINNAME)
 697         add_assoc_string(return_value, "domainname", u.domainname);
 698 #endif
 699 }
 700 /* }}} */
 701 
 702 /* POSIX.1, 4.5.1 time() - Get System Time
 703                                                         already covered by PHP
 704  */
 705 
 706 /* {{{ proto array posix_times(void)
 707    Get process times (POSIX.1, 4.5.2) */
 708 PHP_FUNCTION(posix_times)
 709 {
 710         struct tms t;
 711         clock_t    ticks;
 712 
 713         PHP_POSIX_NO_ARGS;
 714 
 715         if ((ticks = times(&t)) == -1) {
 716                 POSIX_G(last_error) = errno;
 717                 RETURN_FALSE;
 718         }
 719 
 720         array_init(return_value);
 721 
 722         add_assoc_long(return_value, "ticks",   ticks);                 /* clock ticks */
 723         add_assoc_long(return_value, "utime",   t.tms_utime);   /* user time */
 724         add_assoc_long(return_value, "stime",   t.tms_stime);   /* system time */
 725         add_assoc_long(return_value, "cutime",  t.tms_cutime);  /* user time of children */
 726         add_assoc_long(return_value, "cstime",  t.tms_cstime);  /* system time of children */
 727 }
 728 /* }}} */
 729 
 730 /* POSIX.1, 4.6.1 getenv() - Environment Access
 731                                                         already covered by PHP
 732 */
 733 
 734 /* {{{ proto string posix_ctermid(void)
 735    Generate terminal path name (POSIX.1, 4.7.1) */
 736 #ifdef HAVE_CTERMID
 737 PHP_FUNCTION(posix_ctermid)
 738 {
 739         char  buffer[L_ctermid];
 740 
 741         PHP_POSIX_NO_ARGS;
 742 
 743         if (NULL == ctermid(buffer)) {
 744                 /* Found no documentation how the defined behaviour is when this
 745                  * function fails
 746                  */
 747                 POSIX_G(last_error) = errno;
 748                 RETURN_FALSE;
 749         }
 750 
 751         RETURN_STRING(buffer);
 752 }
 753 #endif
 754 /* }}} */
 755 
 756 /* Checks if the provides resource is a stream and if it provides a file descriptor */
 757 static int php_posix_stream_get_fd(zval *zfp, int *fd) /* {{{ */
 758 {
 759         php_stream *stream;
 760 
 761         php_stream_from_zval_no_verify(stream, zfp);
 762 
 763         if (stream == NULL) {
 764                 php_error_docref(NULL, E_WARNING, "expects argument 1 to be a valid stream resource");
 765                 return 0;
 766         }
 767         if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
 768                 php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)fd, 0);
 769         } else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
 770                 php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)fd, 0);
 771         } else {
 772                 php_error_docref(NULL, E_WARNING, "could not use stream of type '%s'",
 773                                 stream->ops->label);
 774                 return 0;
 775         }
 776         return 1;
 777 }
 778 /* }}} */
 779 
 780 /* {{{ proto string posix_ttyname(int fd)
 781    Determine terminal device name (POSIX.1, 4.7.2) */
 782 PHP_FUNCTION(posix_ttyname)
 783 {
 784         zval *z_fd;
 785         char *p;
 786         int fd;
 787 #if defined(ZTS) && defined(HAVE_TTYNAME_R) && defined(_SC_TTY_NAME_MAX)
 788         zend_long buflen;
 789 #endif
 790 
 791         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &z_fd) == FAILURE) {
 792                 RETURN_FALSE;
 793         }
 794 
 795         switch (Z_TYPE_P(z_fd)) {
 796                 case IS_RESOURCE:
 797                         if (!php_posix_stream_get_fd(z_fd, &fd)) {
 798                                 RETURN_FALSE;
 799                         }
 800                         break;
 801                 default:
 802                         convert_to_long_ex(z_fd);
 803                         fd = Z_LVAL_P(z_fd);
 804         }
 805 #if defined(ZTS) && defined(HAVE_TTYNAME_R) && defined(_SC_TTY_NAME_MAX)
 806         buflen = sysconf(_SC_TTY_NAME_MAX);
 807         if (buflen < 1) {
 808                 RETURN_FALSE;
 809         }
 810         p = emalloc(buflen);
 811 
 812         if (ttyname_r(fd, p, buflen)) {
 813                 POSIX_G(last_error) = errno;
 814                 efree(p);
 815                 RETURN_FALSE;
 816         }
 817         RETURN_STRING(p);
 818         efree(p);
 819 #else
 820         if (NULL == (p = ttyname(fd))) {
 821                 POSIX_G(last_error) = errno;
 822                 RETURN_FALSE;
 823         }
 824 #endif
 825         RETURN_STRING(p);
 826 }
 827 /* }}} */
 828 
 829 /* {{{ proto bool posix_isatty(int fd)
 830    Determine if filedesc is a tty (POSIX.1, 4.7.1) */
 831 PHP_FUNCTION(posix_isatty)
 832 {
 833         zval *z_fd;
 834         int fd;
 835 
 836         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &z_fd) == FAILURE) {
 837                 RETURN_FALSE;
 838         }
 839 
 840         switch (Z_TYPE_P(z_fd)) {
 841                 case IS_RESOURCE:
 842                         if (!php_posix_stream_get_fd(z_fd, &fd)) {
 843                                 RETURN_FALSE;
 844                         }
 845                         break;
 846                 default:
 847                         convert_to_long_ex(z_fd);
 848                         fd = Z_LVAL_P(z_fd);
 849         }
 850 
 851         if (isatty(fd)) {
 852                 RETURN_TRUE;
 853         } else {
 854                 RETURN_FALSE;
 855         }
 856 }
 857 /* }}} */
 858 
 859 /*
 860         POSIX.1, 4.8.1 sysconf() - TODO
 861         POSIX.1, 5.7.1 pathconf(), fpathconf() - TODO
 862 
 863         POSIX.1, 5.1.2 opendir(), readdir(), rewinddir(), closedir()
 864         POSIX.1, 5.2.1 chdir()
 865                                 already supported by PHP
 866  */
 867 
 868 /* {{{ proto string posix_getcwd(void)
 869    Get working directory pathname (POSIX.1, 5.2.2) */
 870 PHP_FUNCTION(posix_getcwd)
 871 {
 872         char  buffer[MAXPATHLEN];
 873         char *p;
 874 
 875         PHP_POSIX_NO_ARGS;
 876 
 877         p = VCWD_GETCWD(buffer, MAXPATHLEN);
 878         if (!p) {
 879                 POSIX_G(last_error) = errno;
 880                 RETURN_FALSE;
 881         }
 882 
 883         RETURN_STRING(buffer);
 884 }
 885 /* }}} */
 886 
 887 /*
 888         POSIX.1, 5.3.x open(), creat(), umask()
 889         POSIX.1, 5.4.1 link()
 890                 already supported by PHP.
 891  */
 892 
 893 /* {{{ proto bool posix_mkfifo(string pathname, int mode)
 894    Make a FIFO special file (POSIX.1, 5.4.2) */
 895 #ifdef HAVE_MKFIFO
 896 PHP_FUNCTION(posix_mkfifo)
 897 {
 898         char *path;
 899         size_t path_len;
 900         zend_long mode;
 901         int     result;
 902 
 903         if (zend_parse_parameters(ZEND_NUM_ARGS(), "pl", &path, &path_len, &mode) == FAILURE) {
 904                 RETURN_FALSE;
 905         }
 906 
 907         if (php_check_open_basedir_ex(path, 0)) {
 908                 RETURN_FALSE;
 909         }
 910 
 911         result = mkfifo(path, mode);
 912         if (result < 0) {
 913                 POSIX_G(last_error) = errno;
 914                 RETURN_FALSE;
 915         }
 916 
 917         RETURN_TRUE;
 918 }
 919 #endif
 920 /* }}} */
 921 
 922 /* {{{ proto bool posix_mknod(string pathname, int mode [, int major [, int minor]])
 923    Make a special or ordinary file (POSIX.1) */
 924 #ifdef HAVE_MKNOD
 925 PHP_FUNCTION(posix_mknod)
 926 {
 927         char *path;
 928         size_t path_len;
 929         zend_long mode;
 930         zend_long major = 0, minor = 0;
 931         int result;
 932         dev_t php_dev;
 933 
 934         php_dev = 0;
 935 
 936         if (zend_parse_parameters(ZEND_NUM_ARGS(), "pl|ll", &path, &path_len,
 937                         &mode, &major, &minor) == FAILURE) {
 938                 RETURN_FALSE;
 939         }
 940 
 941         if (php_check_open_basedir_ex(path, 0)) {
 942                 RETURN_FALSE;
 943         }
 944 
 945         if ((mode & S_IFCHR) || (mode & S_IFBLK)) {
 946                 if (ZEND_NUM_ARGS() == 2) {
 947                         php_error_docref(NULL, E_WARNING, "For S_IFCHR and S_IFBLK you need to pass a major device kernel identifier");
 948                         RETURN_FALSE;
 949                 }
 950                 if (major == 0) {
 951                         php_error_docref(NULL, E_WARNING,
 952                                 "Expects argument 3 to be non-zero for POSIX_S_IFCHR and POSIX_S_IFBLK");
 953                         RETURN_FALSE;
 954                 } else {
 955 #if defined(HAVE_MAKEDEV) || defined(makedev)
 956                         php_dev = makedev(major, minor);
 957 #else
 958                         php_error_docref(NULL, E_WARNING, "Cannot create a block or character device, creating a normal file instead");
 959 #endif
 960                 }
 961         }
 962 
 963         result = mknod(path, mode, php_dev);
 964         if (result < 0) {
 965                 POSIX_G(last_error) = errno;
 966                 RETURN_FALSE;
 967         }
 968 
 969         RETURN_TRUE;
 970 }
 971 #endif
 972 /* }}} */
 973 
 974 /* Takes a pointer to posix group and a pointer to an already initialized ZVAL
 975  * array container and fills the array with the posix group member data. */
 976 int php_posix_group_to_array(struct group *g, zval *array_group) /* {{{ */
 977 {
 978         zval array_members;
 979         int count;
 980 
 981         if (NULL == g)
 982                 return 0;
 983 
 984         if (array_group == NULL || Z_TYPE_P(array_group) != IS_ARRAY)
 985                 return 0;
 986 
 987         array_init(&array_members);
 988 
 989         add_assoc_string(array_group, "name", g->gr_name);
 990         add_assoc_string(array_group, "passwd", g->gr_passwd);
 991         for (count=0; g->gr_mem[count] != NULL; count++) {
 992                 add_next_index_string(&array_members, g->gr_mem[count]);
 993         }
 994         zend_hash_str_update(Z_ARRVAL_P(array_group), "members", sizeof("members")-1, &array_members);
 995         add_assoc_long(array_group, "gid", g->gr_gid);
 996         return 1;
 997 }
 998 /* }}} */
 999 
1000 /*
1001         POSIX.1, 5.5.1 unlink()
1002         POSIX.1, 5.5.2 rmdir()
1003         POSIX.1, 5.5.3 rename()
1004         POSIX.1, 5.6.x stat(), chmod(), utime() already supported by PHP.
1005 */
1006 
1007 /* {{{ proto bool posix_access(string file [, int mode])
1008    Determine accessibility of a file (POSIX.1 5.6.3) */
1009 PHP_FUNCTION(posix_access)
1010 {
1011         zend_long mode = 0;
1012         size_t filename_len, ret;
1013         char *filename, *path;
1014 
1015         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|l", &filename, &filename_len, &mode) == FAILURE) {
1016                 RETURN_FALSE;
1017         }
1018 
1019         path = expand_filepath(filename, NULL);
1020         if (!path) {
1021                 POSIX_G(last_error) = EIO;
1022                 RETURN_FALSE;
1023         }
1024 
1025         if (php_check_open_basedir_ex(path, 0)) {
1026                 efree(path);
1027                 POSIX_G(last_error) = EPERM;
1028                 RETURN_FALSE;
1029         }
1030 
1031         ret = access(path, mode);
1032         efree(path);
1033 
1034         if (ret) {
1035                 POSIX_G(last_error) = errno;
1036                 RETURN_FALSE;
1037         }
1038 
1039         RETURN_TRUE;
1040 }
1041 /* }}} */
1042 
1043 /*
1044         POSIX.1, 6.x most I/O functions already supported by PHP.
1045         POSIX.1, 7.x tty functions, TODO
1046         POSIX.1, 8.x interactions with other C language functions
1047         POSIX.1, 9.x system database access
1048 */
1049 
1050 /* {{{ proto array posix_getgrnam(string groupname)
1051    Group database access (POSIX.1, 9.2.1) */
1052 PHP_FUNCTION(posix_getgrnam)
1053 {
1054         char *name;
1055         struct group *g;
1056         size_t name_len;
1057 #if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
1058         struct group gbuf;
1059         long buflen;
1060         char *buf;
1061 #endif
1062 
1063         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
1064                 RETURN_FALSE;
1065         }
1066 
1067 #if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
1068         buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
1069         if (buflen < 1) {
1070                 RETURN_FALSE;
1071         }
1072         buf = emalloc(buflen);
1073         g = &gbuf;
1074 
1075         if (getgrnam_r(name, g, buf, buflen, &g) || g == NULL) {
1076                 POSIX_G(last_error) = errno;
1077                 efree(buf);
1078                 RETURN_FALSE;
1079         }
1080 #else
1081         if (NULL == (g = getgrnam(name))) {
1082                 POSIX_G(last_error) = errno;
1083                 RETURN_FALSE;
1084         }
1085 #endif
1086         array_init(return_value);
1087 
1088         if (!php_posix_group_to_array(g, return_value)) {
1089                 zval_dtor(return_value);
1090                 php_error_docref(NULL, E_WARNING, "unable to convert posix group to array");
1091                 RETVAL_FALSE;
1092         }
1093 #if defined(ZTS) && defined(HAVE_GETGRNAM_R) && defined(_SC_GETGR_R_SIZE_MAX)
1094         efree(buf);
1095 #endif
1096 }
1097 /* }}} */
1098 
1099 /* {{{ proto array posix_getgrgid(long gid)
1100    Group database access (POSIX.1, 9.2.1) */
1101 PHP_FUNCTION(posix_getgrgid)
1102 {
1103         zend_long gid;
1104 #if defined(ZTS) && defined(HAVE_GETGRGID_R) && defined(_SC_GETGR_R_SIZE_MAX)
1105         int ret;
1106         struct group _g;
1107         struct group *retgrptr = NULL;
1108         long grbuflen;
1109         char *grbuf;
1110 #endif
1111         struct group *g;
1112 
1113         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &gid) == FAILURE) {
1114                 RETURN_FALSE;
1115         }
1116 #if defined(ZTS) && defined(HAVE_GETGRGID_R) && defined(_SC_GETGR_R_SIZE_MAX)
1117 
1118         grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
1119         if (grbuflen < 1) {
1120                 RETURN_FALSE;
1121         }
1122 
1123         grbuf = emalloc(grbuflen);
1124 
1125         ret = getgrgid_r(gid, &_g, grbuf, grbuflen, &retgrptr);
1126         if (ret || retgrptr == NULL) {
1127                 POSIX_G(last_error) = ret;
1128                 efree(grbuf);
1129                 RETURN_FALSE;
1130         }
1131         g = &_g;
1132 #else
1133         if (NULL == (g = getgrgid(gid))) {
1134                 POSIX_G(last_error) = errno;
1135                 RETURN_FALSE;
1136         }
1137 #endif
1138         array_init(return_value);
1139 
1140         if (!php_posix_group_to_array(g, return_value)) {
1141                 zval_dtor(return_value);
1142                 php_error_docref(NULL, E_WARNING, "unable to convert posix group struct to array");
1143                 RETVAL_FALSE;
1144         }
1145 #if defined(ZTS) && defined(HAVE_GETGRGID_R) && defined(_SC_GETGR_R_SIZE_MAX)
1146         efree(grbuf);
1147 #endif
1148 }
1149 /* }}} */
1150 
1151 int php_posix_passwd_to_array(struct passwd *pw, zval *return_value) /* {{{ */
1152 {
1153         if (NULL == pw)
1154                 return 0;
1155         if (NULL == return_value || Z_TYPE_P(return_value) != IS_ARRAY)
1156                 return 0;
1157 
1158         add_assoc_string(return_value, "name",      pw->pw_name);
1159         add_assoc_string(return_value, "passwd",    pw->pw_passwd);
1160         add_assoc_long  (return_value, "uid",       pw->pw_uid);
1161         add_assoc_long  (return_value, "gid",           pw->pw_gid);
1162         add_assoc_string(return_value, "gecos",     pw->pw_gecos);
1163         add_assoc_string(return_value, "dir",       pw->pw_dir);
1164         add_assoc_string(return_value, "shell",     pw->pw_shell);
1165         return 1;
1166 }
1167 /* }}} */
1168 
1169 /* {{{ proto array posix_getpwnam(string groupname)
1170    User database access (POSIX.1, 9.2.2) */
1171 PHP_FUNCTION(posix_getpwnam)
1172 {
1173         struct passwd *pw;
1174         char *name;
1175         size_t name_len;
1176 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R)
1177         struct passwd pwbuf;
1178         long buflen;
1179         char *buf;
1180 #endif
1181 
1182         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
1183                 RETURN_FALSE;
1184         }
1185 
1186 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R)
1187         buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
1188         if (buflen < 1) {
1189                 RETURN_FALSE;
1190         }
1191         buf = emalloc(buflen);
1192         pw = &pwbuf;
1193 
1194         if (getpwnam_r(name, pw, buf, buflen, &pw) || pw == NULL) {
1195                 efree(buf);
1196                 POSIX_G(last_error) = errno;
1197                 RETURN_FALSE;
1198         }
1199 #else
1200         if (NULL == (pw = getpwnam(name))) {
1201                 POSIX_G(last_error) = errno;
1202                 RETURN_FALSE;
1203         }
1204 #endif
1205         array_init(return_value);
1206 
1207         if (!php_posix_passwd_to_array(pw, return_value)) {
1208                 zval_dtor(return_value);
1209                 php_error_docref(NULL, E_WARNING, "unable to convert posix passwd struct to array");
1210                 RETVAL_FALSE;
1211         }
1212 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R)
1213         efree(buf);
1214 #endif
1215 }
1216 /* }}} */
1217 
1218 /* {{{ proto array posix_getpwuid(long uid)
1219    User database access (POSIX.1, 9.2.2) */
1220 PHP_FUNCTION(posix_getpwuid)
1221 {
1222         zend_long uid;
1223 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWUID_R)
1224         struct passwd _pw;
1225         struct passwd *retpwptr = NULL;
1226         long pwbuflen;
1227         char *pwbuf;
1228         int ret;
1229 #endif
1230         struct passwd *pw;
1231 
1232         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &uid) == FAILURE) {
1233                 RETURN_FALSE;
1234         }
1235 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWUID_R)
1236         pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
1237         if (pwbuflen < 1) {
1238                 RETURN_FALSE;
1239         }
1240         pwbuf = emalloc(pwbuflen);
1241 
1242         ret = getpwuid_r(uid, &_pw, pwbuf, pwbuflen, &retpwptr);
1243         if (ret || retpwptr == NULL) {
1244                 POSIX_G(last_error) = ret;
1245                 efree(pwbuf);
1246                 RETURN_FALSE;
1247         }
1248         pw = &_pw;
1249 #else
1250         if (NULL == (pw = getpwuid(uid))) {
1251                 POSIX_G(last_error) = errno;
1252                 RETURN_FALSE;
1253         }
1254 #endif
1255         array_init(return_value);
1256 
1257         if (!php_posix_passwd_to_array(pw, return_value)) {
1258                 zval_dtor(return_value);
1259                 php_error_docref(NULL, E_WARNING, "unable to convert posix passwd struct to array");
1260                 RETVAL_FALSE;
1261         }
1262 #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWUID_R)
1263         efree(pwbuf);
1264 #endif
1265 }
1266 /* }}} */
1267 
1268 
1269 #ifdef HAVE_GETRLIMIT
1270 
1271 #define UNLIMITED_STRING "unlimited"
1272 
1273 /* {{{ posix_addlimit
1274  */
1275 static int posix_addlimit(int limit, char *name, zval *return_value) {
1276         int result;
1277         struct rlimit rl;
1278         char hard[80];
1279         char soft[80];
1280 
1281         snprintf(hard, 80, "hard %s", name);
1282         snprintf(soft, 80, "soft %s", name);
1283 
1284         result = getrlimit(limit, &rl);
1285         if (result < 0) {
1286                 POSIX_G(last_error) = errno;
1287                 return FAILURE;
1288         }
1289 
1290         if (rl.rlim_cur == RLIM_INFINITY) {
1291                 add_assoc_stringl(return_value, soft, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1);
1292         } else {
1293                 add_assoc_long(return_value, soft, rl.rlim_cur);
1294         }
1295 
1296         if (rl.rlim_max == RLIM_INFINITY) {
1297                 add_assoc_stringl(return_value, hard, UNLIMITED_STRING, sizeof(UNLIMITED_STRING)-1);
1298         } else {
1299                 add_assoc_long(return_value, hard, rl.rlim_max);
1300         }
1301 
1302         return SUCCESS;
1303 }
1304 /* }}} */
1305 
1306 /* {{{ limits[]
1307  */
1308 struct limitlist {
1309         int limit;
1310         char *name;
1311 } limits[] = {
1312 #ifdef RLIMIT_CORE
1313         { RLIMIT_CORE,  "core" },
1314 #endif
1315 
1316 #ifdef RLIMIT_DATA
1317         { RLIMIT_DATA,  "data" },
1318 #endif
1319 
1320 #ifdef RLIMIT_STACK
1321         { RLIMIT_STACK, "stack" },
1322 #endif
1323 
1324 #ifdef RLIMIT_VMEM
1325         { RLIMIT_VMEM, "virtualmem" },
1326 #endif
1327 
1328 #ifdef RLIMIT_AS
1329         { RLIMIT_AS, "totalmem" },
1330 #endif
1331 
1332 #ifdef RLIMIT_RSS
1333         { RLIMIT_RSS, "rss" },
1334 #endif
1335 
1336 #ifdef RLIMIT_NPROC
1337         { RLIMIT_NPROC, "maxproc" },
1338 #endif
1339 
1340 #ifdef RLIMIT_MEMLOCK
1341         { RLIMIT_MEMLOCK, "memlock" },
1342 #endif
1343 
1344 #ifdef RLIMIT_CPU
1345         { RLIMIT_CPU,   "cpu" },
1346 #endif
1347 
1348 #ifdef RLIMIT_FSIZE
1349         { RLIMIT_FSIZE, "filesize" },
1350 #endif
1351 
1352 #ifdef RLIMIT_NOFILE
1353         { RLIMIT_NOFILE, "openfiles" },
1354 #endif
1355 
1356 #ifdef RLIMIT_OFILE
1357         { RLIMIT_OFILE, "openfiles" },
1358 #endif
1359 
1360         { 0, NULL }
1361 };
1362 /* }}} */
1363 
1364 
1365 /* {{{ proto array posix_getrlimit(void)
1366    Get system resource consumption limits (This is not a POSIX function, but a BSDism and a SVR4ism. We compile conditionally) */
1367 PHP_FUNCTION(posix_getrlimit)
1368 {
1369         struct limitlist *l = NULL;
1370 
1371         PHP_POSIX_NO_ARGS;
1372 
1373         array_init(return_value);
1374 
1375         for (l=limits; l->name; l++) {
1376                 if (posix_addlimit(l->limit, l->name, return_value) == FAILURE) {
1377                         zval_dtor(return_value);
1378                         RETURN_FALSE;
1379                 }
1380         }
1381 }
1382 /* }}} */
1383 
1384 #endif /* HAVE_GETRLIMIT */
1385 
1386 #ifdef HAVE_SETRLIMIT
1387 /* {{{ proto bool posix_setrlimit(int resource, int softlimit, int hardlimit)
1388    Set system resource consumption limits (POSIX.1-2001) */
1389 PHP_FUNCTION(posix_setrlimit)
1390 {
1391         struct rlimit rl;
1392         zend_long res, cur, max;
1393 
1394         if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &res, &cur, &max) == FAILURE) {
1395                 RETURN_FALSE;
1396         }
1397 
1398         rl.rlim_cur = cur;
1399         rl.rlim_max = max;
1400 
1401         if (setrlimit(res, &rl) == -1) {
1402                 POSIX_G(last_error) = errno;
1403                 RETURN_FALSE;
1404         }
1405 
1406         RETURN_TRUE;
1407 }
1408 /* }}} */
1409 
1410 #endif /* HAVE_SETRLIMIT */
1411 
1412 
1413 /* {{{ proto int posix_get_last_error(void)
1414    Retrieve the error number set by the last posix function which failed. */
1415 PHP_FUNCTION(posix_get_last_error)
1416 {
1417         PHP_POSIX_NO_ARGS;
1418 
1419         RETURN_LONG(POSIX_G(last_error));
1420 }
1421 /* }}} */
1422 
1423 /* {{{ proto string posix_strerror(int errno)
1424    Retrieve the system error message associated with the given errno. */
1425 PHP_FUNCTION(posix_strerror)
1426 {
1427         zend_long error;
1428 
1429         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &error) == FAILURE) {
1430                 RETURN_FALSE;
1431         }
1432 
1433         RETURN_STRING(strerror(error));
1434 }
1435 /* }}} */
1436 
1437 #endif
1438 
1439 #ifdef HAVE_INITGROUPS
1440 /* {{{ proto bool posix_initgroups(string name, int base_group_id)
1441    Calculate the group access list for the user specified in name. */
1442 PHP_FUNCTION(posix_initgroups)
1443 {
1444         zend_long basegid;
1445         char *name;
1446         size_t name_len;
1447 
1448         if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &name, &name_len, &basegid) == FAILURE) {
1449                 RETURN_FALSE;
1450         }
1451 
1452         if (name_len == 0) {
1453                 RETURN_FALSE;
1454         }
1455 
1456         RETURN_BOOL(!initgroups((const char *)name, basegid));
1457 }
1458 /* }}} */
1459 #endif
1460 
1461 /*
1462  * Local variables:
1463  * tab-width: 4
1464  * c-basic-offset: 4
1465  * End:
1466  * vim600: sw=4 ts=4 fdm=marker
1467  * vim<600: sw=4 ts=4
1468  */

/* [<][>][^][v][top][bottom][index][help] */