725 Py_XDECREF(rfdslist); |
725 Py_XDECREF(rfdslist); |
726 return NULL; |
726 return NULL; |
727 } |
727 } |
728 |
728 |
729 #endif /* CMSG_LEN */ |
729 #endif /* CMSG_LEN */ |
|
730 |
|
731 #if defined(HAVE_SETPROCTITLE) |
|
732 /* setproctitle is the first choice - available in FreeBSD */ |
|
733 #define SETPROCNAME_USE_SETPROCTITLE |
|
734 #elif (defined(__linux__) || defined(__APPLE__)) && PY_MAJOR_VERSION == 2 |
|
735 /* rewrite the argv buffer in place - works in Linux and OS X. Py_GetArgcArgv |
|
736 * in Python 3 returns the copied wchar_t **argv, thus unsupported. */ |
|
737 #define SETPROCNAME_USE_ARGVREWRITE |
|
738 #else |
|
739 #define SETPROCNAME_USE_NONE |
|
740 #endif |
|
741 |
|
742 #ifndef SETPROCNAME_USE_NONE |
|
743 static PyObject *setprocname(PyObject *self, PyObject *args) |
|
744 { |
|
745 const char *name = NULL; |
|
746 if (!PyArg_ParseTuple(args, "s", &name)) |
|
747 return NULL; |
|
748 |
|
749 #if defined(SETPROCNAME_USE_SETPROCTITLE) |
|
750 setproctitle("%s", name); |
|
751 #elif defined(SETPROCNAME_USE_ARGVREWRITE) |
|
752 { |
|
753 static char *argvstart = NULL; |
|
754 static size_t argvsize = 0; |
|
755 if (argvstart == NULL) { |
|
756 int argc = 0, i; |
|
757 char **argv = NULL; |
|
758 char *argvend; |
|
759 extern void Py_GetArgcArgv(int *argc, char ***argv); |
|
760 Py_GetArgcArgv(&argc, &argv); |
|
761 |
|
762 /* Check the memory we can use. Typically, argv[i] and |
|
763 * argv[i + 1] are continuous. */ |
|
764 argvend = argvstart = argv[0]; |
|
765 for (i = 0; i < argc; ++i) { |
|
766 if (argv[i] > argvend || argv[i] < argvstart) |
|
767 break; /* not continuous */ |
|
768 size_t len = strlen(argv[i]); |
|
769 argvend = argv[i] + len + 1 /* '\0' */; |
|
770 } |
|
771 if (argvend > argvstart) /* sanity check */ |
|
772 argvsize = argvend - argvstart; |
|
773 } |
|
774 |
|
775 if (argvstart && argvsize > 1) { |
|
776 int n = snprintf(argvstart, argvsize, "%s", name); |
|
777 if (n >= 0 && (size_t)n < argvsize) |
|
778 memset(argvstart + n, 0, argvsize - n); |
|
779 } |
|
780 } |
|
781 #endif |
|
782 |
|
783 Py_RETURN_NONE; |
|
784 } |
|
785 #endif /* ndef SETPROCNAME_USE_NONE */ |
|
786 |
730 #endif /* ndef _WIN32 */ |
787 #endif /* ndef _WIN32 */ |
731 |
788 |
732 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
789 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs) |
733 { |
790 { |
734 PyObject *statobj = NULL; /* initialize - optional arg */ |
791 PyObject *statobj = NULL; /* initialize - optional arg */ |
897 "Returns None for non-existent entries and entries of other types.\n"}, |
954 "Returns None for non-existent entries and entries of other types.\n"}, |
898 #ifdef CMSG_LEN |
955 #ifdef CMSG_LEN |
899 {"recvfds", (PyCFunction)recvfds, METH_VARARGS, |
956 {"recvfds", (PyCFunction)recvfds, METH_VARARGS, |
900 "receive list of file descriptors via socket\n"}, |
957 "receive list of file descriptors via socket\n"}, |
901 #endif |
958 #endif |
902 #endif |
959 #ifndef SETPROCNAME_USE_NONE |
|
960 {"setprocname", (PyCFunction)setprocname, METH_VARARGS, |
|
961 "set process title (best-effort)\n"}, |
|
962 #endif |
|
963 #endif /* ndef _WIN32 */ |
903 #ifdef __APPLE__ |
964 #ifdef __APPLE__ |
904 { |
965 { |
905 "isgui", (PyCFunction)isgui, METH_NOARGS, |
966 "isgui", (PyCFunction)isgui, METH_NOARGS, |
906 "Is a CoreGraphics session available?" |
967 "Is a CoreGraphics session available?" |
907 }, |
968 }, |