home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / os2db2.zip / SHUTDOWN.CPP < prev    next >
Text File  |  1994-09-03  |  12KB  |  282 lines

  1.  /******************************************************
  2. *  SHUTDOWN.CPP - SHUTDOWN CLASS                        *
  3.  ******************************************************/
  4.  
  5. #include "defines.hpp"                                // type defines
  6. #include "shutdown.hpp"                               // shutdown class
  7. #include "command.hpp"                                // command class
  8. #include "process.hpp"                                // process class
  9.  
  10. #define   DB2WAIT 30000L                              // DB2 manager
  11. #define   OS2WAIT 30000L                              // file system
  12.  
  13. extern "C" {
  14. #include "sql.h"                                      // sql prototypes
  15. #include "sqlenv.h"                                   //   "        "
  16. #include "sqlcodes.h"                                 //   "        "
  17. #include "sqlutil.h"                                  //   "        "
  18. #include "sqlca.h"                                    //   "        "
  19.  
  20. NOLINE VOID stopDB2Manager(VOID *shutDown);           // client server
  21. NOLINE VOID stopDB2Clients(VOID *shutDown);           // manager only
  22. NOLINE VOID stopFileSystem(VOID *shutDown);           // update deamon
  23. }
  24. static sqlca SQLCA;                                   // sql structure
  25. static HEV DB2Handle = 0L, DB2Finish = 0L;            // DB2 semaphores
  26. static HEV OS2Handle = 0L, OS2Finish = 0L;            // OS2 semaphores
  27.  
  28. /******************************************************
  29. *  main - mainline                                      *
  30.  ******************************************************/
  31.  
  32. VOID main(ULONG argCount, PCHAR argVector[])
  33.  
  34. {
  35.      LONG returnCode = 0L;                            // return code
  36.  
  37.      COMMAND *comMand = new COMMAND();                // command class
  38.      PROCESS *proCess = new PROCESS();                // process class
  39.  
  40.      if (comMand->commandLine(argCount, argVector) == TRUE) {
  41.           SHUTDOWN *shutDown = new SHUTDOWN();        // shutdown class
  42.           /*
  43.            * create a few semaphores
  44.            */
  45.           returnCode = DosCreateEventSem("\\SEM32\\STOPDB2.SEM", &DB2Handle, 0L, FALSE);
  46.           returnCode = DosCreateEventSem("\\SEM32\\WAITDB2.SEM", &DB2Finish, 0L, FALSE);
  47.           returnCode = DosCreateEventSem("\\SEM32\\STOPOS2.SEM", &OS2Handle, 0L, FALSE);
  48.           returnCode = DosCreateEventSem("\\SEM32\\WAITOS2.SEM", &OS2Finish, 0L, FALSE);
  49.           /*
  50.            * next just list the running process (no DosKillProcess)
  51.            */
  52.           if ((comMand->doEverything == TRUE) ||      // do everything
  53.               (comMand->listingFlag  == TRUE)) {      // listing flag
  54.                proCess->listRunning();                // list process
  55.                /*
  56.                 * if we're doing anything else then we skip a line
  57.                 */
  58.                if ((comMand->doEverything != FALSE) ||// do everything
  59.                    (comMand->clientServer != FALSE) ||// client server
  60.                    (comMand->managerOnly  != FALSE) ||// manager only
  61.                    (comMand->updateDeamon != FALSE) ||// update deamon
  62.                    (comMand->killProcess  != NOPID)) {// kill process
  63.                     printf("\n");                     // skip a line
  64.                }                                      // do everything
  65.           }
  66.           /*
  67.            * if there's a process to kill then call DosKillProcess
  68.            */
  69.           // ((comMand->doEverything == TRUE) ||      // do everything
  70.           if ((comMand->killProcess  != NOPID)) {     // kill process
  71.                PID processID = (PID) comMand->killProcess;
  72.                printf("Killing process (pid=%lu)...", (ULONG) processID);
  73.                returnCode = DosKillProcess(1L, processID);
  74.                printf("complete! (rc=%lu)\n", returnCode);
  75.           }
  76.           /*
  77.            * if we're really doing anything tell the user about it
  78.            */
  79.           if ((comMand->doEverything != FALSE) ||     // do everything
  80.               (comMand->clientServer != FALSE) ||     // client server
  81.               (comMand->managerOnly  != FALSE) ||     // manager only
  82.               (comMand->updateDeamon != FALSE)) {     // update deamon
  83.                printf("Starting shutdown...\n");      // start shutdown
  84.                shutDown->startThreads(comMand);       // start threads
  85.                shutDown->cycleThreads(comMand);       // cycle threads
  86.           }                                           // do everything
  87.           /*
  88.            * clean up the semaphores
  89.            */
  90.           returnCode = DosCloseEventSem(DB2Handle);   // DB2 manager
  91.           returnCode = DosCloseEventSem(DB2Finish);   // DB2 manager
  92.           returnCode = DosCloseEventSem(OS2Handle);   // file system
  93.           returnCode = DosCloseEventSem(OS2Finish);   // file system
  94.  
  95.           delete shutDown;                            // shutdown class
  96.      }
  97.      delete proCess;                                  // process class
  98.      delete comMand;                                  // command class
  99.  
  100.      DosExit(EXIT_PROCESS, returnCode);               // exit program
  101. }
  102.  
  103.  /******************************************************
  104. *  SHUTDOWN::startThreads - start threads               *
  105.  ******************************************************/
  106.  
  107. NOLINE VOID SHUTDOWN::startThreads(COMMAND *comMand)
  108.  
  109. {
  110.      LONG returnCode;                                 // return code
  111.      /*
  112.       * kill DB/2 connections and/or stop the database manager
  113.       */
  114.      // ((comMand->doEverything == TRUE) ||           // do everything
  115.      if ((comMand->managerOnly  == TRUE)) {           // manager only
  116.           _beginthread(::stopDB2Manager, NULL, 0x4000U, (VOID *) this);
  117.      } else
  118.      /*
  119.       * kill DB/2 connections and/or stop the database manager
  120.       */
  121.      if ((comMand->doEverything == TRUE) ||           // do everything
  122.          (comMand->clientServer == TRUE)) {           // client server
  123.           _beginthread(::stopDB2Clients, NULL, 0x4000U, (VOID *) this);
  124.      }
  125.      /*
  126.       * finally we should be able to stop the OS/2 file system
  127.       */
  128.      if ((comMand->doEverything == TRUE) ||           // do everything
  129.          (comMand->updateDeamon == TRUE)) {           // update deamon
  130.           _beginthread(::stopFileSystem, NULL, 0x4000U, (VOID *) this);
  131.      }
  132. }
  133.  
  134.  /******************************************************
  135. *  SHUTDOWN::cycleThreads - cycle threads               *
  136.  ******************************************************/
  137.  
  138. NOLINE VOID SHUTDOWN::cycleThreads(COMMAND *comMand)
  139.  
  140. {
  141.      LONG returnCode;                                 // return code
  142.      /*
  143.       * kill DB/2 connections and/or stop the database manager
  144.       */
  145.      // ((comMand->doEverything == TRUE) ||           // do everything
  146.      if ((comMand->managerOnly  == TRUE)) {           // manager only
  147.           returnCode = DosPostEventSem(DB2Handle);    // wakeup thread
  148.           returnCode = DosWaitEventSem(DB2Finish, DB2WAIT);
  149.      } else
  150.      /*
  151.       * kill DB/2 connections and/or stop the database manager
  152.       */
  153.      if ((comMand->doEverything == TRUE) ||           // do everything
  154.          (comMand->clientServer == TRUE)) {           // client server
  155.           returnCode = DosPostEventSem(DB2Handle);    // wakeup thread
  156.           returnCode = DosWaitEventSem(DB2Finish, DB2WAIT);
  157.      }
  158.      /*
  159.       * finally we should be able to stop the OS/2 file system
  160.       */
  161.      if ((comMand->doEverything == TRUE) ||           // do everything
  162.          (comMand->updateDeamon == TRUE)) {           // update deamon
  163.           returnCode = DosPostEventSem(OS2Handle);    // wakeup thread
  164.           returnCode = DosWaitEventSem(OS2Finish, OS2WAIT);
  165.      }
  166. }
  167.  
  168.  /******************************************************
  169. *  SHUTDOWN::stopDB2Manager - stop DB2 manager          *
  170.  ******************************************************/
  171.  
  172. NOLINE LONG SHUTDOWN::stopDB2Manager(VOID)
  173.  
  174. {
  175.      LONG returnCode;                                 // return code
  176.  
  177.      printf("Stopping database manager...\n");        // show message
  178.  
  179.      sqlestop(&SQLCA);                                // DB2 manager
  180.  
  181.      returnCode = SQLCA.sqlcode;                      // get sqlcode
  182.  
  183.      if (returnCode == 0L) printf("Database shutdown!\n");
  184.  
  185.      else                  printf("DB/2 Error! (%ld)\n");
  186.  
  187.      return returnCode;                               // return code
  188. }
  189.  
  190.  /******************************************************
  191. *  SHUTDOWN::stopDB2Clients - stop DB2 clients          *
  192.  ******************************************************/
  193.  
  194. NOLINE LONG SHUTDOWN::stopDB2Clients(VOID)
  195.  
  196. {
  197.      LONG returnCode;                                 // return code
  198.  
  199.      printf("Killing DB/2 connections and\n");         // show message
  200.  
  201.      printf("Stopping database manager...\n");        // show message
  202.  
  203.      sqlekill(&SQLCA);                                // DB2 clients
  204.  
  205.      returnCode = SQLCA.sqlcode;                      // get sqlcode
  206.  
  207.      if (returnCode == 0L) printf("Database complete!\n");
  208.  
  209.      else                  printf("DB/2 Error! (%ld)\n");
  210.  
  211.      return returnCode;                               // return code
  212. }
  213.  
  214.  /******************************************************
  215. *  SHUTDOWN::stopFileSystem - stop file system          *
  216.  ******************************************************/
  217.  
  218. NOLINE LONG SHUTDOWN::stopFileSystem(VOID)
  219.  
  220. {
  221.      LONG returnCode;                                 // return code
  222.  
  223.      printf("Stopping OS/2 file system...\n");        // show message
  224.  
  225.      returnCode = DosShutdown(0L);                    // file system
  226.  
  227.      if (returnCode == 0L) printf("Shutdown complete!\n");
  228.  
  229.      else                  printf("OS/2 Error! (%ld)\n");
  230.  
  231.      return returnCode;                               // return code
  232. }
  233.  
  234.  /******************************************************
  235. *  stopDB2Manager - stop DB2 manager                    *
  236.  ******************************************************/
  237.  
  238. NOLINE VOID stopDB2Manager(VOID *shutDown)
  239.  
  240. {
  241.      LONG returnCode = 0L;                            // return code
  242.  
  243.      returnCode = DosWaitEventSem(DB2Handle, -1L);    // wait forever
  244.  
  245.      returnCode = ((SHUTDOWN *) shutDown)->stopDB2Manager();
  246.  
  247.      returnCode = DosPostEventSem(DB2Finish);         // wake thread
  248. }
  249.  
  250.  /******************************************************
  251. *  stopDB2Clients - stop DB2 clients                    *
  252.  ******************************************************/
  253.  
  254. NOLINE VOID stopDB2Clients(VOID *shutDown)
  255.  
  256. {
  257.      LONG returnCode = 0L;                            // return code
  258.  
  259.      returnCode = DosWaitEventSem(DB2Handle, -1L);    // wait forever
  260.  
  261.      returnCode = ((SHUTDOWN *) shutDown)->stopDB2Clients();
  262.  
  263.      returnCode = DosPostEventSem(DB2Finish);         // wake thread
  264. }
  265.  
  266.  /******************************************************
  267. *  stopFileSystem - stop file system                    *
  268.  ******************************************************/
  269.  
  270. NOLINE VOID stopFileSystem(VOID *shutDown)
  271.  
  272. {
  273.      LONG returnCode = 0L;                            // return code
  274.  
  275.      returnCode = DosWaitEventSem(OS2Handle, -1L);    // wait forever
  276.  
  277.      returnCode = ((SHUTDOWN *) shutDown)->stopFileSystem();
  278.  
  279.      returnCode = DosPostEventSem(OS2Finish);         // wake thread
  280. }
  281.  
  282.