Clean up, move "linux" facilities to "posix" where appropirate.
authorbruceperens <bruceperens@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 14 Mar 2014 02:13:48 +0000 (02:13 +0000)
committerbruceperens <bruceperens@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 14 Mar 2014 02:13:48 +0000 (02:13 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@1441 01035d8c-6547-0410-b346-abe4f91aad63

freedv-server/source/big_main.cpp
freedv-server/source/platform/linux/privilege.cpp [deleted file]
freedv-server/source/platform/linux/scheduler.cpp [deleted file]
freedv-server/source/platform/posix/privilege.cpp [new file with mode: 0644]
freedv-server/source/platform/posix/scheduler.cpp [new file with mode: 0644]

index a9e306f8f14c6ab1eab9322b56305baf27381eac..cbb2d29ef684d19e627776461df121c51b66665e 100644 (file)
@@ -90,7 +90,7 @@ static const struct option options[] = {
 
 namespace FreeDV {
   const char * program_name = 0;
-};
+}
 
 int
 main(int argc, char * * argv)
diff --git a/freedv-server/source/platform/linux/privilege.cpp b/freedv-server/source/platform/linux/privilege.cpp
deleted file mode 100644 (file)
index 271e484..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-#include <iostream>
-#include <unistd.h>
-#include <limits.h>
-#include <grp.h>
-
-static const char insufficient_privilege_message[] =
-"Warning: The user running the program is not a member of the "
-"\"audio\" group.\n"
-"This may make it impossible to access audio devices.\n"
-"To fix, use one of these solutions:\n"
-"\tadd the user to the \"audio\" group.\n"
-"\tadd the setgid-audio privilege to the executable file\n"
-"with these commands, as root:\n"
-"\n"
-"\t\tchgrp audio filename\n"
-"\t\tchmod 2755 filename\n"
-"\n"
-"Alternatively, you can execute this program as root.\n\n";
-
-namespace FreeDV {
-  void
-  check_privileges()
-  {
-    const int uid = getuid();
-    const int euid = geteuid();
-
-    if ( uid == 0 || euid == 0 )
-      return;
-
-    const struct group *       audio = getgrnam("audio");
-    const int                  gid = getgid();
-    const int                  egid = getgid();
-
-    if ( audio ) {
-      gid_t    groups[NGROUPS_MAX];
-      int      size = sizeof(groups) / sizeof(*groups);
-      int      length;
-
-      if ( gid == audio->gr_gid || egid == audio->gr_gid )
-        return;
-
-      if ( (length = getgroups(size, groups)) > 0 ) {
-        for ( int i = 0; i < length; i++ ) {
-          if ( groups[i] == audio->gr_gid )
-           return;
-        }
-      }
-      std::cerr << insufficient_privilege_message << std::endl;
-    }
-  }
-}
diff --git a/freedv-server/source/platform/linux/scheduler.cpp b/freedv-server/source/platform/linux/scheduler.cpp
deleted file mode 100644 (file)
index 2c33ed8..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-#include "drivers.h"
-#include <string>
-#include <cstring>
-#include <iostream>
-#include <unistd.h>
-#include <errno.h>
-
-#ifdef _POSIX_PRIORITY_SCHEDULING
-#include <sched.h>
-#endif
-
-#ifdef _POSIX_MEMLOCK_RANGE
-#include <sys/mman.h>
-#endif
-
-
-
-namespace FreeDV {
-  static const char privilege_message_a[] =
-  "Warning: Insufficient privilege to set a real-time scheduling priority,\n"
-  "or to lock memory.\n"
-  "This could cause audio to be interrupted while other programs use the CPU.\n" 
-  "To fix: As root, run\n"
-  "\n"
-  "\tsetcap cap_sys_nice+ep "; /* program_name */
-  static const char privilege_message_b[] = "\n\tsetcap cap_ipc_lock+ep ";
-  /* program_name */
-  static const char privilege_message_c[] = "\n\n"
-  "That will allow you to use a real-time scheduling priority and locked\n"
-  "memory while running as any user.\n"
-  "Alternatively, you can execute this program as root.\n\n";
-  
-  static const char old_kernel_message[] =
-  "This kernel doesn't seem to have real-time facilities or memory locking.\n"
-  "If audio is sometimes interrupted, try a newer kernel.\n";
-
-  void
-  set_scheduler()
-  {
-    bool insufficient_privilege = false;
-    bool old_kernel = false;
-
-#ifdef _POSIX_PRIORITY_SCHEDULING
-    // Put this process on the round-robin realtime scheduling queue at the
-    // minimum priority. Other real-time processes (perhaps portaudio) can
-    // run with a higher priority than this, but this process will run at
-    // a higher priority than all normal processes.
-    sched_param        p;
-    int                policy = SCHED_RR;
-
-    p.sched_priority = sched_get_priority_min(policy);
-    const int result = sched_setscheduler(0, policy, &p);
-    if ( result < 0 ) {
-      std::cerr << "sched_setscheduler: " << strerror(errno) << std::endl;
-      if ( errno == EINVAL )
-         old_kernel = true;
-      if ( errno == EPERM )
-         insufficient_privilege = true;
-    }
-#endif
-
-#ifdef _POSIX_MEMLOCK_RANGE
-    if ( mlockall(MCL_CURRENT|MCL_FUTURE) < 0 ) {
-      std::cerr << "mlockall: " << strerror(errno) << std::endl;
-      if ( errno == EINVAL )
-         old_kernel = true;
-      if ( errno == EPERM )
-         insufficient_privilege = true;
-    }
-#endif
-    if ( old_kernel )
-      std::cerr << old_kernel_message;
-    else if ( insufficient_privilege )
-      std::cerr << privilege_message_a;
-      std::cerr << program_name;
-      std::cerr << privilege_message_b;
-      std::cerr << program_name;
-      std::cerr << privilege_message_c;
-  }
-}
diff --git a/freedv-server/source/platform/posix/privilege.cpp b/freedv-server/source/platform/posix/privilege.cpp
new file mode 100644 (file)
index 0000000..0d72b2c
--- /dev/null
@@ -0,0 +1,51 @@
+#include <iostream>
+#include <unistd.h>
+#include <limits.h>
+#include <grp.h>
+
+static const char insufficient_privilege_message[] =
+"Warning: The user running the program is not a member of the "
+"\"audio\" group.\n"
+"This may make it impossible to access audio devices.\n"
+"To fix, use one of these solutions:\n"
+"\tadd the user to the \"audio\" group.\n"
+"\tadd the setgid-audio privilege to the executable file\n"
+"with these commands, as root:\n"
+"\n"
+"\t\tchgrp audio filename\n"
+"\t\tchmod 2755 filename\n"
+"\n"
+"Alternatively, you can execute this program as root.\n\n";
+
+namespace FreeDV {
+  void
+  check_privileges()
+  {
+    const uid_t uid = getuid();
+    const uid_t euid = geteuid();
+
+    if ( uid == 0 || euid == 0 )
+      return;
+
+    const struct group *       audio = getgrnam("audio");
+    const gid_t                        gid = getgid();
+    const gid_t                        egid = getgid();
+
+    if ( audio ) {
+      gid_t    groups[NGROUPS_MAX];
+      int      size = sizeof(groups) / sizeof(*groups);
+      int      length;
+
+      if ( gid == audio->gr_gid || egid == audio->gr_gid )
+        return;
+
+      if ( (length = getgroups(size, groups)) > 0 ) {
+        for ( unsigned int i = 0; i < length; i++ ) {
+          if ( groups[i] == audio->gr_gid )
+           return;
+        }
+      }
+      std::cerr << insufficient_privilege_message << std::endl;
+    }
+  }
+}
diff --git a/freedv-server/source/platform/posix/scheduler.cpp b/freedv-server/source/platform/posix/scheduler.cpp
new file mode 100644 (file)
index 0000000..2c33ed8
--- /dev/null
@@ -0,0 +1,80 @@
+#include "drivers.h"
+#include <string>
+#include <cstring>
+#include <iostream>
+#include <unistd.h>
+#include <errno.h>
+
+#ifdef _POSIX_PRIORITY_SCHEDULING
+#include <sched.h>
+#endif
+
+#ifdef _POSIX_MEMLOCK_RANGE
+#include <sys/mman.h>
+#endif
+
+
+
+namespace FreeDV {
+  static const char privilege_message_a[] =
+  "Warning: Insufficient privilege to set a real-time scheduling priority,\n"
+  "or to lock memory.\n"
+  "This could cause audio to be interrupted while other programs use the CPU.\n" 
+  "To fix: As root, run\n"
+  "\n"
+  "\tsetcap cap_sys_nice+ep "; /* program_name */
+  static const char privilege_message_b[] = "\n\tsetcap cap_ipc_lock+ep ";
+  /* program_name */
+  static const char privilege_message_c[] = "\n\n"
+  "That will allow you to use a real-time scheduling priority and locked\n"
+  "memory while running as any user.\n"
+  "Alternatively, you can execute this program as root.\n\n";
+  
+  static const char old_kernel_message[] =
+  "This kernel doesn't seem to have real-time facilities or memory locking.\n"
+  "If audio is sometimes interrupted, try a newer kernel.\n";
+
+  void
+  set_scheduler()
+  {
+    bool insufficient_privilege = false;
+    bool old_kernel = false;
+
+#ifdef _POSIX_PRIORITY_SCHEDULING
+    // Put this process on the round-robin realtime scheduling queue at the
+    // minimum priority. Other real-time processes (perhaps portaudio) can
+    // run with a higher priority than this, but this process will run at
+    // a higher priority than all normal processes.
+    sched_param        p;
+    int                policy = SCHED_RR;
+
+    p.sched_priority = sched_get_priority_min(policy);
+    const int result = sched_setscheduler(0, policy, &p);
+    if ( result < 0 ) {
+      std::cerr << "sched_setscheduler: " << strerror(errno) << std::endl;
+      if ( errno == EINVAL )
+         old_kernel = true;
+      if ( errno == EPERM )
+         insufficient_privilege = true;
+    }
+#endif
+
+#ifdef _POSIX_MEMLOCK_RANGE
+    if ( mlockall(MCL_CURRENT|MCL_FUTURE) < 0 ) {
+      std::cerr << "mlockall: " << strerror(errno) << std::endl;
+      if ( errno == EINVAL )
+         old_kernel = true;
+      if ( errno == EPERM )
+         insufficient_privilege = true;
+    }
+#endif
+    if ( old_kernel )
+      std::cerr << old_kernel_message;
+    else if ( insufficient_privilege )
+      std::cerr << privilege_message_a;
+      std::cerr << program_name;
+      std::cerr << privilege_message_b;
+      std::cerr << program_name;
+      std::cerr << privilege_message_c;
+  }
+}