I am trying to use prctl( PR_SET_NAME, \"procname\", 0, 0, 0) to set name for a process, when I am reading the Linux Manual about the PR_SET_NAME, looks like it
Try the following code:
const char *newName = "newname";
char *baseName;
// find application base name to correct
char *appName = const_cast(argv[0]);
if (((baseName = strrchr(appName, '/')) != NULL ||
(baseName = strrchr(appName, '\\')) != NULL) && baseName[1]) {
appName = baseName + 1;
}
// Important! set new application name inside existing memory block.
// we want to avoid argv[0] = newName; because we don't know
// how cmd line buffer will be released during application shutdown phase
// Note: new process name has equal or shorter length than current argv[0]
size_t appNameLen;
if ((appNameLen = strlen(appName)) != 0) {
strncpy(appName, newName, appNameLen);
appName[appNameLen] = 0;
}
// set new current thread name
if (prctl(PR_SET_NAME, reinterpret_cast(const_cast(newName)), NULL, NULL, NULL)) {
Log::error("prctl(PR_SET_NAME, \"%s\") error - %s", newName, strerror(errno));
}