shutdown.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*!
  2. * \file db/dbmi_client/shutdown.c
  3. *
  4. * \brief DBMI Library (client) - shutdown 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 <stdlib.h>
  15. #ifdef __MINGW32__
  16. #include <process.h>
  17. #else
  18. #include <sys/wait.h>
  19. #endif
  20. #include <grass/dbmi.h>
  21. #include "macros.h"
  22. /*!
  23. \brief Closedown the driver, and free the driver structure
  24. <b>Note:</b> the management of the memory for the driver structure
  25. probably should be handled differently.
  26. db_start_driver() could take a pointer to driver structure as
  27. an argument, instead of returning the pointer to allocated
  28. then there would be no hidden free required
  29. \param driver db driver
  30. \return status (?)
  31. */
  32. int db_shutdown_driver(dbDriver * driver)
  33. {
  34. #ifndef __MINGW32__
  35. int pid;
  36. #endif
  37. int status;
  38. #ifdef __MINGW32__
  39. db__set_protocol_fds(driver->send, driver->recv);
  40. DB_START_PROCEDURE_CALL(DB_PROC_SHUTDOWN_DRIVER);
  41. #endif
  42. /* close the communication FILEs */
  43. fclose(driver->send);
  44. fclose(driver->recv);
  45. driver->send = NULL;
  46. driver->recv = NULL;
  47. /* wait for the driver to finish */
  48. status = -1;
  49. #ifdef __MINGW32__
  50. /* TODO: convert status to something like from wait? */
  51. _cwait(&status, driver->pid, WAIT_CHILD);
  52. #else
  53. /* TODO: Should not be here waitpid() ? */
  54. while ((pid = wait(&status)) > 0 && pid != driver->pid) {
  55. }
  56. #endif
  57. driver->pid = 0;
  58. /* free the driver structure. THIS IS GOOFY */
  59. free(driver);
  60. return status;
  61. }