Move get_self_executable_path() into "os.h"/os.c

This commit is contained in:
Andrew Bettison 2014-05-05 18:12:01 +09:30
parent df1814309b
commit f606daebc6
4 changed files with 25 additions and 15 deletions

14
log.c
View File

@ -722,20 +722,6 @@ void logConfigChanged()
logFlush();
}
ssize_t get_self_executable_path(char *buf, size_t len)
{
#if defined(linux)
return read_symlink("/proc/self/exe", buf, len);
#elif defined (__sun__)
return read_symlink("/proc/self/path/a.out", buf, len);
#elif defined (__APPLE__)
uint32_t bufsize = len;
return _NSGetExecutablePath(buf, &bufsize) == -1 && len ? -1 : bufsize;
#else
#error Unable to find executable path
#endif
}
int log_backtrace(int level, struct __sourceloc whence)
{
#ifndef NO_BACKTRACE

1
log.h
View File

@ -104,7 +104,6 @@ __attribute__ (( format(printf,3,4) ));
void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list);
void logConfigChanged();
int logDump(int level, struct __sourceloc whence, char *name, const unsigned char *addr, size_t len);
ssize_t get_self_executable_path(char *buf, size_t len);
int log_backtrace(int level, struct __sourceloc whence);
struct strbuf;

14
os.c
View File

@ -332,3 +332,17 @@ int alter_file_meta(const char *path, const struct file_meta *origp, struct file
}
return 1;
}
ssize_t get_self_executable_path(char *buf, size_t len)
{
#if defined(linux)
return read_symlink("/proc/self/exe", buf, len);
#elif defined (__sun__)
return read_symlink("/proc/self/path/a.out", buf, len);
#elif defined (__APPLE__)
uint32_t bufsize = len;
return _NSGetExecutablePath(buf, &bufsize) == -1 && len ? -1 : bufsize;
#else
#error Unable to find executable path
#endif
}

11
os.h
View File

@ -203,4 +203,15 @@ int cmp_file_meta(const struct file_meta *a, const struct file_meta *b);
// by bumping the file's modification time or altering its inode.
int alter_file_meta(const char *path, const struct file_meta *origp, struct file_meta *metap);
/* Fill the given buffer with the nul-terminated absolute path of the calling
* process's executable. Logs an error and returns -1 if the executable cannot
* be determined or the supplied buffer is too short. Otherwise returns the
* number of bytes placed in the buffer, including the terminating nul (ie,
* returns strlen(buf) + 1).
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
ssize_t get_self_executable_path(char *buf, size_t len);
#endif //__SERVAL_DNA__OS_H