Exemple #1
0
/*>void RunJob(char *queueDir, int jobID, int verbose)
   ---------------------------------------------------
*//**
   \param[in]  queueDir   Queue directory
   \param[in]  jobID      Job number
   \param[in]  verbose    Verbosity level

   Actually runs a job

-  16.10.15  Original   By: ACRM
-  19.10.15  Now uses GetOwner()
*/
void RunJob(char *queueDir, int jobID, int verbose)
{
   char jobFile[MAXBUFF],
        pwd[MAXBUFF],
        job[MAXBUFF],
        exe[MAXBUFF],
        cmd[MAXBUFF];
   FILE *fp;
   

   sprintf(jobFile, "%s/%d", queueDir, jobID);

   if(verbose)
   {
      char msg[MAXBUFF];
      sprintf(msg, "Running job %d", jobID);
      Message(PROGNAME, MSG_INFO, msg);
   }

   if((fp=fopen(jobFile, "r"))!=NULL)
   {
      char *username;
      
      /* Find the owner of the job file                                 */
      username = GetOwner(queueDir, jobID);
      
      /* Get the working directory for running the job                  */
      if(!fgets(pwd, MAXBUFF, fp))
         CLOSE_AND_RETURN(fp, jobID);
      TERMINATE(pwd);

      /* Get the job itself                                             */
      if(!fgets(job, MAXBUFF, fp))
         CLOSE_AND_RETURN(fp, jobID);
      TERMINATE(job);

      fclose(fp);

      /* Run the job as the requested user                              */
      sprintf(cmd, "(cd %s; %s)", pwd, job);
      sprintf(exe, "su - %s -c \"%s\"", username, cmd);

      if(verbose >= 2)
      {
         char msg[MAXBUFF];
         sprintf(msg, "Command is: %s", cmd);         
         Message(PROGNAME, MSG_INFO, msg);
      }
      
      if(verbose >= 3)
      {
         char msg[MAXBUFF];
         sprintf(msg, "Expanded command is: %s", exe);         
         Message(PROGNAME, MSG_INFO, msg);
      }
      
      system(exe);

      /* Remove the job from the queue                                  */
      unlink(jobFile);
   }
}
Exemple #2
0
int dns_getaddr(const char *host, struct in_addr *addr, st_utime_t timeout)
{
  char name[MAXDNAME], **domain;
  const char *cp;
  int s, n, maxlen, dots;
  int trailing_dot, tried_as_is;
  st_netfd_t nfd;

  if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
    h_errno = NETDB_INTERNAL;
    return -1;
  }
  if (_res.options & RES_USEVC) {
    h_errno = NETDB_INTERNAL;
    errno = ENOSYS;
    return -1;
  }
  if (!host || *host == '\0') {
    h_errno = HOST_NOT_FOUND;
    return -1;
  }

  /* Create UDP socket */
  if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
    h_errno = NETDB_INTERNAL;
    return -1;
  }
  if ((nfd = st_netfd_open_socket(s)) == NULL) {
    h_errno = NETDB_INTERNAL;
    n = errno;
    close(s);
    errno = n;
    return -1;
  }

  maxlen = sizeof(name) - 1;
  n = 0;
  dots = 0;
  trailing_dot = 0;
  tried_as_is = 0;

  for (cp = host; *cp && n < maxlen; cp++) {
    dots += (*cp == '.');
    name[n++] = *cp;
  }
  if (name[n - 1] == '.')
    trailing_dot = 1;

  /*
   * If there are dots in the name already, let's just give it a try
   * 'as is'.  The threshold can be set with the "ndots" option.
   */
  if (dots >= _res.ndots) {
    if (query_domain(nfd, host, addr, timeout) == 0)
      CLOSE_AND_RETURN(0);
    if (h_errno == NETDB_INTERNAL && errno == EINTR)
      CLOSE_AND_RETURN(-1);
    tried_as_is = 1;
  }

  /*
   * We do at least one level of search if
   *     - there is no dot and RES_DEFNAME is set, or
   *     - there is at least one dot, there is no trailing dot,
   *       and RES_DNSRCH is set.
   */
  if ((!dots && (_res.options & RES_DEFNAMES)) ||
      (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
    name[n++] = '.';
    for (domain = _res.dnsrch; *domain; domain++) {
      strncpy(name + n, *domain, maxlen - n);
      if (query_domain(nfd, name, addr, timeout) == 0)
	CLOSE_AND_RETURN(0);
      if (h_errno == NETDB_INTERNAL && errno == EINTR)
	CLOSE_AND_RETURN(-1);
      if (!(_res.options & RES_DNSRCH))
	break;
    }
  }

  /*
   * If we have not already tried the name "as is", do that now.
   * note that we do this regardless of how many dots were in the
   * name or whether it ends with a dot.
   */
  if (!tried_as_is) {
    if (query_domain(nfd, host, addr, timeout) == 0)
      CLOSE_AND_RETURN(0);
  }

  CLOSE_AND_RETURN(-1);
}
Exemple #3
0
int dns_getaddr(const char *hosttest, struct in_addr *addr, st_utime_t timeout)
{
	//return 0;
//	host="www.baidu.com";
#if 0
char *host=malloc(128);
memset(host,0,128);
strcpy(host,hosttest);
#endif
char host[128];
memset(host,0,128);
strcpy(host,hosttest);
LOGD("hosttest [%s] host[%s]",hosttest,host);

  char name[MAXDNAME], **domain;
  const char *cp;
  int s, n, maxlen, dots;
  int trailing_dot, tried_as_is;
  st_netfd_t nfd;

  if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
    h_errno = NETDB_INTERNAL;
    return -1;
  }
  if (_res.options & RES_USEVC) {
    h_errno = NETDB_INTERNAL;
    errno = ENOSYS;
    return -1;
  }
  if (!host || *host == '\0') {
    h_errno = HOST_NOT_FOUND;
    return -1;
  }

  /* Create UDP socket */
  if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
    h_errno = NETDB_INTERNAL;
    return -1;
  }
  if ((nfd = st_netfd_open_socket(s)) == NULL) {
    h_errno = NETDB_INTERNAL;
    n = errno; //n用来记录错误号
    close(s);
    errno = n;
    return -1;
  }
  //可以走到这里
  maxlen = sizeof(name) - 1;
  LOGD("maxlen[%d]",maxlen);
  n = 0; //此时n是计数器
  dots = 0;
  trailing_dot = 0;
  tried_as_is = 0;

  for (cp = host; *cp && n < maxlen; cp++) {
  	//dots记录host中有几个.
    dots += (*cp == '.'); //走这里
    name[n++] = *cp; //看起来好像是复制host到name中
  }
  //n是后加,所以n-1,此时n-1对应的是最后一个字符
  if (name[n - 1] == '.')
    trailing_dot = 1;
  LOGD("========AAA===name[%s]=========",name);
  /*
   * If there are dots in the name already, let's just give it a try
   * 'as is'.  The threshold can be set with the "ndots" option.
   */



  if (dots >= _res.ndots) {
    if (query_domain(nfd, host, addr, timeout) == 0)
    {
		/*貌似我的query域名就是返回0,只好关闭并且退出了*/
	//	LOGD("query_domain return 0");
     /*不管我是否注释掉这句话都有段错误*/

		return 0;
#if 0
		CLOSE_AND_RETURN(0);
#else
     int ret=0;
     LOGD("===BB==========");
    // LOGD("%-40s %s", (char *)host, inet_ntoa(addr));
    // LOGD("DO RETURN ret[%d]",ret);

  	//LOGD(" errno[%d]",errno);
   // n = errno;
   // st_netfd_close(nfd);
  //  errno = n;
    return -1;
//  goto ZBTEST;
#endif
		//下面这句话不被执行,上面的return直接退出了这个函数
		slogi("query_domain WILL NOT HERE");
    }
    if (h_errno == NETDB_INTERNAL && errno == EINTR)
      CLOSE_AND_RETURN(-1);
    tried_as_is = 1;
  }

  /*
   * We do at least one level of search if
   *     - there is no dot and RES_DEFNAME is set, or
   *     - there is at least one dot, there is no trailing dot,
   *       and RES_DNSRCH is set.
   */
  if ((!dots && (_res.options & RES_DEFNAMES)) ||
      (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
    name[n++] = '.';
    for (domain = _res.dnsrch; *domain; domain++) {
      strncpy(name + n, *domain, maxlen - n);
      if (query_domain(nfd, name, addr, timeout) == 0)
	CLOSE_AND_RETURN(0);
      if (h_errno == NETDB_INTERNAL && errno == EINTR)
	CLOSE_AND_RETURN(-1);
      if (!(_res.options & RES_DNSRCH))
	break;
    }
  }

  /*
   * If we have not already tried the name "as is", do that now.
   * note that we do this regardless of how many dots were in the
   * name or whether it ends with a dot.
   */
  if (!tried_as_is) {
    if (query_domain(nfd, host, addr, timeout) == 0)
    {
		//LOGD("===HERE=query_domain");
		LOGD("----1----query_domain------");
     	CLOSE_AND_RETURN(0);
    }
  }
  CLOSE_AND_RETURN(-1);
//  ZBTEST:
	//  	LOGD("ZBTEST");
  //	return 0;
}