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
|
// Copyright (C) 1999-2005 Open Source Telecom Corporation.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
//
// This exception applies only to the code released under the name GNU
// Common C++. If you copy code from other releases into a copy of GNU
// Common C++, as the General Public License permits, the exception does
// not apply to the code that you add in this way. To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for GNU Common C++, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.
//
#include <cc++/config.h>
#include <cc++/export.h>
#include <cc++/thread.h>
#include <cc++/process.h>
#include <cc++/strchar.h>
#include <cstdlib>
#include <cstdio>
#include <cerrno>
#include <csignal>
#ifdef MACOSX
#undef _POSIX_PRIORITY_SCHEDULING
#endif
#ifndef WIN32
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <pwd.h>
#include <grp.h>
#ifdef SIGTSTP
#include <sys/file.h>
#include <sys/ioctl.h>
#endif
#ifndef _PATH_TTY
#define _PATH_TTY "/dev/tty"
#endif
#endif
#ifdef WIN32
#include <process.h>
#include <stdio.h>
#endif
#ifdef CCXX_NAMESPACES
namespace ost {
#endif
static char *_pUser = NULL;
static char *_pHome = NULL;
bool Process::rtflag = false;
#ifdef WIN32
static SYSTEM_INFO sysinfo;
static LPSYSTEM_INFO lpSysInfo = NULL;
static void init_sysinfo(void)
{
if(!lpSysInfo) {
lpSysInfo = &sysinfo;
memset(&sysinfo, 0, sizeof(sysinfo));
GetSystemInfo(lpSysInfo);
}
}
const char *Process::getUser(void)
{
static char userid[65];
DWORD length = sizeof(userid);
if(GetUserName(userid, &length))
return userid;
return NULL;
}
size_t Process::getPageSize(void)
{
init_sysinfo();
return (size_t) lpSysInfo->dwPageSize;
}
int Process::spawn(const char *exename, const char **args, bool wait)
{
int mode = P_NOWAIT;
if(wait)
mode = P_WAIT;
return (int)::spawnvp(mode, (char *)exename, (char **)args);
}
int Process::join(int pid)
{
int status, result;
if(pid == -1)
return pid;
result = (int)cwait(&status, pid, WAIT_CHILD);
if(status & 0x0)
return -1;
return result;
}
bool Process::cancel(int pid, int sig)
{
HANDLE hPid = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
bool rtn = true;
if(!hPid)
return false;
switch(sig) {
case SIGABRT:
case SIGTERM:
if(!TerminateProcess(hPid, -1))
rtn = false;
case 0:
break;
default:
rtn = false;
}
CloseHandle(hPid);
return rtn;
}
void Process::setPriority(int pri)
{
DWORD pc = NORMAL_PRIORITY_CLASS;
DWORD pid = GetCurrentProcessId();
HANDLE hProcess = OpenProcess(PROCESS_DUP_HANDLE, TRUE, pid);
#ifdef BELOW_NORMAL_PRIORITY_CLASS
if(pri == -1)
pc = BELOW_NORMAL_PRIORITY_CLASS;
else if(pri == 1)
pc = ABOVE_NORMAL_PRIORITY_CLASS;
else
#endif
if(pri == 2)
pc = HIGH_PRIORITY_CLASS;
else if(pri > 2)
pc = REALTIME_PRIORITY_CLASS;
else if(pri < -1)
pc = IDLE_PRIORITY_CLASS;
SetPriorityClass(hProcess, pc);
CloseHandle(hProcess);
}
void Process::setScheduler(const char *cp)
{
if(!stricmp(cp, "fifo"))
setPriority(3);
else if(!stricmp(cp, "rr"))
setPriority(2);
else if(!stricmp(cp, "idle"))
setPriority(-2);
else
setPriority(0);
}
void Process::setRealtime(int pri)
{
setPriority(3);
}
bool Process::isScheduler(void)
{
return false;
}
#else
#ifndef WEXITSTATUS
#define WEXITSTATUS(status) ((unsigned)(status) >> 8)
#endif
#ifndef WIFEXITED
#define WIFEXITED(status) (((status) & 255) == 0)
#endif
#ifndef WTERMSIG
#define WTERMSIG(status) (((unsigned)(status)) & 0x7F)
#endif
#ifndef WIFSIGNALLED
#define WIFSIGNALLED(status) (((status) & 255) != 0)
#endif
#ifndef WCOREDUMP
#define WCOREDUMP(status) (((status) & 0x80) != 0)
#endif
static void lookup(void)
{
struct passwd *pw = NULL;
#ifdef HAVE_GETPWUID_R
struct passwd pwd;
char buffer[1024];
::getpwuid_r(geteuid(), &pwd, buffer, 1024, &pw);
#else
pw = ::getpwuid(geteuid());
#endif
if(_pHome)
delString(_pHome);
if(_pUser)
delString(_pUser);
_pUser = _pHome = NULL;
if(pw != NULL && pw->pw_dir != NULL)
_pHome = newString(pw->pw_dir);
if(pw != NULL && pw->pw_name != NULL)
_pUser = newString(pw->pw_name);
endpwent();
}
const char *Process::getUser(void)
{
if(!_pUser)
lookup();
return (const char *)_pUser;
}
const char *Process::getConfigDir(void)
{
#ifdef ETC_CONFDIR
return ETC_CONFDIR;
#else
return ETC_PREFIX;
#endif
}
const char *Process::getHomeDir(void)
{
if(!_pHome)
lookup();
return (const char *)_pHome;
}
#ifdef HAVE_GETPAGESIZE
size_t Process::getPageSize(void)
{
return (size_t)getpagesize();
}
#else
size_t Process::getPageSize(void)
{
return 1024;
}
#endif
bool Process::setUser(const char *id, bool grp)
{
struct passwd *pw = NULL;
#ifdef HAVE_GETPWNAM_R
struct passwd pwd;
char buffer[1024];
::getpwnam_r(id, &pwd, buffer, 1024, &pw);
#else
pw = ::getpwnam(id);
#endif
if(!pw)
return false;
if(grp)
if(setgid(pw->pw_gid))
return false;
if(setuid(pw->pw_uid))
return false;
lookup();
return true;
}
bool Process::setGroup(const char *id)
{
struct group *group = NULL;
#ifdef HAVE_GETGRNAM_R
struct group grp;
char buffer[2048];
::getgrnam_r(id, &grp, buffer, 1024, &group);
#else
group = ::getgrnam(id);
#endif
if(!group) {
endgrent();
return false;
}
#ifdef HAVE_SETEGID
setegid(group->gr_gid);
#endif
if(setgid(group->gr_gid)) {
endgrent();
return false;
}
endgrent();
return true;
}
bool Process::cancel(int pid, int sig)
{
if(!sig)
sig = SIGTERM;
if(pid < 1)
return false;
if(::kill(pid, sig))
return false;
return true;
}
int Process::join(int pid)
{
int status;
if(pid < 1)
return -1;
#ifdef HAVE_WAITPID
waitpid(pid, &status, 0);
#else
#ifdef HAVE_WAIT4
wait4(pid, &status, 0, NULL);
#else
int result;
while((result = wait(&status)) != pid && result != -1)
;
#endif
#endif
if(WIFEXITED(status))
return WEXITSTATUS(status);
else if(WIFSIGNALLED(status))
return -WTERMSIG(status);
else
return -1;
}
int Process::spawn(const char *exename, const char **args, bool wait)
{
int pid;
pid = vfork();
if(pid == -1)
return -1;
if(!pid) {
execvp((char *)exename, (char **)args);
_exit(-1);
}
if(!wait)
return pid;
return join(pid);
}
Process::Trap Process::setInterruptSignal(int signo, Trap func)
{
struct sigaction sig_act, old_act;
memset(&sig_act, 0, sizeof(sig_act));
sig_act.sa_handler = func;
sigemptyset(&sig_act.sa_mask);
if(signo != SIGALRM)
sigaddset(&sig_act.sa_mask, SIGALRM);
sig_act.sa_flags = 0;
#ifdef SA_INTERRUPT
sig_act.sa_flags |= SA_INTERRUPT;
#endif
if(sigaction(signo, &sig_act, &old_act) < 0)
return SIG_ERR;
return old_act.sa_handler;
}
Process::Trap Process::setPosixSignal(int signo, Trap func)
{
struct sigaction sig_act, old_act;
memset(&sig_act, 0, sizeof(sig_act));
sig_act.sa_handler = func;
sigemptyset(&sig_act.sa_mask);
sig_act.sa_flags = 0;
if(signo == SIGALRM) {
#ifdef SA_INTERRUPT
sig_act.sa_flags |= SA_INTERRUPT;
#endif
}
else {
sigaddset(&sig_act.sa_mask, SIGALRM);
#ifdef SA_RESTART
sig_act.sa_flags |= SA_RESTART;
#endif
}
if(sigaction(signo, &sig_act, &old_act) < 0)
return SIG_ERR;
return old_act.sa_handler;
}
void Process::detach(void)
{
attach("/dev/null");
}
void Process::attach(const char *dev)
{
int pid;
int fd;
if(getppid() == 1)
return;
::close(0);
::close(1);
::close(2);
#ifdef SIGTTOU
setPosixSignal(SIGTTOU, SIG_IGN);
#endif
#ifdef SIGTTIN
setPosixSignal(SIGTTIN, SIG_IGN);
#endif
#ifdef SIGTSTP
setPosixSignal(SIGTSTP, SIG_IGN);
#endif
if((pid = fork()) < 0)
THROW(pid);
else if(pid > 0)
exit(0);
#if defined(SIGTSTP) && defined(TIOCNOTTY)
if(setpgid(0, getpid()) == -1)
THROW(-1);
if((fd = open(_PATH_TTY, O_RDWR)) >= 0) {
ioctl(fd, TIOCNOTTY, NULL);
close(fd);
}
#else
#ifdef HAVE_SETPGRP
if(setpgrp() == -1)
THROW(-1);
#else
if(setpgid(0, getpid()) == -1)
THROW(-1);
#endif
setPosixSignal(SIGHUP, SIG_IGN);
if((pid = fork()) < 0)
THROW(-1);
else if(pid > 0)
exit(0);
#endif
if(dev && *dev) {
::open(dev, O_RDWR);
::open(dev, O_RDWR);
::open(dev, O_RDWR);
}
}
void Process::setScheduler(const char *pol)
{
#ifdef _POSIX_PRIORITY_SCHEDULING
struct sched_param p;
int policy;
sched_getparam(0, &p);
if(pol) {
#if defined(SCHED_TS)
policy = SCHED_TS;
#elif defined(SCHED_OTHER)
policy = SCHED_OTHER;
#else
policy = 0;
#endif
#ifdef SCHED_RR
if(!stricmp(pol, "rr"))
policy = SCHED_RR;
#endif
#if !defined(SCHED_RR) && defined(SCHED_FIFO)
if(!stricmp(pol, "rr"))
policy = SCHED_FIFO;
#endif
#ifdef SCHED_FIFO
if(!stricmp(pol, "fifo")) {
rtflag = true;
policy = SCHED_FIFO;
}
#endif
#ifdef SCHED_TS
if(!stricmp(pol, "ts"))
policy = SCHED_TS;
#endif
#ifdef SCHED_OTHER
if(!stricmp(pol, "other"))
policy = SCHED_OTHER;
#endif
}
else
policy = sched_getscheduler(0);
int min = sched_get_priority_min(policy);
int max = sched_get_priority_max(policy);
if(p.sched_priority < min)
p.sched_priority = min;
else if(p.sched_priority > max)
p.sched_priority = max;
sched_setscheduler(0, policy, &p);
#endif
}
void Process::setPriority(int pri)
{
#ifdef _POSIX_PRIORITY_SCHEDULING
struct sched_param p;
int policy = sched_getscheduler(0);
int min = sched_get_priority_min(policy);
int max = sched_get_priority_max(policy);
sched_getparam(0, &p);
if(pri < min)
pri = min;
if(pri > max)
pri = max;
p.sched_priority = pri;
sched_setparam(0, &p);
#else
if(pri < -20)
pri = -20;
if(pri > 20)
pri = 20;
nice(-pri);
#endif
}
bool Process::isScheduler(void)
{
#ifdef _POSIX_PRIORITY_SCHEDULING
return true;
#else
return false;
#endif
}
void Process::setRealtime(int pri)
{
if(pri < 1)
pri = 1;
setScheduler("rr");
setPriority(pri);
}
#endif // not win32
#ifdef _OSF_SOURCE
#undef HAVE_SETENV
#endif
void Process::setEnv(const char *name, const char *value, bool overwrite)
{
#ifdef HAVE_SETENV
::setenv(name, value, (int)overwrite);
#else
char strbuf[256];
snprintf(strbuf, sizeof(strbuf), "%s=%s", name, value);
if(!overwrite)
if(getenv(strbuf))
return;
::putenv(strdup(strbuf));
#endif
}
const char *Process::getEnv(const char *name)
{
return ::getenv(name);
}
#if defined(HAVE_MLOCKALL) && defined(MCL_FUTURE)
#include <sys/mman.h>
bool Process::lock(bool future)
{
int rc;
if(future)
rc = mlockall(MCL_CURRENT | MCL_FUTURE);
else
rc = mlockall(MCL_CURRENT);
if(rc)
return false;
return true;
}
void Process::unlock(void)
{
munlockall();
}
#else
bool Process::lock(bool future)
{
return false;
}
void Process::unlock(void)
{
}
#endif
#ifdef CCXX_NAMESPACES
}
#endif
/** EMACS **
* Local variables:
* mode: c++
* c-basic-offset: 8
* End:
*/
|