diff srogue/mdport.c @ 120:d6b7c3fb37ea

srogue: add and use more md_* portable functions. Privileges and memory usage checks are now more portable.
author John "Elwin" Edwards
date Fri, 02 May 2014 15:06:23 -0700
parents 458df24e973d
children e6c8652473fe
line wrap: on
line diff
--- a/srogue/mdport.c	Wed Apr 30 14:46:30 2014 -0700
+++ b/srogue/mdport.c	Fri May 02 15:06:23 2014 -0700
@@ -413,6 +413,16 @@
 #endif
 }
 
+gid_t
+md_getgid(void)
+{
+#ifdef HAVE_GETGID
+    return( getgid() );
+#else
+    return(42);
+#endif
+}
+
 pid_t
 md_getpid(void)
 {
@@ -1509,3 +1519,51 @@
 
 #endif
 
+long
+md_memused(void)
+{
+#ifdef _WIN32
+    MEMORYSTATUS stat;
+
+    GlobalMemoryStatus(&stat);
+
+    return((long)stat.dwTotalPageFile);
+#else
+    return( (long)sbrk(0) );
+#endif
+}
+
+void
+md_droppriv(void)
+{
+#if defined(HAVE_GETUID)
+	uid_t realuid = getuid();
+
+#if defined(HAVE_SETRESUID)
+    if (setresuid(-1, realuid, realuid) != 0) {
+#elif defined (HAVE_SETREUID) 
+    if (setreuid(realuid, realuid) != 0) {
+#elif defined (HAVE_SETUID)
+    if (setuid(realuid) != 0) {
+#else
+    if (0) {
+#endif
+	printf("Cannot change to effective uid: %d\n", realuid);
+	exit(1);
+    }
+#endif
+}
+
+extern uid_t playuid;
+extern gid_t playgid;
+
+void
+md_resetpriv(void)
+{
+#if defined (HAVE_SETUID)
+	setuid(playuid);
+#endif
+#if defined (HAVE_SETGID)
+	setgid(playgid);
+#endif
+}