Blame src/unix/aix-common.c

Packit b5b901
/* Copyright libuv project contributors. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#include "uv.h"
Packit b5b901
#include "internal.h"
Packit b5b901
Packit b5b901
#include <stdio.h>
Packit b5b901
#include <stdint.h>
Packit b5b901
#include <stdlib.h>
Packit b5b901
#include <string.h>
Packit b5b901
#include <assert.h>
Packit b5b901
#include <errno.h>
Packit b5b901
Packit b5b901
#include <sys/types.h>
Packit b5b901
#include <sys/socket.h>
Packit b5b901
#include <sys/ioctl.h>
Packit b5b901
#include <net/if.h>
Packit b5b901
#include <netinet/in.h>
Packit Service e08953
#include <netinet/in6_var.h>
Packit b5b901
#include <arpa/inet.h>
Packit b5b901
Packit b5b901
#include <sys/time.h>
Packit b5b901
#include <unistd.h>
Packit b5b901
#include <fcntl.h>
Packit b5b901
#include <utmp.h>
Packit b5b901
#include <libgen.h>
Packit b5b901
Packit b5b901
#include <sys/protosw.h>
Packit b5b901
#include <procinfo.h>
Packit b5b901
#include <sys/proc.h>
Packit b5b901
#include <sys/procfs.h>
Packit b5b901
Packit b5b901
#include <sys/poll.h>
Packit b5b901
Packit b5b901
#include <sys/pollset.h>
Packit b5b901
#include <ctype.h>
Packit b5b901
Packit b5b901
#include <sys/mntctl.h>
Packit b5b901
#include <sys/vmount.h>
Packit b5b901
#include <limits.h>
Packit b5b901
#include <strings.h>
Packit b5b901
#include <sys/vnode.h>
Packit b5b901
Packit b5b901
uint64_t uv__hrtime(uv_clocktype_t type) {
Packit b5b901
  uint64_t G = 1000000000;
Packit b5b901
  timebasestruct_t t;
Packit b5b901
  read_wall_time(&t, TIMEBASE_SZ);
Packit b5b901
  time_base_to_time(&t, TIMEBASE_SZ);
Packit b5b901
  return (uint64_t) t.tb_high * G + t.tb_low;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
/*
Packit b5b901
 * We could use a static buffer for the path manipulations that we need outside
Packit b5b901
 * of the function, but this function could be called by multiple consumers and
Packit b5b901
 * we don't want to potentially create a race condition in the use of snprintf.
Packit b5b901
 * There is no direct way of getting the exe path in AIX - either through /procfs
Packit b5b901
 * or through some libc APIs. The below approach is to parse the argv[0]'s pattern
Packit b5b901
 * and use it in conjunction with PATH environment variable to craft one.
Packit b5b901
 */
Packit b5b901
int uv_exepath(char* buffer, size_t* size) {
Packit b5b901
  int res;
Packit b5b901
  char args[PATH_MAX];
Packit b5b901
  char abspath[PATH_MAX];
Packit b5b901
  size_t abspath_size;
Packit b5b901
  struct procsinfo pi;
Packit b5b901
Packit b5b901
  if (buffer == NULL || size == NULL || *size == 0)
Packit b5b901
    return UV_EINVAL;
Packit b5b901
Packit b5b901
  pi.pi_pid = getpid();
Packit b5b901
  res = getargs(&pi, sizeof(pi), args, sizeof(args));
Packit b5b901
  if (res < 0)
Packit b5b901
    return UV_EINVAL;
Packit b5b901
Packit b5b901
  /*
Packit b5b901
   * Possibilities for args:
Packit b5b901
   * i) an absolute path such as: /home/user/myprojects/nodejs/node
Packit b5b901
   * ii) a relative path such as: ./node or ../myprojects/nodejs/node
Packit b5b901
   * iii) a bare filename such as "node", after exporting PATH variable
Packit b5b901
   *     to its location.
Packit b5b901
   */
Packit b5b901
Packit b5b901
  /* Case i) and ii) absolute or relative paths */
Packit b5b901
  if (strchr(args, '/') != NULL) {
Packit b5b901
    if (realpath(args, abspath) != abspath)
Packit b5b901
      return UV__ERR(errno);
Packit b5b901
Packit b5b901
    abspath_size = strlen(abspath);
Packit b5b901
Packit b5b901
    *size -= 1;
Packit b5b901
    if (*size > abspath_size)
Packit b5b901
      *size = abspath_size;
Packit b5b901
Packit b5b901
    memcpy(buffer, abspath, *size);
Packit b5b901
    buffer[*size] = '\0';
Packit b5b901
Packit b5b901
    return 0;
Packit b5b901
  } else {
Packit b5b901
    /* Case iii). Search PATH environment variable */
Packit b5b901
    char trypath[PATH_MAX];
Packit b5b901
    char *clonedpath = NULL;
Packit b5b901
    char *token = NULL;
Packit b5b901
    char *path = getenv("PATH");
Packit b5b901
Packit b5b901
    if (path == NULL)
Packit b5b901
      return UV_EINVAL;
Packit b5b901
Packit b5b901
    clonedpath = uv__strdup(path);
Packit b5b901
    if (clonedpath == NULL)
Packit b5b901
      return UV_ENOMEM;
Packit b5b901
Packit b5b901
    token = strtok(clonedpath, ":");
Packit b5b901
    while (token != NULL) {
Packit b5b901
      snprintf(trypath, sizeof(trypath) - 1, "%s/%s", token, args);
Packit b5b901
      if (realpath(trypath, abspath) == abspath) {
Packit b5b901
        /* Check the match is executable */
Packit b5b901
        if (access(abspath, X_OK) == 0) {
Packit b5b901
          abspath_size = strlen(abspath);
Packit b5b901
Packit b5b901
          *size -= 1;
Packit b5b901
          if (*size > abspath_size)
Packit b5b901
            *size = abspath_size;
Packit b5b901
Packit b5b901
          memcpy(buffer, abspath, *size);
Packit b5b901
          buffer[*size] = '\0';
Packit b5b901
Packit b5b901
          uv__free(clonedpath);
Packit b5b901
          return 0;
Packit b5b901
        }
Packit b5b901
      }
Packit b5b901
      token = strtok(NULL, ":");
Packit b5b901
    }
Packit b5b901
    uv__free(clonedpath);
Packit b5b901
Packit b5b901
    /* Out of tokens (path entries), and no match found */
Packit b5b901
    return UV_EINVAL;
Packit b5b901
  }
Packit b5b901
}