home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / downsv.exe / DOWNSERV.C next >
Text File  |  1991-08-01  |  9KB  |  332 lines

  1. /*▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄*/
  2. /*▌                                                                          ▐*/
  3. /*▌      DOWNSERV NLM                                                        ▐*/
  4. /*▌                                                                          ▐*/
  5. /*▌      NetWare Loadable Module to schedule shutdown of a NetWare 386       ▐*/
  6. /*▌      file server at a later time.                                        ▐*/      
  7. /*▌                                                                          ▐*/
  8. /*▌      written by:    Morgan Adair                                         ▐*/
  9. /*▌      date:          15 July 1991                                         ▐*/
  10. /*▌                                                                          ▐*/
  11. /*▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀*/
  12.  
  13. #include <advanced.h>
  14. #include <process.h>
  15. #include <time.h>
  16. #include <nwbindry.h>
  17. #include <errno.h>
  18.  
  19. #define DELAY_AMOUNT 9
  20. #define TIME_FOR_FIRST_WARNING 10
  21. #define TIME_FOR_SECOND_WARNING 5
  22.  
  23. LONG        AESEventTag;
  24. struct    AESProcessStructure     ClockEvent, WarningEvent1,
  25.                                         WarningEvent2, ShutdownEvent;
  26. LONG        MyNLMHandle;
  27. int        myThreadID;
  28. int        timeRemaining = -1;
  29. int        shutdownTime = FALSE;
  30. int        firstTry = TRUE;
  31.  
  32. void Usage(void);
  33. LONG TimeToTicks(char *timeIn);
  34. void WakeUp(void);
  35. void SetTimeRemaining(void);
  36. void SetShutdownFlag(void);
  37. void ExitProcedure(void);
  38. void ShutdownServer(void);
  39. void WarnUsers(void);
  40.  
  41. void main(int argc, char *argv[])
  42. {
  43.     time_t    currentTime;
  44.     char        timeBuf[30];
  45.     LONG        nTicks;
  46.     LONG        warningTimeInTicks;
  47.     int        ccode;
  48.  
  49.     if (argc != 2) {
  50.         printf("Incorrect number of command line parameters\n");
  51.         Usage();
  52.         exit(-1);
  53.     }
  54.  
  55.     SecondsToTicks((TIME_FOR_FIRST_WARNING * 60L), 0L, &warningTimeInTicks);
  56.  
  57.     nTicks = TimeToTicks(argv[1]);
  58.     if (nTicks == -1L) {
  59.         printf("Invalid time entered");
  60.         exit(-1);
  61.     } else if (nTicks < warningTimeInTicks) {
  62.         printf("Shutdown time must be more than %d minutes away", TIME_FOR_FIRST_WARNING);
  63.         exit(-1);
  64.     }
  65.  
  66.     myThreadID = GetThreadID();
  67.     MyNLMHandle = GetNLMHandle();
  68.  
  69.     /* AES Event Resource Tag */
  70.     AESEventTag = AllocateResourceTag(MyNLMHandle, (BYTE *)"AES Events",
  71.                                                     AESProcessSignature);
  72.     if (AESEventTag == NULL) {
  73.         printf("Unable to get AES Event resource tag\n");
  74.         exit(-1);
  75.     }
  76.  
  77.     /* Register exit procedure to return AES Event Resource Tag */
  78.     if (atexit(ExitProcedure) == EFAILURE) {
  79.         printf("Unable to register exit procedure:\n");
  80.         printf("     AES Events resources will not be released when NLM is unloaded\n");
  81.     }
  82.  
  83.     /* Initialize AESProcessStructure for screen clock */
  84.     ClockEvent.AWakeUpDelayAmount = DELAY_AMOUNT;
  85.     ClockEvent.AProcessToCall = WakeUp;
  86.     ClockEvent.ARTag = AESEventTag;
  87.  
  88.     /* Initialize AESProcessStructure for first warning */
  89.     WarningEvent1.AWakeUpDelayAmount = nTicks - warningTimeInTicks;
  90.     WarningEvent1.AProcessToCall = SetTimeRemaining;
  91.     WarningEvent1.ARTag = AESEventTag;
  92.  
  93.     /* Schedule first warning */
  94.     ScheduleSleepAESProcessEvent(&WarningEvent1);
  95.  
  96.     SecondsToTicks((TIME_FOR_SECOND_WARNING * 60L), 0L, &warningTimeInTicks);
  97.  
  98.     /* Initialize AESProcessStructure for second warning */
  99.     WarningEvent2.AWakeUpDelayAmount = nTicks - warningTimeInTicks;
  100.     WarningEvent2.AProcessToCall = SetTimeRemaining;
  101.     WarningEvent2.ARTag = AESEventTag;
  102.  
  103.     /* Schedule second warning */
  104.     ScheduleSleepAESProcessEvent(&WarningEvent2);
  105.  
  106.     /* Initialize AESProcessStructure for shutdown procedure */
  107.     ShutdownEvent.AWakeUpDelayAmount = nTicks;
  108.     ShutdownEvent.AProcessToCall = SetShutdownFlag;
  109.     ShutdownEvent.ARTag = AESEventTag;
  110.  
  111.     /* Schedule shutdown */
  112.     ScheduleSleepAESProcessEvent(&ShutdownEvent);
  113.  
  114.     HideInputCursor();
  115.  
  116.     gotoxy(10, 10);
  117.     printf("Server shutdown scheduled for:  %s:00", argv[1]);
  118.  
  119.     do {
  120.         /* Get the time */
  121.         currentTime = time(NULL);
  122.         if (!currentTime)
  123.             printf("Unable to get current time\n");
  124.  
  125.         else {
  126.             /* Display time */
  127.             strftime(timeBuf, 28, "%H:%M:%S     (%I:%M:%S %p)", localtime(¤tTime));
  128.             gotoxy(27, 12);
  129.             printf(           "Current time:  %s", timeBuf);
  130.         }
  131.  
  132.         /* Check flags and take appropriate actions */
  133.         if ((timeRemaining == TIME_FOR_FIRST_WARNING) || 
  134.              (timeRemaining == TIME_FOR_SECOND_WARNING) ||
  135.              (timeRemaining == 0))
  136.             WarnUsers();
  137.  
  138.         if (shutdownTime == TRUE)
  139.             ShutdownServer();
  140.  
  141.         /* Schedule a wakeup call */
  142.         ScheduleSleepAESProcessEvent(&ClockEvent);
  143.  
  144.         /* Go to sleep */
  145.         ccode = SuspendThread(myThreadID);
  146.         if (ccode != ESUCCESS)
  147.             printf("Suspend thread failed, ccode = %d\r\n", ccode);
  148.  
  149.     } while (1);
  150. }
  151.  
  152. void Usage (void)
  153. {
  154.     ConsolePrintf("DOWNSERV:  Schedule server shutdown\n");
  155.     ConsolePrintf("Usage: load downserv <hh:mm>\n");
  156.     ConsolePrintf("Uses 24-hour clock (22:30 = 10:30 pm)\n");
  157. }
  158.  
  159. LONG TimeToTicks(char *timeIn)
  160. {
  161.     char    hourStr[3];
  162.     char    minStr[3];
  163.     int    hour, min;
  164.     LONG    nSec;
  165.     LONG    nTicks;
  166.     time_t        timeInSec;
  167.     struct tm    *currentTime;
  168.  
  169.     if ((strlen(timeIn) < 4) || (strlen(timeIn) > 5)) {
  170.         printf("Invalid time specification\n");
  171.         Usage();
  172.         exit(-1);
  173.     }
  174.  
  175.     if (strlen(timeIn) == 4) {
  176.         if (timeIn[1] != ':') {
  177.             printf("Missing ':' in time specification\n");
  178.             return((LONG)-1L);
  179.         }
  180.         hourStr[0] = timeIn[0];
  181.         hourStr[1] = '\0';
  182.         minStr[0] = timeIn[2];
  183.         minStr[1] = timeIn[3];
  184.         minStr[2] = '\0';
  185.  
  186.     } else {
  187.         if (timeIn[2] != ':') {
  188.             printf("Missing ':' in time specification\n");
  189.             return((LONG)-1L);
  190.         }
  191.         hourStr[0] = timeIn[0];
  192.         hourStr[1] = timeIn[1];
  193.         hourStr[2] = '\0';
  194.         minStr[0] = timeIn[3];
  195.         minStr[1] = timeIn[4];
  196.         minStr[2] = '\0';
  197.     }
  198.  
  199.     hour = atoi(hourStr);
  200.     min = atoi(minStr);
  201.  
  202.     if ((hour > 23) || (min > 59)) {
  203.         printf("Hour must be 0-23 and minutes must be 0-59\n");
  204.         return((LONG)-1L);
  205.     }
  206.     timeInSec = time(NULL);
  207.     currentTime = localtime(&timeInSec);
  208.  
  209.     if ((*currentTime).tm_hour > hour)
  210.         hour = 24 - (*currentTime).tm_hour + hour;
  211.     else
  212.         hour = hour - (*currentTime).tm_hour;
  213.  
  214.     nSec = 3600 * hour;
  215.  
  216.     nSec = nSec + (60 * min);
  217.     nSec = nSec - (60 * (*currentTime).tm_min);
  218.  
  219.     SecondsToTicks(nSec, 0L, &nTicks);
  220.     return(nTicks);
  221. }
  222.  
  223. void WakeUp(void)
  224. {
  225.     int    ccode;
  226.  
  227.     ccode = ResumeThread(myThreadID);
  228.     if (ccode != ESUCCESS)
  229.         ConsolePrintf("DOWNSERV: ResumeThread failed, ccode = %d\r\n", ccode);
  230. }
  231.  
  232. void ExitProcedure(void)
  233. {
  234.     CancelSleepAESProcessEvent(&ClockEvent);
  235.     CancelSleepAESProcessEvent(&WarningEvent1);
  236.     CancelSleepAESProcessEvent(&WarningEvent2);
  237.     CancelSleepAESProcessEvent(&ShutdownEvent);
  238. }
  239.  
  240. void SetTimeRemaining(void)
  241. {
  242.     if (timeRemaining == -1)
  243.         timeRemaining = TIME_FOR_FIRST_WARNING;
  244.     else if (timeRemaining == TIME_FOR_FIRST_WARNING - 1)
  245.         timeRemaining = TIME_FOR_SECOND_WARNING;
  246.     else
  247.         timeRemaining = 0;
  248. }
  249.  
  250. void SetShutdownFlag(void)
  251. {
  252.     shutdownTime = TRUE;
  253. }
  254.  
  255. void WarnUsers(void)
  256. {
  257.     int    ccode;
  258.     char    serverName[49];
  259.     char    buffer[80];
  260.  
  261.     GetFileServerName(0, serverName);
  262.     sprintf(buffer, "%s shutting down in %d min",
  263.                             serverName, timeRemaining);
  264.      ConsolePrintf("DOWNSERV NLM: Server shutdown in %d minutes\n", timeRemaining);
  265.  
  266.     gotoxy(20,15);
  267.     if (timeRemaining == TIME_FOR_FIRST_WARNING)
  268.         printf("STATUS:  Broadcasting first server shutdown warning");
  269.     else if (timeRemaining == TIME_FOR_SECOND_WARNING)
  270.         printf("STATUS:  Broadcasting second server shutdown warning");
  271.  
  272.     /* Send warning to all connected stations */
  273.     ccode = SendConsoleBroadcast(buffer, 0, (WORD *)NULL);
  274.     if (ccode != ESUCCESS) {
  275.         gotoxy(20,15);
  276.         printf("STATUS:  Unable to notify connections of server shutdown. ccode = %x\n", ccode);
  277.     }
  278.  
  279.     timeRemaining--;
  280. }
  281.  
  282. void ShutdownServer(void)
  283. {
  284.     char    serverName[49];
  285.     char    buffer[80];
  286.     int    ccode;
  287.     LONG    oneMinInTicks;
  288.  
  289.     /* Clear old status messages, if any */
  290.     gotoxy(20,15);
  291.     printf("\n\n");
  292.  
  293.     /* Do not force server to shut down if files are open */
  294.     if (firstTry) {
  295.         ccode = DownFileServer(0);
  296.         firstTry = FALSE;
  297.  
  298.         if (ccode == ESUCCESS)
  299.             exit(0);
  300.         else {
  301.             shutdownTime = FALSE;
  302.  
  303.             gotoxy(20,15);
  304.             printf("STATUS:  Files open--can't shut down.  ccode = %x.  Force shutdown in 1 minute.\n", ccode);
  305.  
  306.             SecondsToTicks(60L, 0L, &oneMinInTicks);
  307.  
  308.             /* Reset time in AESProcessStructure for shutdown procedure */
  309.             ShutdownEvent.AWakeUpDelayAmount = oneMinInTicks;
  310.  
  311.             /* Reschedule shutdown */
  312.             ScheduleSleepAESProcessEvent(&ShutdownEvent);
  313.  
  314.             /* Send warning to all connected stations */
  315.             sprintf(buffer, "%s shutting down. Logout now.", serverName);
  316.             ccode = SendConsoleBroadcast(buffer, 0, (WORD *)NULL);
  317.             if (ccode != ESUCCESS)
  318.                 printf("Unable to notify connections of server shutdown. ccode = %x\n", ccode);
  319.         }
  320.  
  321.     } else {
  322.         ccode = DownFileServer(1);
  323.         if (ccode == ESUCCESS)
  324.             exit(0);
  325.         else {
  326.             gotoxy(20,15);
  327.             printf("STATUS:  Unable to shut down.  ccode = %xh.\n", ccode);
  328.         }
  329.     }
  330. }
  331.  
  332.