aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/process.c74
1 files changed, 25 insertions, 49 deletions
diff --git a/src/process.c b/src/process.c
index 47518b60..acdb5a35 100644
--- a/src/process.c
+++ b/src/process.c
@@ -22,48 +22,32 @@ extern regex_t **regList;
int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) {
/* Fork command 'cmd', returning pid, and optionally pointer
* to open file descriptor fd */
- int fdout, fderr, fdcmd, pid;
+ int fdin, fdout, fderr, fdcmd, pid;
int outpipe[2], errpipe[2], fdpipe[2];
int ourpid;
+ if ( (pipe(outpipe)==-1) || (pipe(errpipe)==-1) || (pipe(fdpipe)==-1) ) {
+ perror("pipe");
+ return -1;
+ }
+
if (outfd) {
- if (pipe(outpipe)==-1) {
- perror("pipe");
- return -1;
- }
fdout = outpipe[1];
*outfd = outpipe[0];
} else {
- if (!quiet)
- if ((fdout=dup(1))==-1) {
- perror("dup");
- return -1;
- }
+ if (!quiet)
+ fdout=dup(1);
}
if (errfd) {
- if (pipe(errpipe)==-1) {
- perror("pipe");
- return -1;
- }
fderr = errpipe[1];
*errfd = errpipe[0];
} else {
if (!quiet)
- if ((fderr=dup(2))==-1) {
- perror("dup");
- return -1;
- }
- }
- if (cmdfd) {
- if (pipe(fdpipe)==-1) {
- perror("pipe");
- return -1;
- }
- fdcmd = fdpipe[1];
- *cmdfd = fdpipe[0];
- } else {
- fdcmd = open("/dev/null",O_WRONLY);
+ fderr=dup(2);
}
+ fdcmd = fdpipe[1];
+ if (cmdfd)
+ *cmdfd = fdpipe[0];
ourpid = getpid();
if ((pid = fork())==-1) {
perror("fork");
@@ -74,6 +58,7 @@ int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) {
* fucks up and we segfault or something, we don't kill rc.sysinit. */
if ( (cmdfd&&!pid) || (pid &&!cmdfd)) {
/* parent */
+ close(fdin);
close(fdout);
close(fderr);
close(fdcmd);
@@ -127,7 +112,7 @@ int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) {
int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, int debug) {
struct pollfd *pfds;
- char *buf;
+ char *buf=malloc(8192*sizeof(char));
char *outbuf=NULL;
char *tmpstr=NULL;
int x,y,rc=-1;
@@ -136,18 +121,14 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet,
char **cmdargs=NULL;
char **tmpargs=NULL;
int cmdargc;
- char procpath[20];
+ char *procpath;
if (reexec) {
+ procpath=malloc(20*sizeof(char));
snprintf(procpath,20,"/proc/%d",pid);
}
-
- buf=malloc(8192*sizeof(char));
+
pfds = malloc(numfds*sizeof(struct pollfd));
- if (!buf || !pfds) {
- perror("malloc");
- exit(errno);
- }
for (x=0;x<numfds;x++) {
pfds[x].fd = fds[x];
pfds[x].events = POLLIN | POLLPRI;
@@ -174,7 +155,7 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet,
int bytesread = 0;
do {
- memset(buf,'\0',8192);
+ buf=calloc(8192,sizeof(char));
bytesread = read(pfds[y].fd,buf,8192);
if (bytesread==-1) {
perror("read");
@@ -206,16 +187,15 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet,
if (!ignore) {
if (!reexec) {
if (getenv("IN_INITLOG")) {
- char *buffer=alloca(8192);
+ char *buffer=calloc(8192,sizeof(char));
DDEBUG("sending =%s= to initlog parent\n",tmpstr);
- if (buffer) {
- snprintf(buffer,8192,"-n %s -s \"%s\"\n",
+ snprintf(buffer,8192,"-n %s -s \"%s\"\n",
cmdname,tmpstr);
- /* don't blow up if parent isn't there */
- signal(SIGPIPE,SIG_IGN);
- write(CMD_FD,buffer,strlen(buffer));
- signal(SIGPIPE,SIG_DFL);
- }
+ /* don't blow up if parent isn't there */
+ signal(SIGPIPE,SIG_IGN);
+ write(CMD_FD,buffer,strlen(buffer));
+ signal(SIGPIPE,SIG_DFL);
+ free(buffer);
} else {
logString(cmdname,tmpstr);
}
@@ -275,14 +255,10 @@ int runCommand(char *cmd, int reexec, int quiet, int debug) {
cmdname+=3;
if (!reexec) {
pid=forkCommand(args,&fds[0],&fds[1],NULL,quiet);
- if (pid==-1)
- exit(-1);
x=monitor(cmdname,pid,2,fds,reexec,quiet,debug);
} else {
setenv("IN_INITLOG","yes",1);
pid=forkCommand(args,NULL,NULL,&fds[0],quiet);
- if (pid==-1)
- exit(-1);
unsetenv("IN_INITLOG");
x=monitor(cmdname,pid,1,&fds[0],reexec,quiet,debug);
}
='#n234'>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
package common; # $Id$

use MDK::Common;
use diagnostics;
use strict;
BEGIN { eval { require Locale::gettext } } #- allow common.pm to be used in drakxtools-backend without perl-Locale-gettext

use log;
use run_program;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw($SECTORSIZE N P N_ check_for_xserver files_exist formatTime MB formatXiB get_parent_uid makedev mandrake_release mandrake_release_info removeXiBSuffix require_root_capability setVirtual set_alternative set_l10n_sort set_permissions to_utf8  translate unmakedev);

# perl_checker: RE-EXPORT-ALL
push @EXPORT, @MDK::Common::EXPORT;


$::prefix ||= ""; # no warning

#-#####################################################################################
#- Globals
#-#####################################################################################
our $SECTORSIZE  = 512;

#-#####################################################################################
#- Functions
#-#####################################################################################

sub P {
    my ($s_singular, $s_plural, $nb, @para) = @_; 
    sprintf(translate($s_singular, $s_plural, $nb), @para);
}

sub N {
    my ($s, @para) = @_; 
    sprintf(translate($s), @para);
}
sub N_ { $_[0] }


sub makedev { ($_[0] << 8) | $_[1] }
sub unmakedev { $_[0] >> 8, $_[0] & 0xff }

sub translate_real {
    my ($s, $o_plural, $o_nb) = @_;
    $s or return '';
    my $s2;
    foreach (@::textdomains, 'libDrakX') {
     if ($o_plural) {
         $s2 = Locale::gettext::dngettext($_, $s, $o_plural, $o_nb);
     } else {
         $s2 = Locale::gettext::dgettext($_, $s);
     }
	# when utf8 pragma is in use, Locale::gettext() returns an utf8 string not tagged as such:
	c::set_tagged_utf8($s2) if !utf8::is_utf8($s2) && utf8::is_utf8($s);
	return $s2 if $s ne $s2 && $s2 ne $o_plural;
    }
    # didn't lookup anything or locale is "C":
    $s2;
}

sub remove_translate_context {
    my ($s) = @_;
    #- translation with context, kde-like 
    $s =~ s/^_:.*\n//;
    $s;
}

sub translate {
    my $s = translate_real(@_);
    $::one_message_has_been_translated ||= join(':', (caller(1))[1,2]); #- see mygtk2.pm
    remove_translate_context($s);
}

sub from_utf8 {
    my ($s) = @_;
    Locale::gettext::iconv($s, "utf-8", undef); #- undef = locale charmap = nl_langinfo(CODESET)
}
sub to_utf8 { 
    my ($s) = @_;
    my $str = Locale::gettext::iconv($s, undef, "utf-8"); #- undef = locale charmap = nl_langinfo(CODESET)
    c::set_tagged_utf8($str);
    $str;
}

#- This is needed because text printed by Gtk2 will always be encoded
#- in UTF-8;
#- we first check if LC_ALL is defined, because if it is, changing
#- only LC_COLLATE will have no effect.
sub set_l10n_sort() {
    my $collation_locale = $ENV{LC_ALL};
    if (!$collation_locale) {
        $collation_locale = c::setlocale(c::LC_COLLATE());
        $collation_locale =~ /UTF-8/ or c::setlocale(c::LC_COLLATE(), "$collation_locale.UTF-8");
    }
}


sub setVirtual {
    my ($vt_number) = @_;
    my $vt = '';
    sysopen(my $C, "/dev/console", 2) or die "failed to open /dev/console: $!";
    ioctl($C, c::VT_GETSTATE(), $vt) &&
      ioctl($C, c::VT_ACTIVATE(), $vt_number) &&
	ioctl($C, c::VT_WAITACTIVE(), $vt_number) or die "setVirtual failed";
    unpack "S", $vt;
}

sub nonblock {
    my ($F) = @_;
    fcntl($F, c::F_SETFL(), fcntl($F, c::F_GETFL(), 0) | c::O_NONBLOCK()) or die "can not fcntl F_SETFL: $!";
}

#- return a size in sector
#- ie MB(1) is 2048 sectors, which is 1MB
sub MB {
    my ($nb_MB) = @_;
    $nb_MB * 2048;
}

sub removeXiBSuffix {
    local $_ = shift;

    /(\d+)\s*kB?$/i and return $1 * 1024;
    /(\d+)\s*MB?$/i and return $1 * 1024 * 1024;
    /(\d+)\s*GB?$/i and return $1 * 1024 * 1024 * 1024;
    /(\d+)\s*TB?$/i and return $1 * 1024 * 1024 * 1024 * 1024;
    $_;
}
sub formatXiB {
    my ($newnb, $o_newbase) = @_;
    my $newbase = $o_newbase || 1;
    my $sign = $newnb < 0 ? -1 : 1;
    $newnb = abs(int($newnb));
    my ($nb, $base);
    my $decr = sub { 
	($nb, $base) = ($newnb, $newbase);
	$base >= 1024 ? ($newbase = $base / 1024) : ($newnb = $nb / 1024);
    };
    my $suffix;
    foreach (N("B"), N("KB"), N("MB"), N("GB"), N("TB")) {
	$decr->(); 
	if ($newnb < 1 && $newnb * $newbase < 1) {
	    $suffix = $_;