start.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*!
  2. * \file db/dbmi_client/start.c
  3. *
  4. * \brief DBMI Library (client) - open database connection
  5. *
  6. * (C) 1999-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. * \author Joel Jones (CERL/UIUC), Radim Blazek
  13. */
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #ifdef __MINGW32__
  18. #include <windows.h>
  19. #include <process.h>
  20. #include <fcntl.h>
  21. #endif
  22. #include <grass/dbmi.h>
  23. #define READ 0
  24. #define WRITE 1
  25. /*!
  26. \brief Initialize a new dbDriver for db transaction.
  27. If <i>name</i> is NULL, the db name will be assigned
  28. connection.driverName.
  29. \param name driver name
  30. \return pointer to dbDriver structure
  31. \return NULL on error
  32. */
  33. dbDriver *db_start_driver(const char *name)
  34. {
  35. dbDriver *driver;
  36. dbDbmscap *list, *cur;
  37. const char *startup;
  38. int p1[2], p2[2];
  39. int pid;
  40. int stat;
  41. dbConnection connection;
  42. char ebuf[5];
  43. #ifdef __MINGW32__
  44. int stdin_orig, stdout_orig;
  45. int have_stdin, have_stdout;
  46. int stdin_fd, stdout_fd;
  47. #endif
  48. /* Set some environment variables which are later read by driver.
  49. * This is necessary when application is running without GISRC file and all
  50. * gis variables are set by application.
  51. * Even if GISRC is set, application may change some variables during runtime,
  52. * if for example reads data from different gdatabase, location or mapset*/
  53. /* setenv() is not portable, putenv() is POSIX, putenv() in glibc 2.0-2.1.1 doesn't conform to SUSv2,
  54. * G_putenv() as well, but that is what we want, makes a copy of string */
  55. if (G_get_gisrc_mode() == G_GISRC_MODE_MEMORY) {
  56. G_debug(3, "G_GISRC_MODE_MEMORY\n");
  57. sprintf(ebuf, "%d", G_GISRC_MODE_MEMORY);
  58. G_putenv("GRASS_DB_DRIVER_GISRC_MODE", ebuf); /* to tell driver that it must read variables */
  59. if (G__getenv("DEBUG")) {
  60. G_putenv("DEBUG", G__getenv("DEBUG"));
  61. }
  62. else {
  63. G_putenv("DEBUG", "0");
  64. }
  65. G_putenv("GISDBASE", G__getenv("GISDBASE"));
  66. G_putenv("LOCATION_NAME", G__getenv("LOCATION_NAME"));
  67. G_putenv("MAPSET", G__getenv("MAPSET"));
  68. }
  69. else {
  70. /* Warning: GISRC_MODE_MEMORY _must_ be set to G_GISRC_MODE_FILE, because the module can be
  71. * run from an application which previously set environment variable to G_GISRC_MODE_MEMORY */
  72. sprintf(ebuf, "%d", G_GISRC_MODE_FILE);
  73. G_putenv("GRASS_DB_DRIVER_GISRC_MODE", ebuf);
  74. }
  75. /* read the dbmscap file */
  76. if (NULL == (list = db_read_dbmscap()))
  77. return (dbDriver *) NULL;
  78. /* if name is empty use connection.driverName, added by RB 4/2000 */
  79. if (name == '\0') {
  80. db_get_connection(&connection);
  81. if (NULL == (name = connection.driverName))
  82. return (dbDriver *) NULL;
  83. }
  84. /* find this system name */
  85. for (cur = list; cur; cur = cur->next)
  86. if (strcmp(cur->driverName, name) == 0)
  87. break;
  88. if (cur == NULL) {
  89. char msg[256];
  90. db_free_dbmscap(list);
  91. sprintf(msg, "%s: no such driver available", name);
  92. db_error(msg);
  93. return (dbDriver *) NULL;
  94. }
  95. /* allocate a driver structure */
  96. driver = (dbDriver *) db_malloc(sizeof(dbDriver));
  97. if (driver == NULL) {
  98. db_free_dbmscap(list);
  99. return (dbDriver *) NULL;
  100. }
  101. /* copy the relevant info from the dbmscap entry into the driver structure */
  102. db_copy_dbmscap_entry(&driver->dbmscap, cur);
  103. startup = driver->dbmscap.startup;
  104. /* free the dbmscap list */
  105. db_free_dbmscap(list);
  106. /* run the driver as a child process and create pipes to its stdin, stdout */
  107. #ifdef __MINGW32__
  108. /* create pipes (0 in array for reading, 1 for writing) */
  109. /* p1 : module -> driver, p2 driver -> module */
  110. /* I have seen problems with pipes on NT 5.1 probably related
  111. * to buffer size (psize, originaly 512 bytes).
  112. * But I am not sure, some problems were fixed by bigger
  113. * buffer but others remain.
  114. * Simple test which failed on NT 5.1 worked on NT 5.2
  115. * But there are probably other factors.
  116. */
  117. /* More info about pipes from MSDN:
  118. - Anonymous pipes are implemented using a named pipe
  119. with a unique name.
  120. - CreatePipe() - nSize :
  121. ... The size is only a suggestion; the system uses
  122. the value to calculate an appropriate buffering
  123. mechanism. ...
  124. => that that the size specified is not significant
  125. - If the pipe buffer is full before all bytes are written,
  126. WriteFile does not return until another process or thread
  127. uses ReadFile to make more buffer space available.
  128. (Which does not seem to be true on NT 5.1)
  129. */
  130. if (_pipe(p1, 250000, _O_BINARY) < 0 || _pipe(p2, 250000, _O_BINARY) < 0) {
  131. db_syserror("can't open any pipes");
  132. return (dbDriver *) NULL;
  133. }
  134. /* convert pipes to FILE* */
  135. driver->send = fdopen(p1[WRITE], "wb");
  136. driver->recv = fdopen(p2[READ], "rb");
  137. fflush(stdout);
  138. fflush(stderr);
  139. /* Set pipes for stdin/stdout driver */
  140. have_stdin = have_stdout = 1;
  141. if (_fileno(stdin) < 0) {
  142. have_stdin = 0;
  143. stdin_fd = 0;
  144. }
  145. else {
  146. stdin_fd = _fileno(stdin);
  147. if ((stdin_orig = _dup(_fileno(stdin))) < 0) {
  148. db_syserror("can't duplicate stdin");
  149. return (dbDriver *) NULL;
  150. }
  151. }
  152. if (_dup2(p1[0], stdin_fd) != 0) {
  153. db_syserror("can't duplicate pipe");
  154. return (dbDriver *) NULL;
  155. }
  156. if (_fileno(stdout) < 0) {
  157. have_stdout = 0;
  158. stdout_fd = 1;
  159. }
  160. else {
  161. stdout_fd = _fileno(stdout);
  162. if ((stdout_orig = _dup(_fileno(stdout))) < 0) {
  163. db_syserror("can't duplicate stdout");
  164. return (dbDriver *) NULL;
  165. }
  166. }
  167. if (_dup2(p2[1], stdout_fd) != 0) {
  168. db_syserror("can't duplicate pipe");
  169. return (dbDriver *) NULL;
  170. }
  171. /* Warning: the driver on Windows must have extension .exe
  172. * otherwise _spawnl fails. The name used as _spawnl
  173. * parameter can be without .exe
  174. */
  175. /* spawnl() works but the process inherits all handlers,
  176. * that means, for example p1[WRITE] remains open and if
  177. * module exits the pipe is not close and driver remains running.
  178. * Using CreateProcess() + SetHandleInformation() does not help.
  179. * => currently the only know solution is to close all file
  180. * descriptors in driver (which is another problem)
  181. */
  182. pid = _spawnl(_P_NOWAIT, startup, startup, NULL);
  183. /* This does not help. It runs but pipe remains open when close() is
  184. * called in model but caling close() on that descriptor in driver gives
  185. * error. */
  186. /*
  187. {
  188. STARTUPINFO si;
  189. PROCESS_INFORMATION pi;
  190. GetStartupInfo(&si);
  191. SetHandleInformation ( stdin, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
  192. SetHandleInformation ( stdout, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
  193. SetHandleInformation ( driver->send, HANDLE_FLAG_INHERIT, 0);
  194. SetHandleInformation ( driver->recv, HANDLE_FLAG_INHERIT, 0);
  195. CreateProcess(NULL, startup, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
  196. }
  197. */
  198. /* Reset stdin/stdout for module and close duplicates */
  199. if (have_stdin) {
  200. if (_dup2(stdin_orig, _fileno(stdin)) != 0) {
  201. db_syserror("can't reset stdin");
  202. return (dbDriver *) NULL;
  203. }
  204. close(stdin_orig);
  205. }
  206. if (have_stdout) {
  207. if (_dup2(stdout_orig, _fileno(stdout)) != 0) {
  208. db_syserror("can't reset stdout");
  209. return (dbDriver *) NULL;
  210. }
  211. close(stdout_orig);
  212. }
  213. if (pid == -1) {
  214. db_syserror("can't _spawnl");
  215. return (dbDriver *) NULL;
  216. }
  217. /* record driver process id in driver struct */
  218. driver->pid = pid;
  219. /* most systems will have to use unbuffered io to get the
  220. * send/recv to work */
  221. #ifndef USE_BUFFERED_IO
  222. setbuf(driver->send, NULL);
  223. setbuf(driver->recv, NULL);
  224. #endif
  225. db__set_protocol_fds(driver->send, driver->recv);
  226. if (db__recv_return_code(&stat) != DB_OK || stat != DB_OK)
  227. driver = NULL;
  228. return driver;
  229. #else /* __MINGW32__ */
  230. /* open the pipes */
  231. if ((pipe(p1) < 0) || (pipe(p2) < 0)) {
  232. db_syserror("can't open any pipes");
  233. return (dbDriver *) NULL;
  234. }
  235. /* create a child */
  236. if ((pid = fork()) < 0) {
  237. db_syserror("can't create fork");
  238. return (dbDriver *) NULL;
  239. }
  240. if (pid > 0) { /* parent */
  241. close(p1[READ]);
  242. close(p2[WRITE]);
  243. /* record driver process id in driver struct */
  244. driver->pid = pid;
  245. /* convert pipes to FILE* */
  246. driver->send = fdopen(p1[WRITE], "wb");
  247. driver->recv = fdopen(p2[READ], "rb");
  248. /* most systems will have to use unbuffered io to get the send/recv to work */
  249. #ifndef USE_BUFFERED_IO
  250. setbuf(driver->send, NULL);
  251. setbuf(driver->recv, NULL);
  252. #endif
  253. db__set_protocol_fds(driver->send, driver->recv);
  254. if (db__recv_return_code(&stat) != DB_OK || stat != DB_OK)
  255. driver = NULL;
  256. return driver;
  257. }
  258. else { /* child process */
  259. close(p1[WRITE]);
  260. close(p2[READ]);
  261. close(0);
  262. close(1);
  263. if (dup(p1[READ]) != 0) {
  264. db_syserror("dup r");
  265. _exit(EXIT_FAILURE);
  266. }
  267. if (dup(p2[WRITE]) != 1) {
  268. db_syserror("dup w");
  269. _exit(EXIT_FAILURE);
  270. }
  271. execl("/bin/sh", "sh", "-c", startup, NULL);
  272. db_syserror("execl");
  273. return NULL; /* to keep lint, et. al. happy */
  274. }
  275. #endif /* __MINGW32__ */
  276. }