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.

Qt Code:
  1. int ppid = getppid();
  2. QFile ppidenv(QString("/proc/%1/environ").arg(ppid));
  3. bool bOpen = ppidenv.open(QIODevice::ReadOnly);
  4. char* str=NULL;
  5. qint64 MAXSIZE= 1e18;
  6. int result_of_read = (int)ppidenv.read(str,MAXSIZE);
  7. QString ppidenvstr(str);
  8. printf("The permission for file are: %x\n",(int)ppidenv.permissions());
  9. printf("Is the file open?: %s\n",bOpen?"true":"false");
  10. printf("file size: %d\n",(int)ppidenv.size());
  11. printf("PARENT PID environ path IS: %s\n\n\n\n\n\n\n\n",ppidenv.fileName().toUtf8().data());
  12. printf("PARENT PID IS: %d\n\n\n\n\n\n\n\n",ppid);
  13. printf("result_of_read: %d\n",result_of_read);
  14. printf("ppidenvstr: %s\n",ppidenvstr.toUtf8().data());
  15. 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)