Hello, I have a similar problem, but how do I read the system environment of the parent process that invoked the Qt application?
The reason I need to read the parent process's system environment is that the Qt Application requires root access and so sudo is used which makes the Qt application's environment a different copy. The parent process has the environment variable "GDMSESSION" which is what I want to read as I want to be able to detect the current session's desktop environment from within the program.
What I have done so far is use getppid(), and read the ppid's associated environ file. Problem is, that when using using a QFile to open the environ file, it cannot be read and parsed. Interestingly, the QFile::size() file specifically mentions that the environ file cannot be read like regular files and requires a read() to populate them. But that call is returning a error value so I am not sure if I am going about this the wrong way.
int ppid = getppid();
bool bOpen
= ppidenv.
open(QIODevice::ReadOnly);
char* str=NULL;
qint64 MAXSIZE= 1e18;
int result_of_read = (int)ppidenv.read(str,MAXSIZE);
printf("The permission for file are: %x\n",(int)ppidenv.permissions());
printf("Is the file open?: %s\n",bOpen?"true":"false");
printf("file size: %d\n",(int)ppidenv.size());
printf("PARENT PID environ path IS: %s\n\n\n\n\n\n\n\n",ppidenv.fileName().toUtf8().data());
printf("PARENT PID IS: %d\n\n\n\n\n\n\n\n",ppid);
printf("result_of_read: %d\n",result_of_read);
printf("ppidenvstr: %s\n",ppidenvstr.toUtf8().data());
printf("Str: %s\n",str);
int ppid = getppid();
QFile ppidenv(QString("/proc/%1/environ").arg(ppid));
bool bOpen = ppidenv.open(QIODevice::ReadOnly);
char* str=NULL;
qint64 MAXSIZE= 1e18;
int result_of_read = (int)ppidenv.read(str,MAXSIZE);
QString ppidenvstr(str);
printf("The permission for file are: %x\n",(int)ppidenv.permissions());
printf("Is the file open?: %s\n",bOpen?"true":"false");
printf("file size: %d\n",(int)ppidenv.size());
printf("PARENT PID environ path IS: %s\n\n\n\n\n\n\n\n",ppidenv.fileName().toUtf8().data());
printf("PARENT PID IS: %d\n\n\n\n\n\n\n\n",ppid);
printf("result_of_read: %d\n",result_of_read);
printf("ppidenvstr: %s\n",ppidenvstr.toUtf8().data());
printf("Str: %s\n",str);
To copy to clipboard, switch view to plain text mode
cout:
The permission for file are: 4400
Is the file open?: true
file size: 0
PARENT PID environ path IS: /proc/21890/environ
PARENT PID IS: 21890
result_of_read: -1
ppidenvstr:
Str: (null)
Bookmarks