1 /* @(#)getexecpath.c 1.1 10/11/18 Copyright 2006-2010 J. Schilling */ 2 /* 3 * Copyright (c) 2006.2010 J. Schilling 4 */ 5 /* 6 * The contents of this file are subject to the terms of the 7 * Common Development and Distribution License, Version 1.0 only 8 * (the "License"). You may not use this file except in compliance 9 * with the License. 10 * 11 * See the file CDDL.Schily.txt in this distribution for details. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file CDDL.Schily.txt from this distribution. 15 */ 16 17 #include <schily/mconfig.h> 18 #include <schily/types.h> 19 #include <schily/unistd.h> 20 #include <schily/string.h> 21 #include <schily/standard.h> 22 #include <schily/schily.h> 23 24 #if (defined(sun) || defined(__sun) || defined(__sun__)) && defined(__SVR4) 25 #define PATH_IMPL 26 #define METHOD_SYMLINK 27 #define SYMLINK_PATH "/proc/self/path/a.out" /* Solaris 10 -> ... */ 28 #endif 29 30 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 31 #define PATH_IMPL 32 #define METHOD_SYMLINK 33 #define SYMLINK_PATH "/proc/curproc/file" /* /proc may nor be mounted */ 34 #endif 35 36 #if defined(__linux__) || defined(__linux)|| defined(linux) 37 #define PATH_IMPL 38 #define METHOD_SYMLINK 39 #define SYMLINK_PATH "/proc/self/exe" 40 #endif 41 42 #if defined(HAVE_PROC_PIDPATH) /* Mac OS X */ 43 #ifdef HAVE_LIBPROC_H 44 #include <libproc.h> 45 #endif 46 #define PATH_IMPL 47 #endif 48 49 /* 50 * TODO: AIX: /proc/$$/object/a.out -> plain file, match via st_dev/st_ino 51 */ 52 53 54 EXPORT char * 55 getexecpath() 56 { 57 #ifdef PATH_IMPL 58 #ifdef METHOD_SYMLINK 59 char buf[1024]; 60 ssize_t len; 61 62 len = readlink(SYMLINK_PATH, buf, sizeof (buf)-1); 63 if (len == -1) 64 return (NULL); 65 buf[len] = '\0'; 66 return (strdup(buf)); 67 #endif 68 #ifdef HAVE_PROC_PIDPATH /* Mac OS X */ 69 char buf[1024]; 70 int len; 71 72 len = proc_pidpath(getpid(), buf, sizeof (buf)); 73 if (len == -1) 74 return (NULL); 75 return (strdup(buf)); 76 #endif 77 #else 78 return (NULL); 79 #endif 80 } 81