Portál AbcLinuxu, 7. prosince 2025 22:52


Dotaz: SIGCLD, system, deamon

16.7.2004 13:15 honza
SIGCLD, system, deamon
Přečteno: 109×
Odpovědět | Admin
v minulosti (napr. suse 7.1) mi fungoval deamon program, ktery na zacatku ignoroval SIGCLD. V deamonu se provadelo volani.. ret = system("aaaa_exe"); ret pote obdrzel exit kod provadeneho programu. na novych systemech (suse 9.1) je nyni ret=-1 a errno=10. Kdyz SIGCLD ale neignoruji, tak se hromadi zobie's.

Nema nekdo poneti, jak je to treba programovat dneska?
Nástroje: Začni sledovat (0) ?Zašle upozornění na váš email při vložení nového komentáře.

Odpovědi

16.7.2004 14:43 Maude Lebowski
Rozbalit Rozbalit vše Re: SIGCLD, system, deamon
Odpovědět | | Sbalit | Link | Blokovat | Admin
ja tyhle veci neprogramuju pres system, ktery ma omezene pouzivani prave funkci na cekani, ale mam vlastni wraper: ( veci za include musi byt v < > zavorkach)
/*

  execute system utilities, run a specified binary with
  string arguments and return an output as a single string 

  $Id$

*/

#include unistd.h
#include stdio.h
#include stdlib.h
#include string.h
#include syslog.h
#include sys/types.h
#include sys/wait.h

#define TIMEOUT_SEC 1
#define TIMEOUT_MSEC 200000
/* how long we will waiting for childs... */

int exec_command(const char *command, char *const args[], char *output, int len)
{

  /* parameters:

  - command is a command (the same meaning as path in exec functions)
  - args[] are arugumets for execvp commands (null terminated array of strins)
  - output is output buffer previously allocated to lenght len
  - len is the max. lenght of output

  */

  pid_t f;
  int pipa[2];      /* pipa = the tap to get out beer from barel,in czech:-) */
  int i,j,n;
  fd_set rfds;
  struct timeval tv;

  if( pipe(pipa) < 0 ) {
    syslog(LOG_ERR,"pipe: %m: cannot create for %s\n",command);
    return(-1);
  }

  f = fork();
  if( f < 0 ) {
    syslog(LOG_ERR,"Fork failed. The %s move failed.\n",command);
    return(-1);
  }
  else if( f == 0 ) {
    close(1);
    dup(pipa[1]);
    close(pipa[0]);
    close(pipa[1]);
    fprintf(stderr,"%s %s %s %s\n",command,args[0],args[1],args[2]);
#ifdef DEBUG
    fprintf(stderr,"%s %s %s %s\n",command,args[0],args[1],args[2]);
#endif
    if( (j = execvp(command,args)) < 0 ) {
      syslog(LOG_ERR,"execlp %s: %m\n",command);
      /* a child should (!) write any output else a thread is stoped forever 
         (if block-read without select is used in main) or zombie is created */
      write(1,"",len-1);
    }
    exit(j);
  }
  else {

    /* use of the non-block input when no data are presented */ 
    n = 0;
    strcpy(output,"");
    FD_ZERO(&rfds);
    FD_SET(pipa[0], &rfds);
    tv.tv_sec = TIMEOUT_SEC;
    tv.tv_usec = TIMEOUT_MSEC;
    if( select(pipa[0]+1, &rfds, NULL, NULL, &tv) ) {
      read(pipa[0],output,len);
    }
    for( i = 0; output[i] != '\0' && i < len ; i++)
      if( output[i] == '\n' )
        output[i] = '\0';
#ifdef DEBUG
    printf("Child returned: >%s<\n",output);
#endif
    if( waitpid(f,&i,0) < 0 ) {
      syslog(LOG_ERR,"wait: %m\n");
      return(-1);
    }
    if( !WIFEXITED(i) ) {
      syslog(LOG_ERR,"Child exited with code %d\n",WEXITSTATUS(i)); 
      return(-1);
    }
  }

  return(i);
}
17.7.2004 12:41 honza
Rozbalit Rozbalit vše Re: SIGCLD, system, deamon
predem dekuji za zaslany kod. Provedu obsahly test v mem programu a v pripade chyb nebo nesrovnalosti se ozvu.

P.S.

'Maude Lebowski' je cool

'pipa = the tap to get out beer from barel..' to mi pripada ne tak cool - jestli to smim pouzit, tak to vyndam ?

Založit nové vláknoNahoru

Tiskni Sdílej: Linkuj Jaggni to Vybrali.sme.sk Google Del.icio.us Facebook

ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.