From de060c4f15ee62ac074d6fad2b66426673a49398 Mon Sep 17 00:00:00 2001 From: knarf Date: Fri, 20 Feb 2004 14:26:34 +0000 Subject: - move from AlphaThorns git-svn-id: http://svn.cactuscode.org/arrangements/CactusUtils/Nice/trunk@2 ca4eca94-0e4f-0410-9877-d0c6952cf786 --- src/nice.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/nice.c (limited to 'src/nice.c') diff --git a/src/nice.c b/src/nice.c new file mode 100644 index 0000000..1036ad1 --- /dev/null +++ b/src/nice.c @@ -0,0 +1,147 @@ +#include "cctk.h" +#include "cctk_WarnLevel.h" +#include "cctk_Arguments.h" +#include "cctk_Parameters.h" + +/* manpage of getpriority: + Including is not required these days, + but increases portability. +*/ +#include +#include +#include + +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); + } + +} + -- cgit v1.2.3