Skip to content

Commit 5093824

Browse files
authored
Merge pull request #214 from choco-technologies/copilot/fix-dmod-spawn-return-type
Return Dmod_Pid_t from Dmod_Spawn/Dmod_RunDetached, add Dmod_GetProcessResult
2 parents 6bdd9b0 + 95899a9 commit 5093824

4 files changed

Lines changed: 71 additions & 14 deletions

File tree

inc/dmod_sal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,9 @@ DMOD_BUILTIN_API(Dmod, 1.0, int , _Stdin_SetFlags, ( uint32_t Flags ) );
369369
*/
370370

371371
DMOD_BUILTIN_API(Dmod, 1.0, void, _Exit, ( int Status ) );
372-
DMOD_BUILTIN_API(Dmod, 1.0, int, _Spawn, ( Dmod_Context_t* Context, int argc, char *argv[] ) );
373-
DMOD_BUILTIN_API(Dmod, 1.0, int, _RunDetached, ( Dmod_Context_t* Context, int argc, char *argv[] ) );
372+
DMOD_BUILTIN_API(Dmod, 1.0, Dmod_Pid_t, _Spawn, ( Dmod_Context_t* Context, int argc, char *argv[] ) );
373+
DMOD_BUILTIN_API(Dmod, 1.0, Dmod_Pid_t, _RunDetached, ( Dmod_Context_t* Context, int argc, char *argv[] ) );
374+
DMOD_BUILTIN_API(Dmod, 1.0, int, _GetProcessResult, ( Dmod_Pid_t Pid ) );
374375

375376
//! @}
376377

inc/dmod_types.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,29 @@ typedef enum
4949
Dmod_LogLevel_Count //!< Number of log levels
5050
} Dmod_LogLevel_t;
5151

52+
/**
53+
* @brief Process ID type
54+
*
55+
* This type represents a process ID. On POSIX systems, this is typically pid_t.
56+
* A positive value indicates the process ID of the spawned process.
57+
* A negative value indicates an error code.
58+
*
59+
* Note: int32_t is used for cross-platform consistency. On most systems, PIDs
60+
* fit comfortably within 32-bit signed integers (typical max PID is ~4 million).
61+
* The weak implementation uses DMOD_CURRENT_PROCESS_PID as a placeholder
62+
* when running in the current process (no real process spawning available).
63+
*/
64+
typedef int32_t Dmod_Pid_t;
65+
66+
/**
67+
* @brief Placeholder PID for the current process
68+
*
69+
* This value is returned by weak implementations of spawn functions when
70+
* they run the module in the current process instead of spawning a new one.
71+
* This is not meant to represent a real system PID.
72+
*/
73+
#define DMOD_CURRENT_PROCESS_PID ((Dmod_Pid_t)1)
74+
5275
typedef struct
5376
{
5477
uint32_t Size;

src/system/dmod_system.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,7 @@ int Dmod_RunModule(const char* Module, int argc, char *argv[])
17421742
* @param argc Number of arguments
17431743
* @param argv Arguments
17441744
*
1745-
* @return Return value of the main function
1745+
* @return Process ID on success (positive value), negative error code on failure
17461746
*/
17471747
int Dmod_SpawnModule(const char* Module, int argc, char *argv[])
17481748
{
@@ -1767,9 +1767,9 @@ int Dmod_SpawnModule(const char* Module, int argc, char *argv[])
17671767
return -ENOENT;
17681768
}
17691769

1770-
int result = Dmod_Spawn( context, argc, argv );
1770+
Dmod_Pid_t result = Dmod_Spawn( context, argc, argv );
17711771
Dmod_Unload( context, false );
1772-
return result;
1772+
return (int)result;
17731773
}
17741774

17751775
/**
@@ -1779,7 +1779,7 @@ int Dmod_SpawnModule(const char* Module, int argc, char *argv[])
17791779
* @param argc Number of arguments
17801780
* @param argv Arguments
17811781
*
1782-
* @return Return value of the main function
1782+
* @return Process ID on success (positive value), negative error code on failure
17831783
*/
17841784
int Dmod_RunModuleDetached(const char* Module, int argc, char *argv[])
17851785
{
@@ -1804,9 +1804,9 @@ int Dmod_RunModuleDetached(const char* Module, int argc, char *argv[])
18041804
return -ENOENT;
18051805
}
18061806

1807-
int result = Dmod_RunDetached( context, argc, argv );
1807+
Dmod_Pid_t result = Dmod_RunDetached( context, argc, argv );
18081808
Dmod_Unload( context, false );
1809-
return result;
1809+
return (int)result;
18101810
}
18111811

18121812
/**

src/system/if/dmod_if_proc.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*/
3232

3333
#include "dmod_sal.h"
34+
#include <inttypes.h>
3435
#if DMOD_USE_STDLIB
3536
# include <stdlib.h>
3637
#endif
@@ -67,11 +68,17 @@ DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, void, _Exit, ( int Status ))
6768
* @param Context Module context to spawn
6869
* @param argc Number of arguments
6970
* @param argv Argument array
70-
* @return Return value of the module or error code
71+
* @return Process ID on success (weak implementation returns DMOD_CURRENT_PROCESS_PID as placeholder),
72+
* negative error code on failure
7173
*/
72-
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, int, _Spawn, ( Dmod_Context_t* Context, int argc, char *argv[] ))
74+
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, Dmod_Pid_t, _Spawn, ( Dmod_Context_t* Context, int argc, char *argv[] ))
7375
{
74-
return Dmod_Run(Context, argc, argv);
76+
int result = Dmod_Run(Context, argc, argv);
77+
if(result < 0)
78+
{
79+
return (Dmod_Pid_t)result;
80+
}
81+
return DMOD_CURRENT_PROCESS_PID;
7582
}
7683

7784
/**
@@ -83,9 +90,35 @@ DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, int, _Spawn, ( Dmod_Context_t* Contex
8390
* @param Context Module context to run detached
8491
* @param argc Number of arguments
8592
* @param argv Argument array
86-
* @return Return value of the module or error code
93+
* @return Process ID on success (weak implementation returns DMOD_CURRENT_PROCESS_PID as placeholder),
94+
* negative error code on failure
8795
*/
88-
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, int, _RunDetached, ( Dmod_Context_t* Context, int argc, char *argv[] ))
96+
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, Dmod_Pid_t, _RunDetached, ( Dmod_Context_t* Context, int argc, char *argv[] ))
8997
{
90-
return Dmod_Run(Context, argc, argv);
98+
int result = Dmod_Run(Context, argc, argv);
99+
if(result < 0)
100+
{
101+
return (Dmod_Pid_t)result;
102+
}
103+
return DMOD_CURRENT_PROCESS_PID;
104+
}
105+
106+
/**
107+
* @brief Get the result of a process
108+
*
109+
* This is a weak implementation that returns 0 (success) for the current process placeholder.
110+
* The real implementation should be provided by the dmosi layer to wait for and
111+
* retrieve the exit status of a spawned process.
112+
*
113+
* @param Pid Process ID to get result for
114+
* @return Exit status of the process, or negative error code
115+
*/
116+
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, int, _GetProcessResult, ( Dmod_Pid_t Pid ))
117+
{
118+
if(Pid == DMOD_CURRENT_PROCESS_PID)
119+
{
120+
return 0;
121+
}
122+
DMOD_LOG_ERROR("Dmod_GetProcessResult interface not implemented for PID %" PRId32 "\n", Pid);
123+
return -1;
91124
}

0 commit comments

Comments
 (0)