Esempio n. 1
0
/****** sgeobj/var/var_list_set_sharedlib_path() ******************************
*  NAME
*     var_list_set_sharedlib_path -- set shared lib path
*
*  SYNOPSIS
*     void var_list_set_sharedlib_path(lList **varl);
*
*  FUNCTION
*     Sets or replaces the shared lib path in the list of variables.
*     The SGE shared lib path is always set to the beginning of the
*     resulting shared lib path 
*     (security, see var_get_sharedlib_path_name())
*
*  INPUTS
*     lList **varl - list of nment variables
*
*  SEE ALSO
*     sgeobj/var/var_get_sharedlib_path_name()
*     sgeobj/var/var_list_set_string()
*     sgeobj/var/var_list_set_int()
*     sgeobj/var/var_list_set_sge_u32() 
******************************************************************************/
void var_list_set_sharedlib_path(lList **varl)
{
   char *sharedlib_path;
   char *sge_sharedlib_path;
   const char *sge_root = sge_get_root_dir(0, NULL, 0, 1);
   const char *sharedlib_path_name = var_get_sharedlib_path_name();
   lListElem *sharedlib_elem = NULL;

   DENTER(TOP_LAYER, "set_sharedlib_path");

   /* this is the SGE sharedlib path */
   sge_sharedlib_path = sge_malloc(strlen(sge_root) + 
                        strlen("/lib/") + strlen(sge_get_arch()) + 1);
   sprintf(sge_sharedlib_path, "%s/lib/%s", sge_root, sge_get_arch());

   /* if already in environment: extend by SGE sharedlib path, else set */
   sharedlib_elem = lGetElemStr(*varl, VA_variable, sharedlib_path_name);
   if(sharedlib_elem != NULL) {
      const char *old_value = lGetString(sharedlib_elem, VA_value);

      if(old_value && strlen(old_value) > 0) {
         DPRINTF(("sharedlib path %s already set:\n", sharedlib_path_name));
         
         sharedlib_path = sge_malloc(strlen(old_value) + 1 + 
                          strlen(sge_sharedlib_path) + 1);
         strcpy(sharedlib_path, sge_sharedlib_path);
         strcat(sharedlib_path, ":");
         strcat(sharedlib_path, old_value);
         lSetString(sharedlib_elem, VA_value, sharedlib_path);
         sge_free(&sharedlib_path);
      } else {
         DPRINTF(("overwriting empty sharedlib path %s\n", 
                  sharedlib_path_name));
         lSetString(sharedlib_elem, VA_value, sge_sharedlib_path);
      }
   } else {
      DPRINTF(("creating new sharedlib path %s\n", sharedlib_path_name));
      sharedlib_elem = lAddElemStr(varl, VA_variable, 
                                   sharedlib_path_name, VA_Type);
      lSetString(sharedlib_elem, VA_value, sge_sharedlib_path);
   }

   sge_free(&sge_sharedlib_path);
   DEXIT;
}
Esempio n. 2
0
/****** shepherd/qrsh/qlogin_starter() ****************************************
*
*  NAME
*     qlogin_starter -- short description
*
*  SYNOPSIS
*     #include "qlogin_starter.h"
*     int qlogin_starter(const char *cwd, char *daemon);
*
*  FUNCTION
*     The function is called from shepherd to start a protocol daemon
*     like telnetd, rshd or rlogind.
*     The mechanism used to call these daemons is that of inetd:
*        - a socket is created (server side, any free port is assigned
*          by the operating system)
*        - qlogin_starter waits for someone to connect to this socket
*        - the socket file handles are redirected to stdin, stdout
*          and stderr
*        - the daemon process is started
*     Additionally to the inetd mechanism, the port number and some
*     other information is sent to the qrsh process that initiated
*     (over qmaster, schedd, execd, shepherd) the qlogin_starter call.
*
*  INPUTS
*     cwd    - the current working directory (the active_jobs directory)
*     daemon - name and path of the daemon to start
*
*  RESULT
*     on success, the function will not return (it exec's)
*      4, if there is a problem with permissions
*      5, if a socket cannot be allocated
*      6, if a socket bind fails
*      7, if socket name (port) cannot be determined
*      8, if environment (to be passed to qrsh) cannot be read
*      9, if sending information to qrsh fails
*     10, if nobody connects to the socket within a one minute
*     11, if the acception of a connecting client fails
*     12, if the execution of the daemon fails
******************************************************************************/
int qlogin_starter(const char *cwd, char *daemon, char** env)
{
    int ret;
    int port;
    int fd;
    int maxfd;
    int sockfd;
    int on = 1;
    int sso = 1;
    int newsfd;
    fd_set fds;
    struct sockaddr_in serv_addr;
    struct timeval timeout;
    char buffer[2048];
    char *args[20]; /* JG: TODO: should be dynamically allocated */
    int argc = 0;
    const char *sge_root = NULL;
    const char *arch = NULL;

#if defined(IRIX65) || defined(INTERIX) || defined(DARWIN6) || defined(ALPHA5) || defined(HP1164)
    int length;
    int len;
#else
    socklen_t length;
    socklen_t len;
#endif

    len = sizeof(serv_addr);

    /* must be root because we must access /dev/something */
    if( setgid(SGE_SUPERUSER_GID) ||
            setuid(SGE_SUPERUSER_UID) ||
            setegid(SGE_SUPERUSER_GID) ||
            seteuid(SGE_SUPERUSER_UID)) {
        shepherd_trace("cannot change uid/gid\n");
        return 4;
    }
    shepherd_trace("uid = "uid_t_fmt", euid = "uid_t_fmt", gid = "gid_t_fmt
                   ", egid = "gid_t_fmt, getuid(), geteuid(), getgid(), getegid());

    /* socket stuff from here */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd == -1) {
        shepherd_trace("cannot open socket.");
        return 5;
    }
    shepherd_trace("using sfd %d", sockfd);

    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on));

    /* bind an address to any socket */
    memset((char *) &serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_port = 0;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    ret = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
    if (ret != 0) {
        shepherd_trace("cannot bind socket: %s", strerror(errno));
        shutdown(sockfd, 2);
        close(sockfd);
        return 6;
    }

    /* find out assigned port number and pass it to caller */
    length = sizeof(serv_addr);
    if (getsockname(sockfd,(struct sockaddr *) &serv_addr, &length) == -1) {
        shepherd_trace("getting socket name failed: %s", strerror(errno));
        shutdown(sockfd, 2);
        close(sockfd);
        return 7;
    }

    /* listen on socked - make connections be accepted */
    if (listen(sockfd, 1) != 0) {
        shepherd_trace("listen failed: %s", strerror(errno));
        shutdown(sockfd, 2);
        close(sockfd);
        return 8;
    }

    /* send necessary info to qrsh: port + utilbin directory + active job
     * directory
     */
    port = ntohs(serv_addr.sin_port);
    shepherd_trace("bound to port %d", port);

    sge_root = sge_get_root_dir(0, NULL, 0, 1);
    arch = sge_get_arch();

    if (sge_root == NULL || arch == NULL) {
        shepherd_trace("reading environment SGE_ROOT and ARC failed");
        shutdown(sockfd, 2);
        close(sockfd);
        return 9;
    }

    snprintf(buffer, 2048, "0:%d:%s/utilbin/%s:%s:%s",
             port, sge_root, arch, cwd, get_conf_val("host"));

    if (write_to_qrsh(buffer) != 0) {
        shepherd_trace("communication with qrsh failed");
        shutdown(sockfd, 2);
        close(sockfd);
        return 10;
    }

    /* wait for connection */
    shepherd_trace("waiting for connection.");
    /* use a reasonable timeout (60 seconds) to prevent hanging here forever */
    FD_ZERO(&fds);
    FD_SET(sockfd, &fds);
    timeout.tv_sec = 60;
    timeout.tv_usec = 0;
    if (select(sockfd+1, &fds, NULL, NULL, &timeout) < 1) {
        shepherd_trace("nobody connected to the socket");
        shutdown(sockfd, 2);
        close(sockfd);
        return 11;
    }

    /* accept connection */
    newsfd = accept(sockfd, (struct sockaddr *)(&serv_addr), &len);
    if (newsfd == -1) {
        shepherd_trace("error when accepting socket conection");
        shutdown(sockfd, 2);
        close(sockfd);
        return 12;
    }
    shepherd_trace("accepted connection on fd %d", newsfd);

    /* now we have a connection and do no longer need the "well known" port
     * free this resource.
     */
    shutdown(sockfd, 2);
    close(sockfd);

    /* don't close on exec */
    fcntl( newsfd, F_SETFD, 0 );

    /* speed up ;-) */
    setsockopt(newsfd, IPPROTO_TCP, TCP_NODELAY, (const char *) &sso, sizeof(int));

    /* use this fd as stdin,out,err */
    dup2( newsfd, 0 );
    dup2( newsfd, 1 );
    dup2( newsfd, 2 );

    /* close all the rest */
#ifndef WIN32NATIVE
    maxfd = sysconf(_SC_OPEN_MAX);
#else /* WIN32NATIVE */
    maxfd = FD_SETSIZE;
    /* detect maximal number of fds under NT/W2000 (env: Files)*/
#endif /* WIN32NATIVE */

    /* we do not use any FD_SET call it is ok to use _SC_OPEN_MAX */
    for (fd=3; fd<maxfd; fd++) {
        close(fd);
    }

    shepherd_trace("daemon to start: |%s|", daemon);

    /* split daemon commandline into single arguments */
    /* JG: TODO: might contain quoted arguments containing spaces
     *           make function to split or use an already existing one
     */
    args[argc++] = strtok(daemon, " ");
    while ((args[argc++] = strtok(NULL, " ")) != NULL);
#if 0
    {
        int i = 0;
        shepherd_trace("daemon commandline split to %d arguments", argc);
        while (args[i] != NULL) {
            shepherd_trace("daemon argv[%d] = |%s|", i, args[i]);
            i++;
        }
    }
#endif

    /* that it. */
    execve(args[0], args, env);

    /* oh oh, exec failed */
    /* no way to tell anyone, becuase all FDs are closed */
    /* last chance -> tell parent process */
    shutdown(newsfd, 2);
    close(newsfd);
    return 13;
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
   double avg[3];
   int loads;
   char *name = NULL;
#if defined(PLPA_LINUX) || defined(BINDING_SOLARIS)
   dstring msocket   = DSTRING_INIT;
   dstring mcore     = DSTRING_INIT;
   dstring mtopology = DSTRING_INIT;
#endif

#ifdef SGE_LOADMEM
   sge_mem_info_t mem_info;
#endif

#ifdef SGE_LOADCPU
	double total = 0.0;	
#endif

   int i, pos = 0, print_as_int = 0, precision = 0, core_binding = 0;
   char *m = "";

#ifndef WINDOWS
   DENTER_MAIN(TOP_LAYER, "loadcheck");
#endif

#ifdef __SGE_COMPILE_WITH_GETTEXT__   
   /* init language output for gettext() , it will use the right language */
   sge_init_language_func((gettext_func_type)        gettext,
                         (setlocale_func_type)      setlocale,
                         (bindtextdomain_func_type) bindtextdomain,
                         (textdomain_func_type)     textdomain);
   sge_init_language(NULL,NULL);   
#endif /* __SGE_COMPILE_WITH_GETTEXT__  */
   if (argc == 2 && !strcmp(argv[1], "-cb")) {
      core_binding = 1;
   } else {
      for (i = 1; i < argc;) {
         if (!strcmp(argv[i], "-int"))
            print_as_int = 1;
         else if (!strcmp(argv[i], "-loadval")) {
            if (i + 1 < argc)
               pos=i+1;
            else
               usage();
            i++;
         }
         else
            usage();
         i++;
      }
   }   
   
   if (core_binding) {
      check_core_binding();
#ifndef WINDOWS
      DEXIT;
#endif
      return 1;
   } else if (print_as_int) {
      m = "";
      precision = 0;
   }   
   else {
      m = "M";
      precision = 6;
   }   

   if ((pos && !strcmp("arch", argv[pos])) || !pos) {
      const char *arch = "";
#if defined(WINDOWS)
      arch = "win32-x86";
#else
      arch = sge_get_arch();
#endif 
      printf("arch            %s\n", arch);
   }
      
   if ((pos && !strcmp("num_proc", argv[pos])) || !pos) {
      int nprocs = 1;
#if defined(WINDOWS)
      SYSTEM_INFO system_info;
      char        buf[100];

      GetSystemInfo(&system_info);
      nprocs = system_info.dwNumberOfProcessors;
      sprintf(buf, "num_proc        %d", nprocs);
      fflush(stdout);
      write(1, (const void*)buf, (unsigned int)strlen(buf));
      write(1, (const void*)"\0x0a", (unsigned int)1);
#else
      nprocs = sge_nprocs();
      printf("num_proc        %d\n", nprocs);
#endif
   }

#if defined(PLPA_LINUX) || defined(BINDING_SOLARIS)
   fill_socket_core_topology(&msocket, &mcore, &mtopology);
   if ((pos && !strcmp("m_socket", argv[pos])) || !pos) {
      printf("m_socket        %s\n", sge_dstring_get_string(&msocket));
   }
   if ((pos && !strcmp("m_core", argv[pos])) || !pos) {
      printf("m_core          %s\n", sge_dstring_get_string(&mcore));
   }
   if ((pos && !strcmp("m_topology", argv[pos])) || !pos) {
      printf("m_topology      %s\n", sge_dstring_get_string(&mtopology));
   }   
#else 
   if ((pos && !strcmp("m_socket", argv[pos])) || !pos) {
      printf("m_socket        -\n");
   }
   if ((pos && !strcmp("m_core", argv[pos])) || !pos) {
      printf("m_core          -\n");
   }
   if ((pos && !strcmp("m_topology", argv[pos])) || !pos) {
      printf("m_topology      -\n");
   }   
#endif 

#if defined(WINDOWS)
   loads = 0;
   avg[0] = avg[1] = avg[2] = 0;
#else
	loads = sge_getloadavg(avg, 3);
#endif

   if (loads>0 && ((pos && !strcmp("load_short", argv[pos])) || !pos)) 
      printf("load_short      %.2f\n", avg[0]);
   if (loads>1 && ((pos && !strcmp("load_medium", argv[pos])) || !pos)) 
      printf("load_medium     %.2f\n", avg[1]);
   if (loads>2 && ((pos && !strcmp("load_long", argv[pos])) || !pos))
      printf("load_long       %.2f\n", avg[2]);
      
   if (pos)
      name = argv[pos];
   else
      name = NULL;

#ifdef SGE_LOADMEM
   /* memory load report */
   memset(&mem_info, 0, sizeof(sge_mem_info_t));
   if (sge_loadmem(&mem_info)) {
      fprintf(stderr, "%s\n", MSG_SYSTEM_RETMEMORYINDICESFAILED);
#ifndef WINDOWS
      DEXIT;
#endif
#if defined(PLPA_LINUX) || defined(BINDING_SOLARIS)
      sge_dstring_free(&mcore);
      sge_dstring_free(&msocket);
      sge_dstring_free(&mtopology);
#endif
      return 1;
   }

   print_mem_load(LOAD_ATTR_MEM_FREE, name, precision, mem_info.mem_free, m); 
   print_mem_load(LOAD_ATTR_SWAP_FREE, name, precision, mem_info.swap_free, m); 
   print_mem_load(LOAD_ATTR_VIRTUAL_FREE, name, precision, mem_info.mem_free  + mem_info.swap_free, m); 

   print_mem_load(LOAD_ATTR_MEM_TOTAL, name, precision, mem_info.mem_total, m); 
   print_mem_load(LOAD_ATTR_SWAP_TOTAL, name, precision, mem_info.swap_total, m); 
   print_mem_load(LOAD_ATTR_VIRTUAL_TOTAL, name, precision, mem_info.mem_total + mem_info.swap_total, m);

   print_mem_load(LOAD_ATTR_MEM_USED, name, precision, mem_info.mem_total - mem_info.mem_free, m); 
   print_mem_load(LOAD_ATTR_SWAP_USED, name, precision, mem_info.swap_total - mem_info.swap_free, m); 
   print_mem_load(LOAD_ATTR_VIRTUAL_USED, name, precision,(mem_info.mem_total + mem_info.swap_total) - 
                                          (mem_info.mem_free  + mem_info.swap_free), m); 
#  ifdef IRIX
   print_mem_load(LOAD_ATTR_SWAP_USED, name, precision, mem_info.swap_rsvd, m); 
#  endif
#endif /* SGE_LOADMEM */

#ifdef SGE_LOADCPU
   loads = sge_getcpuload(&total);
   sleep(1);
   loads = sge_getcpuload(&total);

   if (loads != -1) {
      print_mem_load("cpu", name,  1, total, "%");
   }
#endif /* SGE_LOADCPU */
#ifndef WINDOWS
   DEXIT;
#endif
#if defined(PLPA_LINUX) || defined(BINDING_SOLARIS)
   sge_dstring_free(&mcore);
   sge_dstring_free(&msocket);
   sge_dstring_free(&mtopology);
#endif
	return 0;
}