Mercurial > dwindows
comparison android/dw.cpp @ 2695:11aaf443d64b
Android: Implement dw_exec() using fork/exec on APIs prior to 28.
Use posix_spawnp() on API 28 and later. Unlike other platforms,
currently this can only launch background processes.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Tue, 02 Nov 2021 20:08:29 +0000 |
parents | cee79add3669 |
children | bc7c16800892 |
comparison
equal
deleted
inserted
replaced
2694:cee79add3669 | 2695:11aaf443d64b |
---|---|
7214 * type: Either DW_EXEC_CON or DW_EXEC_GUI. | 7214 * type: Either DW_EXEC_CON or DW_EXEC_GUI. |
7215 * params: An array of pointers to string arguements. | 7215 * params: An array of pointers to string arguements. |
7216 * Returns: | 7216 * Returns: |
7217 * Process ID on success or DW_ERROR_UNKNOWN (-1) on error. | 7217 * Process ID on success or DW_ERROR_UNKNOWN (-1) on error. |
7218 */ | 7218 */ |
7219 #if defined(__ANDROID__) && (__ANDROID_API__+0) < 28 | |
7219 int API dw_exec(const char *program, int type, char **params) | 7220 int API dw_exec(const char *program, int type, char **params) |
7220 { | 7221 { |
7221 int ret = DW_ERROR_UNKNOWN; | 7222 int retval = DW_ERROR_UNKNOWN; |
7222 | 7223 |
7223 return ret; | 7224 /* Forking isn't recommended on Android... but it works... |
7224 } | 7225 * and prior to API 28 it is the only way to launch processes. |
7226 * Type is ignored, since this can only launch background processes. | |
7227 */ | |
7228 if((retval = fork()) == 0) | |
7229 { | |
7230 int i; | |
7231 | |
7232 /* Close any forked file descriptors */ | |
7233 for(i=3; i<256; i++) | |
7234 close(i); | |
7235 setsid(); | |
7236 | |
7237 execvp(program, params); | |
7238 } | |
7239 return retval; | |
7240 } | |
7241 #else | |
7242 #include <spawn.h> | |
7243 | |
7244 int API dw_exec(const char *program, int type, char **params) | |
7245 { | |
7246 int retval = DW_ERROR_UNKNOWN; | |
7247 pid_t pid; | |
7248 | |
7249 /* API 28 and later has posix_spawn*() so use that instead of | |
7250 * fork/exec, however launched processes are still background | |
7251 * processes so ignore the type parameter. | |
7252 */ | |
7253 if(posix_spawnp(&pid, program, NULL, NULL, params, NULL) == 0) | |
7254 { | |
7255 if(pid > 0) | |
7256 retval = pid; | |
7257 else | |
7258 retval = DW_ERROR_NONE; | |
7259 } | |
7260 return retval; | |
7261 } | |
7262 #endif | |
7225 | 7263 |
7226 /* | 7264 /* |
7227 * Loads a web browser pointed at the given URL. | 7265 * Loads a web browser pointed at the given URL. |
7228 * Parameters: | 7266 * Parameters: |
7229 * url: Uniform resource locator. | 7267 * url: Uniform resource locator. |