3

I'm spawning a child process from my application:

    QString strFullPath(strModulesPath 
                      + strModule.toString());
    QByteArray baFullPath(strFullPath.toLatin1())
              ,baSeconds((QString::number(lngSeconds))
                       .toLatin1());
    char** ppEnviron
        ,* pszFullPath = baFullPath.data()
        ,* pszSeconds = baSeconds.data()
        ,* paryszArgs[] = {pszFullPath
                          ,pszSeconds
                          ,nullptr};
    posix_spawn_file_actions_t* pfileActionsp;
    posix_spawnattr_t* pAttr;
    pid_t pid = 0;
    pfileActionsp = pAttr = nullptr;
    int intRC = posix_spawn(&pid
                           ,pszFullPath
                           ,pfileActionsp
                           ,pAttr
                           ,paryszArgs
                           ,ppEnviron);

The application to launch is specified in baFullPath and contains:

~/XMLMPAM/config/modules/mdFileIO

The pid returned after the call to posix_spawn is valid and intRC returns 2.

However I cannot see the process listed in the "Activity Monitor", the parent process is listed but not the child.

Where is it and how can I see the output from the console as it doesn't appear in the same console as the parent process.

[edit] It would appear that the "posix_spawn" doesn't support spawning using the path prefix "~", so I tried the full path:

/Users/Simon/XMLMPAM/config/modules

I watched in the debugger and now the return is 14, which according to the error list is "Bad Address".

[edit 2] As pointed out by David Schwartz, it wasn't working because I hadn't initialised the "ppEnviron".

SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • 1
    From the posix_spawn man-page: "posix_spawn() returns [...] 0 on success. If an error occurs, [it] returns a non-zero error code as the function return value, and no child process is created." Therefore, if you are seeing `intRC` set to any value other than 0, then `posix_spawn()` must have failed. – Jeremy Friesner Apr 16 '19 at 05:45
  • Thank you I’ll look into the return codes, I thought because PID is being set to what looks like a valid process ID that the process has been started. – SPlatten Apr 16 '19 at 06:10
  • @JeremyFriesner, Finding details of what the return values mean isn't easy, http://pubs.opengroup.org/onlinepubs/009696899/functions/posix_spawn.html, according to http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html, error 2 is no such file or directory – SPlatten Apr 16 '19 at 08:04
  • Your `ppEnviron` parameter contains random junk. – David Schwartz Apr 16 '19 at 18:12
  • @Thank you David, that was the problem! – SPlatten Apr 16 '19 at 19:00

2 Answers2

2

The solution to this problem was pointed out by "David Schwartz" in a comment to the question.

The spawn operation was failing because the pointer to the environment wasn't initialised to NULL.

SPlatten
  • 5,334
  • 11
  • 57
  • 128
0

EFAULT (14) means one of the arguments you passed is an invalid address. Usually you could print all the pointers before you pass them to make sure they point to valid memory.

This line in particular seems suspicious:

pfileActionsp = pAttr = nullptr;

The man page says:

The attrp argument points to an attributes objects that specifies various attributes of the created child process. This object is initialized and populated before the posix_spawn() call using posix_spawnattr_init(3) and the posix_spawnattr_*() functions.

Kevin Chen
  • 994
  • 8
  • 24