From: bruceperens Date: Fri, 14 Mar 2014 02:13:48 +0000 (+0000) Subject: Clean up, move "linux" facilities to "posix" where appropirate. X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=f769b4c237b6b3c9c473e6be69a2e992e8d93f8e;p=freetel-svn-tracking.git Clean up, move "linux" facilities to "posix" where appropirate. git-svn-id: https://svn.code.sf.net/p/freetel/code@1441 01035d8c-6547-0410-b346-abe4f91aad63 --- diff --git a/freedv-server/source/big_main.cpp b/freedv-server/source/big_main.cpp index a9e306f8..cbb2d29e 100644 --- a/freedv-server/source/big_main.cpp +++ b/freedv-server/source/big_main.cpp @@ -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 index 271e4848..00000000 --- a/freedv-server/source/platform/linux/privilege.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include -#include - -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 index 2c33ed85..00000000 --- a/freedv-server/source/platform/linux/scheduler.cpp +++ /dev/null @@ -1,80 +0,0 @@ -#include "drivers.h" -#include -#include -#include -#include -#include - -#ifdef _POSIX_PRIORITY_SCHEDULING -#include -#endif - -#ifdef _POSIX_MEMLOCK_RANGE -#include -#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 index 00000000..0d72b2c2 --- /dev/null +++ b/freedv-server/source/platform/posix/privilege.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include + +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 index 00000000..2c33ed85 --- /dev/null +++ b/freedv-server/source/platform/posix/scheduler.cpp @@ -0,0 +1,80 @@ +#include "drivers.h" +#include +#include +#include +#include +#include + +#ifdef _POSIX_PRIORITY_SCHEDULING +#include +#endif + +#ifdef _POSIX_MEMLOCK_RANGE +#include +#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; + } +}