Print this page
3946 ::gcore
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libproc/common/Pexecname.c
          +++ new/usr/src/lib/libproc/common/Pexecname.c
↓ open down ↓ 14 lines elided ↑ open up ↑
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
       25 +/*
       26 + * Copyright (c) 2013 by Delphix. All rights reserved.
       27 + */
  25   28  
  26   29  #define __EXTENSIONS__
  27   30  #include <string.h>
  28   31  #undef  __EXTENSIONS__
  29   32  
  30   33  #include <libgen.h>
  31   34  #include <limits.h>
  32   35  #include <stdio.h>
  33   36  #include <errno.h>
  34   37  #include <unistd.h>
↓ open down ↓ 194 lines elided ↑ open up ↑
 229  232          return (NULL);
 230  233  
 231  234  found:
 232  235          if ((P->execname = strdup(buf)) == NULL)
 233  236                  dprintf("failed to malloc; executable name is \"%s\"", buf);
 234  237  
 235  238          return (P->execname);
 236  239  }
 237  240  
 238  241  /*
 239      - * Callback function for Pfindexec().  We return a match if we can stat the
 240      - * suggested pathname and confirm its device and inode number match our
 241      - * previous information about the /proc/<pid>/object/a.out file.
      242 + * Return the full pathname for the executable file.
 242  243   */
 243      -static int
 244      -stat_exec(const char *path, struct stat64 *stp)
 245      -{
 246      -        struct stat64 st;
 247      -
 248      -        return (stat64(path, &st) == 0 && S_ISREG(st.st_mode) &&
 249      -            stp->st_dev == st.st_dev && stp->st_ino == st.st_ino);
 250      -}
 251      -
 252      -/*
 253      - * Return the full pathname for the executable file.  If the process handle is
 254      - * a core file, we've already tried our best to get the executable name.
 255      - * Otherwise, we make an attempt using Pfindexec().
 256      - */
 257  244  char *
 258  245  Pexecname(struct ps_prochandle *P, char *buf, size_t buflen)
 259  246  {
 260  247          if (P->execname != NULL) {
 261  248                  (void) strncpy(buf, P->execname, buflen);
 262  249                  return (buf);
 263  250          }
 264  251  
 265      -        if (P->state != PS_DEAD && P->state != PS_IDLE) {
 266      -                char exec_name[PATH_MAX];
 267      -                char cwd[PATH_MAX];
 268      -                char proc_cwd[64];
 269      -                struct stat64 st;
 270      -                int ret;
 271      -
 272      -                /*
 273      -                 * Try to get the path information first.
 274      -                 */
 275      -                (void) snprintf(exec_name, sizeof (exec_name),
 276      -                    "%s/%d/path/a.out", procfs_path, (int)P->pid);
 277      -                if ((ret = readlink(exec_name, buf, buflen - 1)) > 0) {
 278      -                        buf[ret] = '\0';
 279      -                        (void) Pfindobj(P, buf, buf, buflen);
 280      -                        return (buf);
 281      -                }
 282      -
 283      -                /*
 284      -                 * Stat the executable file so we can compare Pfindexec's
 285      -                 * suggestions to the actual device and inode number.
 286      -                 */
 287      -                (void) snprintf(exec_name, sizeof (exec_name),
 288      -                    "%s/%d/object/a.out", procfs_path, (int)P->pid);
 289      -
 290      -                if (stat64(exec_name, &st) != 0 || !S_ISREG(st.st_mode))
 291      -                        return (NULL);
 292      -
 293      -                /*
 294      -                 * Attempt to figure out the current working directory of the
 295      -                 * target process.  This only works if the target process has
 296      -                 * not changed its current directory since it was exec'd.
 297      -                 */
 298      -                (void) snprintf(proc_cwd, sizeof (proc_cwd),
 299      -                    "%s/%d/path/cwd", procfs_path, (int)P->pid);
 300      -
 301      -                if ((ret = readlink(proc_cwd, cwd, PATH_MAX - 1)) > 0)
 302      -                        cwd[ret] = '\0';
 303      -
 304      -                (void) Pfindexec(P, ret > 0 ? cwd : NULL,
 305      -                    (int (*)(const char *, void *))stat_exec, &st);
 306      -        }
 307      -
 308      -        return (NULL);
      252 +        return (P->ops.pop_execname(P, buf, buflen, P->data));
 309  253  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX