set_prior.c

Go to the documentation of this file.
00001 /*  @(#)set_prior.c    2.1  6/26/87  */
00002 /*
00003 **  Written by Dave Gerdes  5/1988
00004 **  US Army Construction Engineering Research Lab
00005 */
00006 
00007 /*
00008 **      For this file, and this file only I have set it up to act on
00009 **      special compiler defines taken from config.h.
00010 **
00011 **          Ideally we are trying to set it up so that priority is set
00012 **      only when needed, (i.e. when collecting points in stream mode.)
00013 **      To do this Collect_points calls set_priority()/unset_priority()
00014 **      before and after talking w/ the digitizer.
00015 **
00016 **      If you have defined MASSCOMP in the DIGIT_OPTS in makehead,
00017 **      digit will not run as root.  On Masscomps anyone can issue a
00018 **      nice () request  so there is no need to run set-user as root.
00019 **
00020 **      Written by Dave Gerdes  4/1988
00021 **
00022 */
00023 
00024 #include <grass/config.h>
00025 
00026 #include <stdio.h>
00027 
00028 #ifdef HAVE_SYS_TIME_H
00029 #include <sys/time.h>
00030 #endif
00031 
00032 #ifdef HAVE_SYS_RESOURCE_H
00033 #include <sys/resource.h>
00034 #endif
00035 
00036 #ifdef HAVE_SYS_TYPES_H
00037 #include <sys/types.h>
00038 #endif
00039 
00040 #ifdef HAVE_UNISTD_H
00041 #include <unistd.h>
00042 #endif
00043 
00044 /*
00045 *  set_priority() - this functions sets the raises the priority of the program.
00046 *    This is done to make sure no points are lost when digitizing in STREAM
00047 *    mode.   This means that this program will get more CPU time then any
00048 *    other program.  It will also take CPU time from graphics and ethernet.
00049 *    If graphics or ethernet seem to be degraded, the priority may have
00050 *    to be lessened.
00051 *    In other words the priority value may differ from machine to machine.
00052 */
00053 static int swap_re_uids (void);
00054 
00055 #ifndef PRIO_PROCESS
00056   #define    PRIO_PROCESS    0
00057 #endif
00058 
00059 /*  WARNING  -18 gets the most CPU time, priority > -18 gets less CPU time  */
00060 /*  If the graphics don't come out except after digitizing a line then your
00061 *   running too fast and the CPU isn't giving the graphics monitor a chance
00062 *   to update. -mh
00063 */
00064 
00065 #define    NORMAL        0
00066 #define    PRIORITY    -10
00067 
00068 /*  read warning above  */
00069 
00070 static  int  priority_set = 0;
00071 
00072 int init_priority (void)
00073 {
00074 
00075 #ifdef MASSCOMP
00076 /* MASSCOMP does not require SU to set priorities so just turn off SU */
00077     if (getuid() == 0 || geteuid() == 0)
00078         if (getuid() != 0)
00079             setuid (getuid());
00080         else
00081             setuid (geteuid ());
00082 #endif
00083     return 0;
00084 }
00085 
00086 /*  set_priority() returns 1 is already set and 0 if it had to set it.
00087 */
00088 
00089 int set_priority ()
00090 {
00091     if (priority_set)
00092         return(priority_set);
00093 
00094     swap_re_uids ();    /* set to root */
00095 
00096 #ifdef HAVE_SETPRIORITY
00097     setpriority (PRIO_PROCESS, (int) getpid (), PRIORITY);
00098 #else
00099 #ifdef HAVE_NICE
00100     nice (PRIORITY);
00101 #endif
00102 #endif /* HAVE_SETPRIORITY */
00103 
00104     swap_re_uids ();    /* and back to user */
00105 
00106     priority_set = 1;
00107     return(0);
00108 }
00109 
00110 int unset_priority ()
00111 {
00112     swap_re_uids ();    /* set to root */
00113 
00114 #ifdef HAVE_SETPRIORITY
00115     setpriority (PRIO_PROCESS, (int) getpid (), NORMAL);
00116 #else
00117 #ifdef HAVE_NICE
00118     nice (-(PRIORITY));
00119 #endif
00120 #endif /* HAVE_SETPRIORITY */
00121 
00122     swap_re_uids ();    /* and back to user */
00123 
00124     priority_set = 0;
00125     return(0);
00126 }
00127 
00128 static int swap_re_uids (void)
00129 {
00130 #ifndef __MINGW32__    
00131     static int flipflop = 0;
00132 
00133 #ifdef HAVE_SETREUID
00134     setreuid ((int)geteuid(), (int)getuid());
00135 #else
00136 #ifdef HAVE_SETRUID
00137 #ifdef HAVE_SETEUID
00138     /* should we be turning off interupts here? */
00139 
00140     /* first time thru  Effective will be 0 */
00141     if (! flipflop)
00142     {
00143         /* set Real to 0 */
00144         hold = getuid ();
00145         setruid (0);
00146         seteuid (hold);
00147 
00148         flipflop = 1;
00149     }
00150     else
00151     {
00152         /* set Effective to 0 */
00153         hold = geteuid ();
00154         seteuid (0);
00155         setruid (hold);
00156         flipflop = 0;
00157     }
00158 #endif /* HAVE_SETEUID */
00159 #endif /* HAVE_SETRUID */
00160 #endif /* HAVE_SETREUID */
00161 #endif /* __MINGW32__ */    
00162     return 0;
00163 }
00164 
00165 /* returns -1 if cannot create a user other than root */
00166 int set_uid_to_user ()
00167 {
00168 #ifndef __MINGW32__    
00169     uid_t user;
00170 
00171     user =  geteuid ();
00172     if (!user)
00173         user = getuid ();
00174     if (!user)
00175     {
00176         fprintf (stderr, "Set_uid_to_user () failed!\n");
00177         return (-1);
00178     }
00179 
00180     if (setuid (user) == -1) {
00181         fprintf (stderr, "Set_uid_to_user () failed!\n");
00182         return (-1);
00183     }
00184 #endif /* __MINGW32__ */   
00185     return (0);
00186 }
00187 
00188 
00189 /* leaving this around to point out that the masscomp
00190 **  getuid and geteuid were backwards
00191 */
00192 /*
00193 #ifdef XMASSCOMP
00194     user =  getuid ();
00195     if (!user)
00196         user = geteuid ();
00197 #else
00198     user =  geteuid ();
00199     if (!user)
00200         user = getuid ();
00201 #endif
00202 */

Generated on Wed Dec 19 14:59:06 2007 for GRASS by  doxygen 1.5.4