1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <pwd.h>
#include <grp.h>
#include <signal.h>
#include <fcntl.h>
#include "uti/sge_rmon.h"
#include "uti/sge_unistd.h"
#include "uti/sge_stdio.h"
#include "uti/sge_uidgid.h"
#include "uti/sge_log.h"
#include "uti/sge_os.h"
#include "uti/msg_utilib.h"
#include "uti/sge_time.h"
#include "basis_types.h"
#include "msg_common.h"
#if defined(SOLARIS)
# include "sge_smf.h"
#endif
#ifdef NO_SGE_COMPILE_DEBUG
# undef SGE_EXIT
# define SGE_EXIT(x) exit(x)
#endif
static void addenv(char *, char *);
static void addenv(char *key, char *value)
{
char *str;
str = malloc(strlen(key) + strlen(value) + 2);
if (!str) {
return;
}
strcpy(str, key);
strcat(str, "=");
strcat(str, value);
putenv(str);
/* there is intentionally no sge_free(&str) */
return;
}
/*
* TODO: CLEANUP
*
* This function is DEPRECATED and should be removed with the next
* major release.
*
* This function can't be used in multi threaded environments because it
* might cause a deadlock in the executing qmaster thread.
* Use sge_peopen_r() instead.
*/
pid_t sge_peopen(const char *shell, int login_shell, const char *command,
const char *user, char **env, FILE **fp_in, FILE **fp_out,
FILE **fp_err, bool null_stderr)
{
pid_t pid;
int pipefds[3][2];
const char *could_not = MSG_SYSTEM_EXECBINSHFAILED;
const char *not_root = MSG_SYSTEM_NOROOTRIGHTSTOSWITCHUSER;
int i;
char arg0[256];
char err_str[256];
uid_t myuid;
DENTER(TOP_LAYER, "sge_peopen");
/* open pipes - close on failure */
for (i=0; i<3; i++) {
if (pipe(pipefds[i]) != 0) {
while (--i >= 0) {
close(pipefds[i][0]);
close(pipefds[i][1]);
}
ERROR((SGE_EVENT, MSG_SYSTEM_FAILOPENPIPES_SS,
command, strerror(errno)));
DEXIT;
return -1;
}
}
#if defined(SOLARIS)
pid = sge_smf_contract_fork(err_str, 256);
#else
pid = fork();
#endif
if (pid == 0) { /* child */
int keep_open[6];
keep_open[0] = 0;
keep_open[1] = 1;
keep_open[2] = 2;
keep_open[3] = pipefds[0][0];
keep_open[4] = pipefds[1][1];
keep_open[5] = pipefds[2][1];
sge_close_all_fds(keep_open, 6);
/* shall we redirect stderr to /dev/null? */
if (null_stderr) {
/* open /dev/null */
int fd = open("/dev/null", O_WRONLY);
if (fd == -1) {
sprintf(err_str, MSG_ERROROPENINGFILEFORWRITING_SS, "/dev/null", strerror(errno));
sprintf(err_str, "\n");
if (write(2, err_str, strlen(err_str)) != strlen(err_str)) {
/* nothing we can do here - we are anyway about to exit */
}
SGE_EXIT(NULL, 1);
}
/* set stderr to /dev/null */
close(2);
if (dup(fd) == -1) {
/* we have a serious problem here - nothing we can do but exit */
SGE_EXIT(NULL, 1);
}
/* we don't need the stderr the pipe - close it */
close(pipefds[2][1]);
} else {
/* redirect stderr to the pipe */
close(2);
if (dup(pipefds[2][1]) == -1) {
/* we have a serious problem here - nothing we can do but exit */
SGE_EXIT(NULL, 1);
}
}
/* redirect stdin and stdout to the pipes */
close(0);
close(1);
if (dup(pipefds[0][0]) == -1 ||
dup(pipefds[1][1]) == -1) {
/* we have a serious problem here - nothing we can do but exit */
SGE_EXIT(NULL, 1);
}
if (user) {
struct passwd *pw;
struct passwd pw_struct;
char *buffer;
int size;
size = get_pw_buffer_size();
buffer = sge_malloc(size);
if (!(pw=sge_getpwnam_r(user, &pw_struct, buffer, size))) {
sprintf(err_str, MSG_SYSTEM_NOUSERFOUND_SS , user, strerror(errno));
sprintf(err_str, "\n");
if (write(2, err_str, strlen(err_str)) != strlen(err_str)) {
/* nothing we can do here - we are anyway about to exit */
}
sge_free(&buffer);
SGE_EXIT(NULL, 1);
}
myuid = geteuid();
if (myuid != pw->pw_uid) {
/* Only change user if we differ from the wanted user */
if(myuid != SGE_SUPERUSER_UID) {
if (write(2, not_root, sizeof(not_root)) != sizeof(not_root)) {
/* nothing we can do here - we are anyway about to exit */
}
sge_free(&buffer);
SGE_EXIT(NULL, 1);
}
sprintf(err_str, "%s %d\n", pw->pw_name, (int)pw->pw_gid);
if (write(2, err_str, strlen(err_str)) != strlen(err_str)) {
/* TODO: required protocol step? If sending fails, exit? */
}
#if !(defined(WIN32) || defined(INTERIX)) /* initgroups not called */
if (initgroups(pw->pw_name, pw->pw_gid) != 0) {
sprintf(err_str, MSG_SYSTEM_INITGROUPSFORUSERFAILED_ISS,
user, strerror(errno));
sprintf(err_str, "\n");
if (write(2, err_str, strlen(err_str)) != strlen(err_str)) {
/* nothing we can do here - we are anyway about to exit */
}
sge_free(&buffer);
SGE_EXIT(NULL, 1);
}
#endif /* WIN32 */
if (sge_setuid(pw->pw_uid)) {
sprintf(err_str, MSG_SYSTEM_SWITCHTOUSERFAILED_SS , user,
strerror(errno));
sprintf(err_str, "\n");
if (write(2, err_str, strlen(err_str)) != strlen(err_str)) {
/* nothing we can do here - we are anyway about to exit */
}
sge_free(&buffer);
SGE_EXIT(NULL, 1);
}
}
addenv("HOME", pw->pw_dir);
addenv("SHELL", pw->pw_shell);
addenv("USER", pw->pw_name);
addenv("LOGNAME", pw->pw_name);
addenv("PATH", SGE_DEFAULT_PATH);
sge_free(&buffer);
}
if (login_shell) {
strcpy(arg0, "-");
} else {
strcpy(arg0, "");
}
strcat(arg0, shell);
if (env) {
for(; *env; env++) {
putenv(*env);
}
}
execlp(shell, arg0, "-c", command, (char *) NULL);
if (write(2, could_not, sizeof(could_not)) != sizeof(could_not)) {
/* nothing we can do here - we are anyway about to exit */
}
SGE_EXIT(NULL, 1);
} /* end child */
if (pid < 0) {
for (i=0; i<3; i++) {
close(pipefds[i][0]);
close(pipefds[i][1]);
}
#if defined(SOLARIS)
if (pid < -1) {
ERROR((SGE_EVENT, MSG_SMF_FORK_FAILED_SS, "sge_peopen()", err_str));
}
#endif
/* fork could have failed, report it */
ERROR((SGE_EVENT, MSG_SMF_FORK_FAILED_SS, "sge_peopen()", strerror(errno)));
DRETURN(-1);
}
/* close the childs ends of the pipes */
close(pipefds[0][0]);
close(pipefds[1][1]);
close(pipefds[2][1]);
/* return filehandles for stdin and stdout */
*fp_in = fdopen(pipefds[0][1], "a");
*fp_out = fdopen(pipefds[1][0], "r");
/* is stderr redirected to /dev/null? */
if (null_stderr) {
/* close the pipe and return NULL as filehandle */
close(pipefds[2][0]);
*fp_err = NULL;
} else {
/* return filehandle for stderr */
*fp_err = fdopen(pipefds[2][0], "r");
}
DRETURN(pid);
}
/****** uti/stdio/sge_peopen_r() ************************************************
* NAME
* sge_peopen_r() -- Advanced popen()
*
* SYNOPSIS
* pid_t sge_peopen_r(const char *shell, int login_shell,
* const char *command, const char *user,
* char **env, FILE **fp_in, FILE **fp_out,
* FILE **fp_err)
*
* FUNCTION
* Advanced popen() with additional parameters:
* - free shell usage
* - login shell if wanted
* - user under which to start (for root only)
* - stdin and stderr file pointers
* - wait for exactly the process we started
* File descriptors have to be closed with sge_peclose().
*
* This function is reentrant as long as env is not provided to
* this function. This means that the function can be used in
* multi thread processed as long as env is not used.
*
* INPUTS
* const char *shell - which shell to use
* int login_shell - make it a login shell?
* const char *command - name of the program
* const char *user - user under which to start (for root only)
* char **env - env variables to add to child
* FILE **fp_in - file input stream
* FILE **fp_out - file output stream
* FILE **fp_err - file error stream
*
* RESULT
* pid_t - process id
*
* NOTES
* MT-NOTE: sge_peopen_r() is MT safe
*
* DO NOT ADD ASYNC SIGNAL UNSAFE FUNCTIONS BETWEEN FORK AND EXEC
* DUE TO THE FACT THAT THIS FUNCTION WILL BE USED IN QMASTER
* (MULTITHREADED ENVIRONMENT) THIS MIGHT CAUSE A DEADLOCK
* IN A MASTER THREAD.
*
* SEE ALSO
* uti/stdio/sge_peclose()
******************************************************************************/
pid_t sge_peopen_r(const char *shell, int login_shell, const char *command,
const char *user, char **env, FILE **fp_in, FILE **fp_out,
FILE **fp_err, bool null_stderr)
{
pid_t pid;
int pipefds[3][2];
int i;
char arg0[256];
#if defined(SOLARIS)
char err_str[256];
#endif
struct passwd *pw = NULL;
uid_t myuid;
uid_t tuid;
DENTER(TOP_LAYER, "sge_peopen_r");
if (sge_has_admin_user()) {
errno = 0;
if (sge_switch2start_user()) {
ERROR((SGE_EVENT, MSG_SWITCH_USER_S, strerror(errno)));
DRETURN(-1);
}
}
myuid = geteuid();
tuid = myuid;
/*
* open pipes - close on failure
*/
for (i = 0; i < 3; i++) {
if (pipe(pipefds[i]) != 0) {
while (--i >= 0) {
close(pipefds[i][0]);
close(pipefds[i][1]);
}
ERROR((SGE_EVENT, MSG_SYSTEM_FAILOPENPIPES_SS, command, strerror(errno)));
if (sge_has_admin_user()) {
sge_switch2admin_user();
}
DRETURN(-1);
}
}
/*
* set arg0 for exec call correctly to that
* either a normal shell or a login shell will be started
*/
if (login_shell) {
strcpy(arg0, "-");
} else {
strcpy(arg0, "");
}
strcat(arg0, shell);
DPRINTF(("arg0 = %s\n", arg0));
DPRINTF(("arg1 = -c\n"));
DPRINTF(("arg2 = %s\n", command));
/*
* prepare the change of the user which might be done after fork()
* if a user name is provided.
*
* this has to be done before the fork() afterwards it might cause
* a deadlock of the child because getpwnam() is not async-thread safe.
*/
if (user) {
struct passwd pw_struct;
int size = get_pw_buffer_size();
char *buffer = sge_malloc(size);
/*
* get information about the target user
*/
if (buffer != NULL) {
pw = sge_getpwnam_r(user, &pw_struct, buffer, size);
if (pw == NULL) {
ERROR((SGE_EVENT, MSG_SYSTEM_NOUSERFOUND_SS, user, strerror(errno)));
sge_free(&buffer);
if (sge_has_admin_user()) {
sge_switch2admin_user();
}
DRETURN(-1);
}
} else {
ERROR((SGE_EVENT, SFNMAX, MSG_UTI_MEMPWNAM));
sge_free(&buffer);
if (sge_has_admin_user()) {
sge_switch2admin_user();
}
DRETURN(-1);
}
DPRINTF(("was able to resolve user\n"));
/*
* only prepare change of user if target user is different from current one
*/
if (myuid != pw->pw_uid) {
if (myuid != SGE_SUPERUSER_UID) {
DPRINTF(("only root is allowed to switch to a different user\n"));
ERROR((SGE_EVENT, SFNMAX, MSG_SYSTEM_NOROOTRIGHTSTOSWITCHUSER));
sge_free(&buffer);
DRETURN(-2);
}
DPRINTF(("Before initgroups\n"));
#if !(defined(WIN32) || defined(INTERIX)) /* initgroups not called */
if (initgroups(pw->pw_name, pw->pw_gid) != 0) {
ERROR((SGE_EVENT, MSG_SYSTEM_INITGROUPSFORUSERFAILED_ISS,
user, strerror(errno)));
sge_free(&buffer);
SGE_EXIT(NULL, 1);
}
DPRINTF(("Initgroups was successful\n"));
#endif /* WIN32 */
}
DPRINTF(("user = %s\n", user));
DPRINTF(("myuid = %d\n", (int)myuid));
if (pw != NULL) {
tuid = pw->pw_uid;
DPRINTF(("target uid = %d\n", (int)tuid));
}
sge_free(&buffer);
}
DPRINTF(("Now process will fork\n"));
#if defined(SOLARIS)
pid = sge_smf_contract_fork(err_str, 256);
#else
pid = fork();
#endif
/*
* in the child pid is 0
*/
if (pid == 0) {
/*
* close all fd's except that ones mentioned in keep_open
*/
int keep_open[6];
keep_open[0] = 0;
keep_open[1] = 1;
keep_open[2] = 2;
keep_open[3] = pipefds[0][0];
keep_open[4] = pipefds[1][1];
keep_open[5] = pipefds[2][1];
sge_close_all_fds(keep_open, 6);
/*
* shall we redirect stderr to /dev/null? Then
* - open "/dev/null"
* - set stderr to "dev/null"
* - close the stderr-pipe
* otherwise
* - redirect stderr to the pipe
*/
if (null_stderr) {
int fd = open("/dev/null", O_WRONLY);
if (fd != -1) {
close(2);
if (dup(fd) == -1) {
SGE_EXIT(NULL, 1);
}
close(pipefds[2][1]);
} else {
SGE_EXIT(NULL, 1);
}
} else {
close(2);
if (dup(pipefds[2][1]) == -1) {
SGE_EXIT(NULL, 1);
}
}
/*
* redirect stdin and stdout to the pipes
*/
close(0);
close(1);
if (dup(pipefds[0][0]) == -1 ||
dup(pipefds[1][1]) == -1) {
SGE_EXIT(NULL, 1);
}
if (pw != NULL) {
int lret = sge_setuid(tuid);
if (lret) {
SGE_EXIT(NULL, 1);
}
}
/*
* set the environment if we got one as argument
*/
if (env != NULL) {
if (pw != NULL) {
addenv("HOME", pw->pw_dir);
addenv("SHELL", pw->pw_shell);
addenv("USER", pw->pw_name);
addenv("LOGNAME", pw->pw_name);
}
addenv("PATH", SGE_DEFAULT_PATH);
for(; *env; env++) {
putenv(*env);
}
}
execlp(shell, arg0, "-c", command, (char *) NULL);
}
if (pid < 0) {
for (i=0; i<3; i++) {
close(pipefds[i][0]);
close(pipefds[i][1]);
}
#if defined(SOLARIS)
if (pid < -1) {
ERROR((SGE_EVENT, MSG_SMF_FORK_FAILED_SS, "sge_peopen()", err_str));
}
#endif
if (sge_has_admin_user()) {
sge_switch2admin_user();
}
DRETURN(-1);
}
/* close the childs ends of the pipes */
close(pipefds[0][0]);
close(pipefds[1][1]);
close(pipefds[2][1]);
/* return filehandles for stdin and stdout */
*fp_in = fdopen(pipefds[0][1], "a");
*fp_out = fdopen(pipefds[1][0], "r");
/* is stderr redirected to /dev/null? */
if (null_stderr) {
/* close the pipe and return NULL as filehandle */
close(pipefds[2][0]);
*fp_err = NULL;
} else {
/* return filehandle for stderr */
*fp_err = fdopen(pipefds[2][0], "r");
}
if (sge_has_admin_user()) {
sge_switch2admin_user();
}
DRETURN(pid);
}
/****** uti/stdio/sge_peclose() ***********************************************
* NAME
* sge_peclose() -- pclose() call which is suitable for sge_peopen()
*
* SYNOPSIS
* int sge_peclose(pid_t pid, FILE *fp_in, FILE *fp_out,
* FILE *fp_err, struct timeval *timeout)
*
* FUNCTION
* ???
*
* INPUTS
* pid_t pid - pid returned by peopen()
* FILE *fp_in
* FILE *fp_out
* FILE *fp_err
* struct timeval *timeout
*
* RESULT
* int - exit code of command or -1 in case of errors
*
* SEE ALSO
* uti/stdio/peopen()
*
* NOTES
* MT-NOTE: sge_peclose() is MT safe
******************************************************************************/
int sge_peclose(pid_t pid, FILE *fp_in, FILE *fp_out, FILE *fp_err,
struct timeval *timeout)
{
int i, status;
long timeout_us = timeout ? (timeout->tv_sec * 1000000L + timeout->tv_usec)
: 0;
DENTER(TOP_LAYER, "sge_peclose");
if (fp_in != NULL) {
FCLOSE(fp_in);
}
if (fp_out != NULL) {
FCLOSE(fp_out);
}
if (fp_err != NULL) {
FCLOSE(fp_err);
}
do {
errno = 0;
i = waitpid(pid, &status, timeout?WNOHANG:0);
if (i==-1) {
DEXIT;
return errno;
}
if (i==0) { /* not yet exited */
if (timeout_us <= 0) {
#ifdef WIN32 /* kill not called */
DPRINTF(("killing not yet implemented\n"));
timeout = NULL;
/* kill(pid, SIGKILL); */
#else
DPRINTF(("killing\n"));
timeout = NULL;
kill(pid, SIGKILL);
#endif /* WIN32 */
} else {
/* Spin less for longer timeouts; for timeout > 1s, sleep
for 0.1s, n the loop, otherwise 1ms. */
int sleep_for = (timeout_us > 1000000) ? 100000 : 1000;
DPRINTF(("%f seconds waiting for exit\n",
timeout->tv_sec + timeout->tv_usec / 1e6));
timeout_us -= sleep_for;
sge_usleep(sleep_for);
}
}
} while (i != pid);
if (status & 0xff) { /* terminated by signal */
DEXIT;
return -1;
}
DEXIT;
return (status&0xff00) >> 8; /* return exitcode */
FCLOSE_ERROR:
return -1;
}
void
print_option_syntax(FILE *fp, const char *option, const char *meaning)
{
if (!meaning)
fprintf(fp," %s\n", option);
else
fprintf(fp," %-40.40s %s\n", option, meaning);
}
bool sge_check_stdout_stream(FILE *file, int fd)
{
if (fileno(file) != fd) {
return false;
}
if(fprintf(file, "%s", "") < 0) {
return false;
}
return true;
}
|