Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions inc/dmod_sal.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,9 @@ DMOD_BUILTIN_API(Dmod, 1.0, int , _Stdin_SetFlags, ( uint32_t Flags ) );
*/

DMOD_BUILTIN_API(Dmod, 1.0, void, _Exit, ( int Status ) );
DMOD_BUILTIN_API(Dmod, 1.0, int, _Spawn, ( Dmod_Context_t* Context, int argc, char *argv[] ) );
DMOD_BUILTIN_API(Dmod, 1.0, int, _RunDetached, ( Dmod_Context_t* Context, int argc, char *argv[] ) );
DMOD_BUILTIN_API(Dmod, 1.0, Dmod_Pid_t, _Spawn, ( Dmod_Context_t* Context, int argc, char *argv[] ) );
DMOD_BUILTIN_API(Dmod, 1.0, Dmod_Pid_t, _RunDetached, ( Dmod_Context_t* Context, int argc, char *argv[] ) );
DMOD_BUILTIN_API(Dmod, 1.0, int, _GetProcessResult, ( Dmod_Pid_t Pid ) );

//! @}

Expand Down
23 changes: 23 additions & 0 deletions inc/dmod_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ typedef enum
Dmod_LogLevel_Count //!< Number of log levels
} Dmod_LogLevel_t;

/**
* @brief Process ID type
*
* This type represents a process ID. On POSIX systems, this is typically pid_t.
* A positive value indicates the process ID of the spawned process.
* A negative value indicates an error code.
*
* Note: int32_t is used for cross-platform consistency. On most systems, PIDs
* fit comfortably within 32-bit signed integers (typical max PID is ~4 million).
* The weak implementation uses DMOD_CURRENT_PROCESS_PID as a placeholder
* when running in the current process (no real process spawning available).
*/
typedef int32_t Dmod_Pid_t;

/**
* @brief Placeholder PID for the current process
*
* This value is returned by weak implementations of spawn functions when
* they run the module in the current process instead of spawning a new one.
* This is not meant to represent a real system PID.
*/
#define DMOD_CURRENT_PROCESS_PID ((Dmod_Pid_t)1)

typedef struct
{
uint32_t Size;
Expand Down
12 changes: 6 additions & 6 deletions src/system/dmod_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ int Dmod_RunModule(const char* Module, int argc, char *argv[])
* @param argc Number of arguments
* @param argv Arguments
*
* @return Return value of the main function
* @return Process ID on success (positive value), negative error code on failure
*/
int Dmod_SpawnModule(const char* Module, int argc, char *argv[])
{
Expand All @@ -1767,9 +1767,9 @@ int Dmod_SpawnModule(const char* Module, int argc, char *argv[])
return -ENOENT;
}

int result = Dmod_Spawn( context, argc, argv );
Dmod_Pid_t result = Dmod_Spawn( context, argc, argv );
Dmod_Unload( context, false );
return result;
return (int)result;
}

/**
Expand All @@ -1779,7 +1779,7 @@ int Dmod_SpawnModule(const char* Module, int argc, char *argv[])
* @param argc Number of arguments
* @param argv Arguments
*
* @return Return value of the main function
* @return Process ID on success (positive value), negative error code on failure
*/
int Dmod_RunModuleDetached(const char* Module, int argc, char *argv[])
{
Expand All @@ -1804,9 +1804,9 @@ int Dmod_RunModuleDetached(const char* Module, int argc, char *argv[])
return -ENOENT;
}

int result = Dmod_RunDetached( context, argc, argv );
Dmod_Pid_t result = Dmod_RunDetached( context, argc, argv );
Dmod_Unload( context, false );
return result;
return (int)result;
}

/**
Expand Down
45 changes: 39 additions & 6 deletions src/system/if/dmod_if_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/

#include "dmod_sal.h"
#include <inttypes.h>
#if DMOD_USE_STDLIB
# include <stdlib.h>
#endif
Expand Down Expand Up @@ -67,11 +68,17 @@ DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, void, _Exit, ( int Status ))
* @param Context Module context to spawn
* @param argc Number of arguments
* @param argv Argument array
* @return Return value of the module or error code
* @return Process ID on success (weak implementation returns DMOD_CURRENT_PROCESS_PID as placeholder),
* negative error code on failure
*/
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, int, _Spawn, ( Dmod_Context_t* Context, int argc, char *argv[] ))
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, Dmod_Pid_t, _Spawn, ( Dmod_Context_t* Context, int argc, char *argv[] ))
{
return Dmod_Run(Context, argc, argv);
int result = Dmod_Run(Context, argc, argv);
if(result < 0)
{
return (Dmod_Pid_t)result;
}
return DMOD_CURRENT_PROCESS_PID;
}

/**
Expand All @@ -83,9 +90,35 @@ DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, int, _Spawn, ( Dmod_Context_t* Contex
* @param Context Module context to run detached
* @param argc Number of arguments
* @param argv Argument array
* @return Return value of the module or error code
* @return Process ID on success (weak implementation returns DMOD_CURRENT_PROCESS_PID as placeholder),
* negative error code on failure
*/
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, int, _RunDetached, ( Dmod_Context_t* Context, int argc, char *argv[] ))
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, Dmod_Pid_t, _RunDetached, ( Dmod_Context_t* Context, int argc, char *argv[] ))
{
return Dmod_Run(Context, argc, argv);
int result = Dmod_Run(Context, argc, argv);
if(result < 0)
{
return (Dmod_Pid_t)result;
}
return DMOD_CURRENT_PROCESS_PID;
}

/**
* @brief Get the result of a process
*
* This is a weak implementation that returns 0 (success) for the current process placeholder.
* The real implementation should be provided by the dmosi layer to wait for and
* retrieve the exit status of a spawned process.
*
* @param Pid Process ID to get result for
* @return Exit status of the process, or negative error code
*/
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, int, _GetProcessResult, ( Dmod_Pid_t Pid ))
{
if(Pid == DMOD_CURRENT_PROCESS_PID)
{
return 0;
}
DMOD_LOG_ERROR("Dmod_GetProcessResult interface not implemented for PID %" PRId32 "\n", Pid);
return -1;
}
Loading