Skip to content

Commit dbe3f76

Browse files
committed
Patch #569139: Implementation of major, minor and makedev.
1 parent 3e3e129 commit dbe3f76

5 files changed

Lines changed: 450 additions & 17 deletions

File tree

Doc/lib/libos.tex

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -700,13 +700,32 @@ \subsection{Files and Directories \label{os-file-dir}}
700700
doesn't open the FIFO --- it just creates the rendezvous point.
701701
\end{funcdesc}
702702

703-
\begin{funcdesc}{mknod}{path\optional{, mode=0600, major, minor}}
703+
\begin{funcdesc}{mknod}{path\optional{, mode=0600, device}}
704704
Create a filesystem node (file, device special file or named pipe)
705-
named filename. mode specifies both the permissions to use and the
706-
type of node to be created, being combined (bitwise OR) with one of
707-
S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO (those constants are available
708-
in \module{stat}). For S_IFCHR and S_IFBLK, major and minor define the
709-
newly created device special file, otherwise they are ignored.
705+
named filename. \var{mode} specifies both the permissions to use and
706+
the type of node to be created, being combined (bitwise OR) with one
707+
of S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO (those constants are
708+
available in \module{stat}). For S_IFCHR and S_IFBLK, \var{device}
709+
defines the newly created device special file (probably using
710+
\function{os.makedev()}), otherwise it is ignored.
711+
712+
\versionadded{2.3}
713+
\end{funcdesc}
714+
715+
\begin{funcdesc}{major}{device}
716+
Extracts a device major number from a raw device number.
717+
718+
\versionadded{2.3}
719+
\end{funcdesc}
720+
721+
\begin{funcdesc}{minor}{device}
722+
Extracts a device minor number from a raw device number.
723+
724+
\versionadded{2.3}
725+
\end{funcdesc}
726+
727+
\begin{funcdesc}{makedev}{major, minor}
728+
Composes a raw device number from the major and minor device numbers.
710729

711730
\versionadded{2.3}
712731
\end{funcdesc}

Modules/posixmodule.c

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,16 @@ extern int lstat(const char *, struct stat *);
277277
# define STRUCT_STAT struct stat
278278
#endif
279279

280+
#if defined(MAJOR_IN_MKDEV)
281+
#include <sys/mkdev.h>
282+
#else
283+
#if defined(MAJOR_IN_SYSMACROS)
284+
#include <sys/sysmacros.h>
285+
#endif
280286
#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
281287
#include <sys/mkdev.h>
282288
#endif
289+
#endif
283290

284291
/* Return a dictionary corresponding to the POSIX environment table */
285292
#ifdef WITH_NEXT_FRAMEWORK
@@ -5081,28 +5088,26 @@ posix_mkfifo(PyObject *self, PyObject *args)
50815088

50825089
#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
50835090
PyDoc_STRVAR(posix_mknod__doc__,
5084-
"mknod(filename, [, mode=0600, major, minor])\n\n\
5091+
"mknod(filename, [, mode=0600, device])\n\n\
50855092
Create a filesystem node (file, device special file or named pipe)\n\
50865093
named filename. mode specifies both the permissions to use and the\n\
50875094
type of node to be created, being combined (bitwise OR) with one of\n\
50885095
S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
5089-
major and minor define the newly created device special file, otherwise\n\
5090-
they are ignored.");
5096+
device defines the newly created device special file (probably using\n\
5097+
os.makedev()), otherwise it is ignored.");
50915098

50925099

50935100
static PyObject *
50945101
posix_mknod(PyObject *self, PyObject *args)
50955102
{
50965103
char *filename;
50975104
int mode = 0600;
5098-
int major = 0;
5099-
int minor = 0;
5105+
int device = 0;
51005106
int res;
5101-
if (!PyArg_ParseTuple(args, "s|iii:mknod", &filename,
5102-
&mode, &major, &minor))
5107+
if (!PyArg_ParseTuple(args, "s|iii:mknod", &filename, &mode, &device))
51035108
return NULL;
51045109
Py_BEGIN_ALLOW_THREADS
5105-
res = mknod(filename, mode, makedev(major, minor));
5110+
res = mknod(filename, mode, device);
51065111
Py_END_ALLOW_THREADS
51075112
if (res < 0)
51085113
return posix_error();
@@ -5111,6 +5116,47 @@ posix_mknod(PyObject *self, PyObject *args)
51115116
}
51125117
#endif
51135118

5119+
#ifdef HAVE_DEVICE_MACROS
5120+
PyDoc_STRVAR(posix_major__doc__,
5121+
"major(device) -> major number\n\
5122+
Extracts a device major number from a raw device number.");
5123+
5124+
static PyObject *
5125+
posix_major(PyObject *self, PyObject *args)
5126+
{
5127+
int device;
5128+
if (!PyArg_ParseTuple(args, "i:major", &device))
5129+
return NULL;
5130+
return PyInt_FromLong((long)major(device));
5131+
}
5132+
5133+
PyDoc_STRVAR(posix_minor__doc__,
5134+
"minor(device) -> minor number\n\
5135+
Extracts a device minor number from a raw device number.");
5136+
5137+
static PyObject *
5138+
posix_minor(PyObject *self, PyObject *args)
5139+
{
5140+
int device;
5141+
if (!PyArg_ParseTuple(args, "i:minor", &device))
5142+
return NULL;
5143+
return PyInt_FromLong((long)minor(device));
5144+
}
5145+
5146+
PyDoc_STRVAR(posix_makedev__doc__,
5147+
"makedev(major, minor) -> device number\n\
5148+
Composes a raw device number from the major and minor device numbers.");
5149+
5150+
static PyObject *
5151+
posix_makedev(PyObject *self, PyObject *args)
5152+
{
5153+
int major, minor;
5154+
if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5155+
return NULL;
5156+
return PyInt_FromLong((long)makedev(major, minor));
5157+
}
5158+
#endif /* device macros */
5159+
51145160

51155161
#ifdef HAVE_FTRUNCATE
51165162
PyDoc_STRVAR(posix_ftruncate__doc__,
@@ -6905,6 +6951,11 @@ static PyMethodDef posix_methods[] = {
69056951
#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
69066952
{"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
69076953
#endif
6954+
#ifdef HAVE_DEVICE_MACROS
6955+
{"major", posix_major, METH_VARARGS, posix_major__doc__},
6956+
{"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
6957+
{"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
6958+
#endif
69086959
#ifdef HAVE_FTRUNCATE
69096960
{"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
69106961
#endif

0 commit comments

Comments
 (0)