-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Program names on platforms without __progname in libc
- Loading branch information
Showing
6 changed files
with
112 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* | ||
* @date Created on: May 12, 2020 | ||
* @author Attila Kovacs | ||
* | ||
* Obtain the process name on platforms where the `__progname` is not defined by libc | ||
* such as on LynxOS 3.1 / PowerPC... | ||
*/ | ||
|
||
#ifndef LYNXOS_PROCNAME_H_ | ||
#define LYNXOS_PROCNAME_H_ | ||
|
||
#define DEFAULT_PROCESS_NAME "anonymous" | ||
|
||
#if __Lynx__ && __powerpc__ | ||
/** | ||
* Gets the process name for a given pid on LynxOS, by parsing process tables from /dev/mem. | ||
* | ||
* \param[in] pid Process ID, such as returned by getpid(). | ||
* \param[out] procName String to return process name in (terminated, and with path stripped) | ||
* \param[in] length The maximum number of characters to print into procName. | ||
* | ||
* \return 0 if the process was successfully identified (i.e. alive) | ||
* 1 if there is no live process with that ID (returns default name) | ||
* -1 if there was an error trying to look up the process name (return redault name). | ||
*/ | ||
int getProcessName(int pid, char *procName, const int length); | ||
|
||
#endif | ||
|
||
#endif /* LYNXOS_PROCNAME_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* | ||
* @date Created on: May 12, 2020 | ||
* @author Attila Kovacs | ||
* | ||
* Obtain the process name on LynxOS 3.1 / PowerPC, where the `__progname` macro does | ||
* not exists... | ||
*/ | ||
|
||
#include "procname.h" | ||
|
||
#if __Lynx__ && __powerpc__ | ||
|
||
#include <errno.h> | ||
#include <info.h> | ||
#include <mem.h> | ||
#include <proc.h> | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
|
||
|
||
// We'll parse the process table to match the PID to a process name... | ||
int getProcessName(int pid, char *procName, const int length) { | ||
int fd, status; | ||
char *shortName; | ||
__pentry proc; | ||
|
||
// Default process name | ||
strncpy(procName, DEFAULT_PROCESS_NAME, length-1); | ||
procName[length-1] = '\0'; | ||
|
||
if(pid <= 0 || pid > info(_I_NPROCTAB)) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
|
||
fd = open("/dev/mem", O_RDONLY); | ||
if (fd < 0) return -1; | ||
|
||
lseek(fd, info(_I_PROCTAB) + pid * sizeof(struct pentry), 0); | ||
status = read(fd, &proc, sizeof(struct pentry)); | ||
close(fd); | ||
|
||
if(status < 0) return -1; | ||
|
||
/* Ignore if slot is marked as free */ | ||
if(proc.pstate & PRFREE) return 1; | ||
|
||
shortName = strrchr(proc.pname, '/'); | ||
if(shortName == NULL) shortName = proc.pname; | ||
else shortName++; | ||
|
||
strncpy(procName, shortName, length-1); | ||
procName[length-1] = '\0'; // Failsafe terminate. | ||
|
||
return 0; | ||
} | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters