xref: /reactos/base/applications/shutdown/misc.c (revision f34a30fe)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS shutdown/logoff utility
4  * FILE:            base/applications/shutdown/misc.c
5  * PURPOSE:         Misc. functions used for the shutdown utility
6  * PROGRAMMERS:     Lee Schroeder
7  */
8 
9 #include "precomp.h"
10 
11 #include <stdio.h>
12 
13 const DWORD defaultReason = SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER;
14 
15 REASON shutdownReason[] =
16 {
17     {L"U" , 0,  0, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER},                                             /* Other (Unplanned) */
18     {L"E" , 0,  0, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER},                                             /* Other (Unplanned) */
19     {L"EP", 0,  0, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED},                 /* Other (Planned) */
20     {L"U" , 0,  5, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_HUNG},                                              /* Other Failure: System Unresponsive */
21     {L"E" , 1,  1, SHTDN_REASON_MAJOR_HARDWARE | SHTDN_REASON_MINOR_MAINTENANCE},                                    /* Hardware: Maintenance (Unplanned) */
22     {L"EP", 1,  1, SHTDN_REASON_MAJOR_HARDWARE | SHTDN_REASON_MINOR_MAINTENANCE | SHTDN_REASON_FLAG_PLANNED},        /* Hardware: Maintenance (Planned) */
23     {L"E" , 1,  2, SHTDN_REASON_MAJOR_HARDWARE | SHTDN_REASON_MINOR_INSTALLATION},                                   /* Hardware: Installation (Unplanned) */
24     {L"EP", 1,  2, SHTDN_REASON_MAJOR_HARDWARE | SHTDN_REASON_MINOR_INSTALLATION | SHTDN_REASON_FLAG_PLANNED},       /* Hardware: Installation (Planned) */
25     {L"P" , 2,  3, SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_UPGRADE | SHTDN_REASON_FLAG_PLANNED},     /* Operating System: Upgrade (Planned) */
26     {L"E" , 2,  4, SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_RECONFIG},                                /* Operating System: Reconfiguration (Unplanned) */
27     {L"EP", 2,  4, SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_RECONFIG | SHTDN_REASON_FLAG_PLANNED},    /* Operating System: Reconfiguration (Planned) */
28     {L"P" , 2, 16, SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_RECONFIG | SHTDN_REASON_FLAG_PLANNED},    /* Operating System: Service pack (Planned) */
29     {L"U" , 2, 17, SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_HOTFIX},                                  /* Operating System: Hotfix (Unplanned) */
30     {L"P" , 2, 17, SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_HOTFIX | SHTDN_REASON_FLAG_PLANNED},      /* Operating System: Hotfix (Planned) */
31     {L"U" , 2, 18, SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_SECURITYFIX},                             /* Operating System: Security fix (Unplanned) */
32     {L"P" , 2, 18, SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_SECURITYFIX | SHTDN_REASON_FLAG_PLANNED}, /* Operating System: Security fix (Planned) */
33     {L"E" , 4,  1, SHTDN_REASON_MAJOR_APPLICATION | SHTDN_REASON_MINOR_MAINTENANCE},                                 /* Application: Maintenance (Unplanned) */
34     {L"EP", 4,  1, SHTDN_REASON_MAJOR_APPLICATION | SHTDN_REASON_MINOR_MAINTENANCE | SHTDN_REASON_FLAG_PLANNED},     /* Application: Maintenance (Planned) */
35     {L"EP", 4,  2, SHTDN_REASON_MAJOR_APPLICATION | SHTDN_REASON_MINOR_INSTALLATION | SHTDN_REASON_FLAG_PLANNED},    /* Application: Installation (Planned) */
36     {L"E" , 4,  5, SHTDN_REASON_MAJOR_APPLICATION | SHTDN_REASON_MINOR_HUNG},                                        /* Application: Unresponsive */
37     {L"E" , 4,  6, SHTDN_REASON_MAJOR_APPLICATION | SHTDN_REASON_MINOR_UNSTABLE},                                    /* Application: Unstable */
38     {L"U" , 5, 15, SHTDN_REASON_MAJOR_SYSTEM | SHTDN_REASON_MINOR_BLUESCREEN},                                       /* System Failure: Stop Error */
39     {L"E" , 5, 19, SHTDN_REASON_MAJOR_SYSTEM | SHTDN_REASON_MINOR_SECURITY},                                         /* Security Issue */
40     {L"U" , 5, 19, SHTDN_REASON_MAJOR_SYSTEM | SHTDN_REASON_MINOR_SECURITY},                                         /* Security Issue */
41     {L"EP", 5, 19, SHTDN_REASON_MAJOR_SYSTEM | SHTDN_REASON_MINOR_SECURITY | SHTDN_REASON_FLAG_PLANNED},             /* Security Issue (Planned) */
42     {L"E" , 5, 20, SHTDN_REASON_MAJOR_SYSTEM | SHTDN_REASON_MINOR_NETWORK_CONNECTIVITY},                             /* Loss of Network Connectivity (Unplanned) */
43     {L"U" , 6, 11, SHTDN_REASON_MAJOR_POWER | SHTDN_REASON_MINOR_CORDUNPLUGGED},                                     /* Power Failure: Cord Unplugged */
44     {L"U" , 6, 12, SHTDN_REASON_MAJOR_POWER | SHTDN_REASON_MINOR_ENVIRONMENT},                                       /* Power Failure: Environment */
45     {L"P" , 7,  0, SHTDN_REASON_MAJOR_POWER | SHTDN_REASON_MINOR_ENVIRONMENT}                                        /* Legacy API shutdown (Planned) */
46 };
47 
48 /*
49  * This function parses the reason code to a usable format that will specify
50  * why the user wants to shut the computer down. Although this is used for
51  * both client and server environments, use of a reason code is more important
52  * in a server environment since servers are supposed to be on all the time
53  * for easier access.
54  */
ParseReasonCode(LPCWSTR code)55 DWORD ParseReasonCode(LPCWSTR code)
56 {
57     PREASON reasonptr;
58     int majorCode = 0;
59     int minorCode = 0;
60     LPWSTR tmpPrefix = NULL;
61     size_t codeSize;
62 
63     /* If no reason code is specified, use "Other (Unplanned)" as the default option */
64     if(code == NULL)
65     {
66         return defaultReason;
67     }
68     else
69     {
70         /* Store the size of the code so we can use it later */
71         codeSize = (size_t)wcslen(code);
72 
73         /* A colon cannot be the first or last character in the reason code */
74         if ((code[0] == L':') || (code[codeSize] == L':'))
75         {
76             return defaultReason;
77         }
78 
79         /* The minimum length that a reason code can be is 5-7 characters in length */
80         if ((codeSize < 5) || (codeSize > 7))
81         {
82             return defaultReason;
83         }
84 
85         /* TODO: Add code for reason parsing here. */
86 
87         /* Make sure that the major and minor codes are within size limits */
88         if ((majorCode > 7 ) || (majorCode < 0) ||
89             (minorCode > 20) || (minorCode < 0))
90         {
91             return defaultReason;
92         }
93 
94         /* Figure out what flags to return */
95         for (reasonptr = shutdownReason ; reasonptr->prefix ; reasonptr++)
96         {
97             if ((majorCode == reasonptr->major) &&
98                 (minorCode == reasonptr->minor) &&
99                 (_wcsicmp(tmpPrefix, reasonptr->prefix) != 0))
100             {
101                 return reasonptr->flag;
102             }
103         }
104     }
105 
106     return defaultReason;
107 }
108 
109 /* Writes the last error as both text and error code to the console */
DisplayError(DWORD dwError)110 VOID DisplayError(DWORD dwError)
111 {
112     ConMsgPuts(StdErr, FORMAT_MESSAGE_FROM_SYSTEM,
113                NULL, dwError, LANG_USER_DEFAULT);
114     ConPrintf(StdErr, L"Error code: %lu\n", dwError);
115 }
116 
117 /* EOF */
118