kiranvizru / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

sched_setaffinity() and sched_getaffinity() implicitly declared #404

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
sched.h of glibc declares sched_setaffinity() and sched_getaffinity() when 
_GNU_SOURCE is defined. See `man sched_setaffinity`.

When _GNU_SOURCE is not defined anywhere, then compilation might fail:

psutil/_psutil_linux.c: In function ‘get_process_cpu_affinity’:
psutil/_psutil_linux.c:182:5: error: implicit declaration of function 
‘sched_getaffinity’ [-Werror=implicit-function-declaration]
     if (sched_getaffinity(pid, len, (cpu_set_t *)&mask) < 0) {
     ^
psutil/_psutil_linux.c: In function ‘set_process_cpu_affinity’:
psutil/_psutil_linux.c:202:5: error: implicit declaration of function 
‘sched_setaffinity’ [-Werror=implicit-function-declaration]
     if (sched_setaffinity(pid, len, (cpu_set_t *)&mask)) {
     ^

Fix:

--- psutil/_psutil_linux.c
+++ psutil/_psutil_linux.c
@@ -11,6 +11,7 @@
 #include <stdlib.h>
 #include <mntent.h>
 #include <utmp.h>
+#define _GNU_SOURCE
 #include <sched.h>
 #include <sys/syscall.h>
 #include <sys/sysinfo.h>

Original issue reported on code.google.com by Arfrever...@gmail.com on 29 Jun 2013 at 7:13

GoogleCodeExporter commented 9 years ago
Python.h indirectly includes features.h, which has _FEATURES_H check against 
multiple inclusion, so _GNU_SOURCE needs to be defined earlier.

Updated patch:
--- psutil/_psutil_linux.c
+++ psutil/_psutil_linux.c
@@ -6,6 +6,7 @@
  * Linux-specific functions.
  */

+#define _GNU_SOURCE
 #include <Python.h>
 #include <errno.h>
 #include <stdlib.h>

Original comment by Arfrever...@gmail.com on 30 Jun 2013 at 7:40

GoogleCodeExporter commented 9 years ago
Fixed in revision b304cfaf3b7c. Thanks for the patch.

Original comment by g.rodola on 4 Jul 2013 at 2:05

GoogleCodeExporter commented 9 years ago
First patch was not working. Please move '#define _GNU_SOURCE' before inclusion 
of Python.h.

Original comment by Arfrever...@gmail.com on 4 Jul 2013 at 2:39

GoogleCodeExporter commented 9 years ago
whops! right.
done, thanks.

Original comment by g.rodola on 6 Jul 2013 at 1:02

GoogleCodeExporter commented 9 years ago

Original comment by g.rodola on 11 Jul 2013 at 8:54

GoogleCodeExporter commented 9 years ago
I'm reopening this as right now we're getting a compiler warning because of 
this change:

/usr/include/python2.7/pyconfig.h:1139:0: warning: "_GNU_SOURCE" redefined 
[enabled by default]

Since I couldn't and still cannot reproduce the original issue, before I commit 
can you please tell me if the following patch breaks anything?

diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c
--- a/psutil/_psutil_linux.c
+++ b/psutil/_psutil_linux.c
@@ -6,22 +6,26 @@
  * Linux-specific functions.
  */

-#define _GNU_SOURCE
 #include <Python.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <mntent.h>
 #include <utmp.h>
-#include <sched.h>
 #include <sys/syscall.h>
 #include <sys/sysinfo.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <linux/unistd.h>

+// "man sched_setaffinity" dictates to include _GNU_SOURCE but this may
+// be already defined elsewhere
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <sched.h>
+
 #include "_psutil_linux.h"

-
 #define HAS_IOPRIO defined(__NR_ioprio_get) && defined(__NR_ioprio_set)

 #if HAS_IOPRIO

Original comment by g.rodola on 28 Aug 2013 at 4:24

GoogleCodeExporter commented 9 years ago
Closing this out again as I believe the warning is gone now.

Original comment by g.rodola on 2 Dec 2013 at 7:40