aboutsummaryrefslogtreecommitdiff
path: root/src/nice.c
blob: 1036ad10eeb73c541b2b08535bb2819891a761f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "cctk.h"
#include "cctk_WarnLevel.h"
#include "cctk_Arguments.h"
#include "cctk_Parameters.h"

/* manpage of getpriority:
   Including  <sys/time.h>  is  not  required  these days,
   but increases portability.
*/
#include <sys/time.h>
#include <errno.h>
#include <sys/resource.h>

void Nice_Renice(CCTK_ARGUMENTS)
{
    /* do not declare the arguments, it will cause an error */
    DECLARE_CCTK_PARAMETERS

    int old_nice, new_nice, nice_return_value;

    /* reset error variable */
    errno=0;
    /* save the old nice value */
    old_nice=getpriority(PRIO_PROCESS, 0);
    /* check errno */
    if (errno)
    {
        switch (errno)
        {
          case ESRCH  : /* this should never occur, since the 0 in getpriority
                           means this process, which should be found
                        */
                        CCTK_INFO("getpriority received ESTCH-error.");
                        break;
          case EINVAL : /* this should definitly not occur */
                        CCTK_INFO("getpriority received EINVAL-error.");
                        break;
          case EPERM  : /* this should never occur
                           It means, that the user of process did not match the
                           callers user */
                        CCTK_INFO("getpriority received EPERM-error.");
                        break;
          case EACCES : /* This cannot occur: non root user attemted to lower
                           priority (since we did not call setpriority here */
                        CCTK_INFO("getpriority received EACCES-error.");
                        break;
          default:      /* This should not occur, otherwise it is an
                           undocumented errno */
                        CCTK_VInfo(CCTK_THORNSTRING,
                                   "getpriority got undocumented errno %d",
                                   errno);
        }
        CCTK_WARN(0, "This should never occur.");
        /* in case CCTK_WARN did not stop the code */
        CCTK_Abort(cctkGH, 1);
    }

    errno=0;
    /* try to set the new priority */
    nice_return_value=setpriority(PRIO_PROCESS, 0, Nice_nice);
    /* check return value */
    if (nice_return_value!=0)
    {
        switch (errno)
        {
          case ESRCH  : /* this should never occur, since the 0 in setpriority
                           means this process, which should be found
                        */
                        CCTK_INFO("setpriority received ESTCH-error.");
                        CCTK_WARN(0, "This should never occur.");
                        break;
          case EINVAL : /* this should definitly not occur */
                        CCTK_INFO("setpriority received EINVAL-error.");
                        CCTK_WARN(0, "This should never occur.");
                        break;
          case EPERM  : /* this should never occur
                           It means, that the user of process did not match the
                           callers user */
                        CCTK_INFO("setpriority received EPERM-error.");
                        CCTK_WARN(0, "This should never occur.");
                        break;
          case EACCES : /* This can occur: non root user attemted to lower
                           priority
                        */
                        CCTK_WARN(0, "Only root can lower priorities.");
                        break;
          default:      /* This should not occur, otherwise it is an
                           undocumented errno */
                        CCTK_VInfo(CCTK_THORNSTRING,
                                   "setpriority got undocumented errno %d",
                                   errno);
        }
        /* in case CCTK_WARN did not stop the code */
        CCTK_Abort(cctkGH, 1);
    }

    errno=0;
    /* It should ok, but check the new priority */
    new_nice=getpriority(PRIO_PROCESS, 0);
    /* check errno */
    if (errno)
    {
        switch (errno)
        {
          case ESRCH  : /* this should never occur, since the 0 in getpriority
                           means this process, which should be found */
                        CCTK_INFO("getpriority received ESTCH-error.");
                        break;
          case EINVAL : /* this should definitly not occur */
                        CCTK_INFO("getpriority received EINVAL-error.");
                        break;
          case EPERM  : /* this should never occur
                           It means, that the user of process did not match the
                           callers user */
                        CCTK_INFO("getpriority received EPERM-error.");
                        break;
          case EACCES : /* This cannot occur: non root user attemted to lower
                           priority (since we did not call setpriority here
                        */
                        CCTK_INFO("getpriority received EACCES-error.");
                        break;
          default:      /* This should not occur, otherwise it is an
                           undocumented errno */
                        CCTK_VInfo(CCTK_THORNSTRING,
                                   "getpriority got undocumented errno %d",
                                   errno);
        }
        CCTK_WARN(0, "This should never occur.");
        /* in case CCTK_WARN did not stop the code */
        CCTK_Abort(cctkGH, 1);
    }

    if (new_nice == Nice_nice)
    {
        CCTK_VInfo(CCTK_THORNSTRING,"Process reniced from nice level %d to %d.",
                                    old_nice, new_nice);
    }
    else
    {
        /* this should never occur because of the error handling before */
        CCTK_VInfo(CCTK_THORNSTRING,
                   "Process reniced to nice level %d instead of %d.",
                   new_nice, Nice_nice);
    }

}