summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2017-07-19 04:10:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2017-07-19 04:10:39 +0000
commitad727b484cac0c4c14d0d32d17c338bd0acc031d (patch)
tree5a52bd37b7255a9f3415706d7f74e06a2217205b
parentInitial commit. (diff)
downloadpython-systemd-ad727b484cac0c4c14d0d32d17c338bd0acc031d.zip
python-systemd-ad727b484cac0c4c14d0d32d17c338bd0acc031d.tar.xz
Adding upstream version 234.upstream/234upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
-rw-r--r--.dir-locals.el7
-rw-r--r--.gitignore32
-rw-r--r--LICENSE.txt502
-rw-r--r--MANIFEST.in8
-rw-r--r--Makefile76
-rw-r--r--NEWS77
-rw-r--r--README.md114
-rw-r--r--docs/.gitignore1
-rw-r--r--docs/conf.py279
-rw-r--r--docs/daemon.rst18
-rw-r--r--docs/default.css196
-rw-r--r--docs/id128.rst53
-rw-r--r--docs/index.rst35
-rw-r--r--docs/journal.rst99
-rw-r--r--docs/layout.html16
-rw-r--r--docs/login.rst28
-rw-r--r--pytest.ini3
-rw-r--r--setup.py102
-rw-r--r--systemd/.gitignore2
-rw-r--r--systemd/__init__.py17
-rw-r--r--systemd/_daemon.c466
-rw-r--r--systemd/_journal.c157
-rw-r--r--systemd/_reader.c1354
-rw-r--r--systemd/daemon.py71
-rw-r--r--systemd/id128-constants.h44
-rw-r--r--systemd/id128-defines.h44
-rw-r--r--systemd/id128.c169
-rw-r--r--systemd/journal.py626
-rw-r--r--systemd/login.c373
-rw-r--r--systemd/macro.h59
-rw-r--r--systemd/pyutil.c79
-rw-r--r--systemd/pyutil.h53
-rw-r--r--systemd/strv.c39
-rw-r--r--systemd/strv.h25
-rw-r--r--systemd/test/test_daemon.py304
-rw-r--r--systemd/test/test_journal.py298
-rw-r--r--systemd/test/test_login.py48
-rw-r--r--systemd/util.c187
-rw-r--r--systemd/util.h33
-rw-r--r--update-constants.py9
40 files changed, 6103 insertions, 0 deletions
diff --git a/.dir-locals.el b/.dir-locals.el
new file mode 100644
index 0000000..8bccaf0
--- /dev/null
+++ b/.dir-locals.el
@@ -0,0 +1,7 @@
+; Sets emacs variables based on mode.
+; A list of (major-mode . ((var1 . value1) (var2 . value2)))
+; Mode can be nil, which gives default values.
+
+((nil . ((indent-tabs-mode . nil)
+ (c-basic-offset . 4)))
+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e6610fe
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,32 @@
+__pycache__/
+*.py[co]
+/journald/*.so
+/TAGS
+
+# Packages
+*.egg
+*.egg-info
+dist
+build
+eggs
+parts
+bin
+var
+sdist
+develop-eggs
+.installed.cfg
+MANIFEST
+
+# Installer logs
+pip-log.txt
+
+# Unit test / coverage reports
+.coverage
+.tox
+.cache
+
+#Translations
+*.mo
+
+#Mr Developer
+.mr.developer.cfg
diff --git a/LICENSE.txt b/LICENSE.txt
new file mode 100644
index 0000000..4362b49
--- /dev/null
+++ b/LICENSE.txt
@@ -0,0 +1,502 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the Lesser GPL. It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+ This license, the Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it. You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations below.
+
+ When we speak of free software, we are referring to freedom of use,
+not price. Our General Public Licenses are designed to make sure that
+you have the freedom to distribute copies of free software (and charge
+for this service if you wish); that you receive source code or can get
+it if you want it; that you can change the software and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+ To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+rights. These restrictions translate to certain responsibilities for
+you if you distribute copies of the library or if you modify it.
+
+ For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you. You must make sure that they, too, receive or can get the source
+code. If you link other code with the library, you must provide
+complete object files to the recipients, so that they can relink them
+with the library after making changes to the library and recompiling
+it. And you must show them these terms so they know their rights.
+
+ We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+ To protect each distributor, we want to make it very clear that
+there is no warranty for the free library. Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+
+ Finally, software patents pose a constant threat to the existence of
+any free program. We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder. Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+ Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License. This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License. We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+ When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library. The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom. The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+ We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License. It also provides other free software developers Less
+of an advantage over competing non-free programs. These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries. However, the Lesser license provides advantages in certain
+special circumstances.
+
+ For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it becomes
+a de-facto standard. To achieve this, non-free programs must be
+allowed to use the library. A more frequent case is that a free
+library does the same job as widely used non-free libraries. In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+ In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software. For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+ Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+ The precise terms and conditions for copying, distribution and
+modification follow. Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library". The
+former contains code derived from the library, whereas the latter must
+be combined with the library in order to run.
+
+ GNU LESSER GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser General Public License (also called "this License").
+Each licensee is addressed as "you".
+
+ A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+ The "Library", below, refers to any such software library or work
+which has been distributed under these terms. A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language. (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+ "Source code" for a work means the preferred form of the work for
+making modifications to it. For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control compilation
+and installation of the library.
+
+ Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it). Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+ 1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+ You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+ 2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) The modified work must itself be a software library.
+
+ b) You must cause the files modified to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ c) You must cause the whole of the work to be licensed at no
+ charge to all third parties under the terms of this License.
+
+ d) If a facility in the modified Library refers to a function or a
+ table of data to be supplied by an application program that uses
+ the facility, other than as an argument passed when the facility
+ is invoked, then you must make a good faith effort to ensure that,
+ in the event an application does not supply such function or
+ table, the facility still operates, and performs whatever part of
+ its purpose remains meaningful.
+
+ (For example, a function in a library to compute square roots has
+ a purpose that is entirely well-defined independent of the
+ application. Therefore, Subsection 2d requires that any
+ application-supplied function or table used by this function must
+ be optional: if the application does not supply it, the square
+ root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library. To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License. (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.) Do not make any other change in
+these notices.
+
+ Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+ This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+ 4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+ If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library". Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+ However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library". The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+ When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library. The
+threshold for this to be true is not precisely defined by law.
+
+ If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work. (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+ Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+ 6. As an exception to the Sections above, you may also combine or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+ You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License. You must supply a copy of this License. If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License. Also, you must do one
+of these things:
+
+ a) Accompany the work with the complete corresponding
+ machine-readable source code for the Library including whatever
+ changes were used in the work (which must be distributed under
+ Sections 1 and 2 above); and, if the work is an executable linked
+ with the Library, with the complete machine-readable "work that
+ uses the Library", as object code and/or source code, so that the
+ user can modify the Library and then relink to produce a modified
+ executable containing the modified Library. (It is understood
+ that the user who changes the contents of definitions files in the
+ Library will not necessarily be able to recompile the application
+ to use the modified definitions.)
+
+ b) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (1) uses at run time a
+ copy of the library already present on the user's computer system,
+ rather than copying library functions into the executable, and (2)
+ will operate properly with a modified version of the library, if
+ the user installs one, as long as the modified version is
+ interface-compatible with the version that the work was made with.
+
+ c) Accompany the work with a written offer, valid for at
+ least three years, to give the same user the materials
+ specified in Subsection 6a, above, for a charge no more
+ than the cost of performing this distribution.
+
+ d) If distribution of the work is made by offering access to copy
+ from a designated place, offer equivalent access to copy the above
+ specified materials from the same place.
+
+ e) Verify that the user has already received a copy of these
+ materials or that you have already sent this user a copy.
+
+ For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it. However, as a special exception,
+the materials to be distributed need not include anything that is
+normally distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+ It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system. Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+ 7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+ a) Accompany the combined library with a copy of the same work
+ based on the Library, uncombined with any other library
+ facilities. This must be distributed under the terms of the
+ Sections above.
+
+ b) Give prominent notice with the combined library of the fact
+ that part of it is a work based on the Library, and explaining
+ where to find the accompanying uncombined form of the same work.
+
+ 8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License. Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License. However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+ 9. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Library or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+ 10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties with
+this License.
+
+ 11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all. For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under any
+particular circumstance, the balance of the section is intended to apply,
+and the section as a whole is intended to apply in other circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License may add
+an explicit geographical distribution limitation excluding those countries,
+so that distribution is permitted only in or among countries not thus
+excluded. In such case, this License incorporates the limitation as if
+written in the body of this License.
+
+ 13. The Free Software Foundation may publish revised and/or new
+versions of the Lesser General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation. If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+ 14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission. For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this. Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+ NO WARRANTY
+
+ 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Libraries
+
+ If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change. You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms of the
+ordinary General Public License).
+
+ To apply these terms, attach the following notices to the library. It is
+safest to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least the
+"copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the library's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the library, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the
+ library `Frob' (a library for tweaking knobs) written by James Random Hacker.
+
+ <signature of Ty Coon>, 1 April 1990
+ Ty Coon, President of Vice
+
+That's all there is to it!
diff --git a/MANIFEST.in b/MANIFEST.in
new file mode 100644
index 0000000..49dc944
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1,8 @@
+include systemd/*.h
+include README.md
+include NEWS
+include LICENSE.txt
+include Makefile
+include pytest.ini
+graft docs
+exclude docs/__pycache__/*
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..26f75b8
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,76 @@
+PYTHON = python
+SED = sed
+SPHINX_BUILD = sphinx-build
+ETAGS = etags
+INCLUDE_DIR := $(shell pkg-config --variable=includedir libsystemd)
+INCLUDE_FLAGS := $(shell pkg-config --cflags libsystemd)
+VERSION := $(shell $(PYTHON) setup.py --version)
+TESTFLAGS = -v
+
+define buildscript
+import sys,sysconfig
+print("build/lib.{}-{}.{}".format(sysconfig.get_platform(), *sys.version_info[:2]))
+endef
+
+builddir := $(shell $(PYTHON) -c '$(buildscript)')
+
+all: build
+
+.PHONY: update-constants
+update-constants: update-constants.py $(INCLUDE_DIR)/systemd/sd-messages.h
+ $(PYTHON) $+ systemd/id128-defines.h | \
+ sort -u | \
+ tee systemd/id128-defines.h.tmp | \
+ $(SED) -n -r 's/,//g; s/#define (SD_MESSAGE_[A-Z0-9_]+)\s.*/add_id(m, "\1", \1) JOINER/p' | \
+ sort -u >systemd/id128-constants.h.tmp
+ mv systemd/id128-defines.h{.tmp,}
+ mv systemd/id128-constants.h{.tmp,}
+ ($(SED) 9q <docs/id128.rst && \
+ sed -n -r 's/#define (SD_MESSAGE_[A-Z0-9_]+) .*/ .. autoattribute:: systemd.id128.\1/p' \
+ systemd/id128-defines.h) >docs/id128.rst.tmp
+ mv docs/id128.rst{.tmp,}
+
+build:
+ $(PYTHON) setup.py build_ext $(INCLUDE_FLAGS)
+ $(PYTHON) setup.py build
+
+install:
+ $(PYTHON) setup.py install --skip-build $(if $(DESTDIR),--root $(DESTDIR))
+
+dist:
+ $(PYTHON) setup.py sdist
+
+sign: dist/systemd-python-$(VERSION).tar.gz
+ gpg --detach-sign -a dist/systemd-python-$(VERSION).tar.gz
+
+clean:
+ rm -rf build systemd/*.so systemd/*.py[co] *.py[co] systemd/__pycache__
+
+distclean: clean
+ rm -rf dist MANIFEST
+
+SPHINXOPTS = -D version=$(VERSION) -D release=$(VERSION)
+sphinx-%: build
+ PYTHONPATH=$(builddir) $(SPHINX_BUILD) -b $* $(SPHINXOPTS) docs build/$*
+ @echo Output has been generated in build/$*
+
+doc: sphinx-html
+
+check: build
+ (cd $(builddir) && $(PYTHON) -m pytest . ../../docs $(TESTFLAGS))
+
+www_target = www.freedesktop.org:/srv/www.freedesktop.org/www/software/systemd/python-systemd
+doc-sync:
+ rsync -rlv --delete --omit-dir-times build/html/ $(www_target)/
+
+upload: dist/systemd-python-$(VERSION).tar.gz dist/systemd-python-$(VERSION).tar.gz.asc
+ twine-3 upload $+
+
+TAGS: $(shell git ls-files systemd/*.[ch])
+ $(ETAGS) $+
+
+shell:
+# we change the directory because python insists on adding $CWD to path
+ (cd $(builddir) && $(PYTHON))
+
+.PHONY: build install dist sign upload clean distclean TAGS doc doc-sync shell
diff --git a/NEWS b/NEWS
new file mode 100644
index 0000000..6daac80
--- /dev/null
+++ b/NEWS
@@ -0,0 +1,77 @@
+Python wrappers for libsystemd API
+
+CHANGES WITH 234:
+
+ * Support for the new sd_is_socket_sockaddr added in systemd 233
+ is added.
+
+ * New id128 constants added in systemd 233 are included.
+
+ * "Extra" fields for the log entry can be passed during
+ LoggerAdapter initialization or for each message separately.
+ Fields specified for the message have higher priority.
+
+ * Small fixes and style tweaks all over.
+
+ Contributions from: Park Jeongsoo, Jimmy Cao, Johannes
+ Weberhofer, Mike Gilbert, Oleksii Shevchuk, Ville Skyttä,
+ Wesley Bowman, Zbigniew Jędrzejewski-Szmek
+
+CHANGES WITH 233:
+
+ * Tests are fixed and should pass on various old and new
+ systems alike.
+
+ * journal.stream() can be used without arguments and defaults
+ to LOG_LEVEL.
+
+ Contributions from Robert James Hernandez,
+ Zbigniew Jędrzejewski-Szmek
+
+CHANGES WITH 232:
+
+ * Wrappers for sd_journal_enumerate_unique, has_runtime_files,
+ has_peristent_files.
+
+ * sd_journal_open_directory_fd, sd_journal_open_files_fd can
+ be used by passing file descriptors to the initializer as
+ the path or files arguments.
+
+ SD_JOURNAL_OS_ROOT flag is supported, and various flags may
+ be passed to the constructor in combination with path or
+ files arguments. All flags arguments are now passed through
+ to the underlying libsystemd library functions, so which
+ combinations are supported depends on that library.
+ systemd 232 contains various fixes in this area.
+
+ Contributions from: Benedit Morbach, Michael Biebl,
+ Michael Herold, Mike Gilbert, Nir Soffer, Sebastian,
+ Ville Skyttä, Zbigniew Jędrzejewski-Szmek
+
+CHANGES WITH 231:
+
+ * Various build fixes and cleanups: documentation can be
+ generated with 'make sphinx-html', pip install works out
+ of the box.
+
+ * Tests that cover most of the python code were added. As a
+ result, a bug in sd_is_mq was fixed in systemd 227.
+
+ * Functions sd_pid_notify and sd_pid_notify_with_fds are now
+ wrapped as optional arguments to notify(), when compiled
+ against a new-enough libsystemd.
+
+ Contributions from: David Strauss, Evgeny Vereshchagin,
+ Jacek Konieczny, Jeroen Dekkers, Zbigniew Jędrzejewski-Szmek
+
+CHANGES WITH 230:
+
+ * python-systemd is again a separate project, after being part
+ of systemd for many years.
+
+ Contributions from: Dave Reisner, David Strauss,
+ Evgeny Vereshchagin, Greg KH, Harald Hoyer, Jacek Konieczny,
+ Jeroen Dekkers, Kay Sievers, Lennart Poettering, Lukas Nykryn,
+ Marti Raudsepp, Richard Marko, Simon Farnsworth,
+ Steven Hiscocks, Thomas Hindoe Paaboel Andersen, Ville Skyttä,
+ Zbigniew Jędrzejewski-Szmek
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..0118060
--- /dev/null
+++ b/README.md
@@ -0,0 +1,114 @@
+python-systemd
+===============
+
+Python module for native access to the systemd facilities. Functionality
+is separated into a number of modules:
+- systemd.journal supports sending of structured messages to the journal
+ and reading journal files,
+- systemd.daemon wraps parts of libsystemd useful for writing daemons
+ and socket activation,
+- systemd.id128 provides functions for querying machine and boot identifiers
+ and a lists of message identifiers provided by systemd,
+- systemd.login wraps parts of libsystemd used to query logged in users
+ and available seats and machines.
+
+Installation
+============
+
+This module should be packaged for almost all Linux distributions. Use
+
+On Fedora/RHEL/CentOS
+
+ dnf install python-systemd python3-systemd
+
+On Debian/Ubuntu/Mint
+
+ apt-get install python-systemd python3-systemd
+
+On openSUSE and SLE
+
+ zypper in python-systemd
+
+To build from source
+
+On Fedora 21+ with Python 2:
+
+ dnf install git python-pip gcc python-devel systemd-devel
+ pip install git+https://github.com/systemd/python-systemd.git#egg=systemd
+
+On Fedora 21+ with Python 3:
+
+ dnf install git python3-pip gcc python3-devel systemd-devel
+ pip3 install git+https://github.com/systemd/python-systemd.git#egg=systemd
+
+On Debian or Ubuntu with Python 2:
+
+ apt-get install libsystemd-{journal,daemon,login,id128}-dev gcc python-dev pkg-config
+
+On Debian or Ubuntu with Python 3:
+
+ apt-get install libsystemd-{journal,daemon,login,id128}-dev gcc python3-dev pkg-config
+
+The project is also available on pypi as `systemd-python`.
+
+Usage
+=====
+
+Quick example:
+
+ from systemd import journal
+ journal.send('Hello world')
+ journal.send('Hello, again, world', FIELD2='Greetings!', FIELD3='Guten tag')
+ journal.send('Binary message', BINARY=b'\xde\xad\xbe\xef')
+
+There is one required argument -- the message, and additional fields
+can be specified as keyword arguments. Following the journald API, all
+names are uppercase.
+
+The journald sendv call can also be accessed directly:
+
+ from systemd import journal
+ journal.sendv('MESSAGE=Hello world')
+ journal.sendv('MESSAGE=Hello, again, world', 'FIELD2=Greetings!',
+ 'FIELD3=Guten tag')
+ journal.sendv('MESSAGE=Binary message', b'BINARY=\xde\xad\xbe\xef')
+
+The two examples should give the same results in the log.
+
+Notes:
+
+ * Unlike the native C version of journald's sd_journal_send(),
+ printf-style substitution is not supported. Perform any
+ substitution using Python's % operator or .format() capabilities
+ first.
+ * A ValueError is raised if sd_journald_sendv() results in an error.
+ This might happen if there are no arguments or one of them is
+ invalid.
+
+Documentation
+=============
+
+Online documentation can be found at [freedesktop.org](https://www.freedesktop.org/software/systemd/python-systemd/)
+
+To build it locally run:
+
+ make sphinx-html
+
+Or use any other builder, see `man sphinx-build` for a list. The compiled docs will be e.g. in `docs/html`.
+
+Viewing Output
+==============
+
+Quick way to view output with all fields as it comes in:
+
+ sudo journalctl -f --output=json
+
+Test Builds (for Development)
+=============================
+
+ python setup.py build_ext -i
+ python
+ >>> from systemd import journal
+ >>> journal.send("Test")
+
+[![Build Status](https://semaphoreci.com/api/v1/projects/42d43c62-f6e5-4fd5-a93a-2b165e6be575/530946/badge.svg)](https://semaphoreci.com/zbyszek/python-systemd)
diff --git a/docs/.gitignore b/docs/.gitignore
new file mode 100644
index 0000000..b06a965
--- /dev/null
+++ b/docs/.gitignore
@@ -0,0 +1 @@
+!layout.html
diff --git a/docs/conf.py b/docs/conf.py
new file mode 100644
index 0000000..1919170
--- /dev/null
+++ b/docs/conf.py
@@ -0,0 +1,279 @@
+# -*- coding: utf-8 -*-
+#
+# python-systemd documentation build configuration file, created by
+# sphinx-quickstart on Sat Feb 9 13:49:42 2013.
+#
+# This file is execfile()d with the current directory set to its containing dir.
+#
+# Note that not all possible configuration values are present in this
+# autogenerated file.
+#
+# All configuration values have a default; values that are commented out
+# serve to show the default.
+
+import sys, os
+
+# If extensions (or modules to document with autodoc) are in another directory,
+# add these directories to sys.path here. If the directory is relative to the
+# documentation root, use os.path.abspath to make it absolute, like shown here.
+#sys.path.insert(0, os.path.abspath('.'))
+
+# -- General configuration -----------------------------------------------------
+
+# If your documentation needs a minimal Sphinx version, state it here.
+#needs_sphinx = '1.0'
+
+# Add any Sphinx extension module names here, as strings. They can be extensions
+# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
+extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.intersphinx', 'sphinx.ext.coverage', 'sphinx.ext.viewcode']
+
+# Add any paths that contain templates here, relative to this directory.
+templates_path = ['.']
+
+# The suffix of source filenames.
+source_suffix = '.rst'
+
+# The encoding of source files.
+#source_encoding = 'utf-8-sig'
+
+# The master toctree document.
+master_doc = 'index'
+
+# General information about the project.
+project = u'python-systemd'
+
+# The language for content autogenerated by Sphinx. Refer to documentation
+# for a list of supported languages.
+#language = None
+
+# There are two options for replacing |today|: either, you set today to some
+# non-false value, then it is used:
+#today = ''
+# Else, today_fmt is used as the format for a strftime call.
+#today_fmt = '%B %d, %Y'
+
+# List of patterns, relative to source directory, that match files and
+# directories to ignore when looking for source files.
+exclude_patterns = []
+
+# The reST default role (used for this markup: `text`) to use for all documents.
+#default_role = None
+
+# If true, '()' will be appended to :func: etc. cross-reference text.
+#add_function_parentheses = True
+
+# If true, the current module name will be prepended to all description
+# unit titles (such as .. function::).
+#add_module_names = True
+
+# If true, sectionauthor and moduleauthor directives will be shown in the
+# output. They are ignored by default.
+#show_authors = False
+
+# The name of the Pygments (syntax highlighting) style to use.
+pygments_style = 'sphinx'
+
+# A list of ignored prefixes for module index sorting.
+#modindex_common_prefix = []
+
+
+# -- Options for HTML output ---------------------------------------------------
+
+# The theme to use for HTML and HTML Help pages. See the documentation for
+# a list of builtin themes.
+html_theme = 'default'
+
+# Theme options are theme-specific and customize the look and feel of a theme
+# further. For a list of options available for each theme, see the
+# documentation.
+#html_theme_options = {}
+
+# Add any paths that contain custom themes here, relative to this directory.
+#html_theme_path = []
+
+# The name for this set of Sphinx documents. If None, it defaults to
+# "<project> v<release> documentation".
+#html_title = None
+
+# A shorter title for the navigation bar. Default is the same as html_title.
+#html_short_title = None
+
+# The name of an image file (relative to this directory) to place at the top
+# of the sidebar.
+#html_logo = None
+
+# The name of an image file (within the static path) to use as favicon of the
+# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
+# pixels large.
+#html_favicon = None
+
+# Add any paths that contain custom static files (such as style sheets) here,
+# relative to this directory. They are copied after the builtin static files,
+# so a file named "default.css" will overwrite the builtin "default.css".
+html_static_path = ['.']
+
+# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
+# using the given strftime format.
+#html_last_updated_fmt = '%b %d, %Y'
+
+# If true, SmartyPants will be used to convert quotes and dashes to
+# typographically correct entities.
+#html_use_smartypants = True
+
+# Custom sidebar templates, maps document names to template names.
+#html_sidebars = {}
+
+# Additional templates that should be rendered to pages, maps page names to
+# template names.
+#html_additional_pages = {}
+
+# If false, no module index is generated.
+#html_domain_indices = True
+
+# If false, no index is generated.
+#html_use_index = True
+
+# If true, the index is split into individual pages for each letter.
+#html_split_index = False
+
+# If true, links to the reST sources are added to the pages.
+html_show_sourcelink = False
+
+# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
+#html_show_sphinx = True
+
+# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
+#html_show_copyright = True
+
+# If true, an OpenSearch description file will be output, and all pages will
+# contain a <link> tag referring to it. The value of this option must be the
+# base URL from which the finished HTML is served.
+#html_use_opensearch = ''
+
+# This is the file name suffix for HTML files (e.g. ".xhtml").
+#html_file_suffix = None
+
+# Output file base name for HTML help builder.
+htmlhelp_basename = 'python-systemddoc'
+
+
+# -- Options for LaTeX output --------------------------------------------------
+
+latex_elements = {
+# The paper size ('letterpaper' or 'a4paper').
+#'papersize': 'letterpaper',
+
+# The font size ('10pt', '11pt' or '12pt').
+#'pointsize': '10pt',
+
+# Additional stuff for the LaTeX preamble.
+#'preamble': '',
+}
+
+# Grouping the document tree into LaTeX files. List of tuples
+# (source start file, target name, title, author, documentclass [howto/manual]).
+latex_documents = [
+ ('index', 'python-systemd.tex', u'python-systemd Documentation',
+ None, 'manual'),
+]
+
+# The name of an image file (relative to this directory) to place at the top of
+# the title page.
+#latex_logo = None
+
+# For "manual" documents, if this is true, then toplevel headings are parts,
+# not chapters.
+#latex_use_parts = False
+
+# If true, show page references after internal links.
+#latex_show_pagerefs = False
+
+# If true, show URL addresses after external links.
+#latex_show_urls = False
+
+# Documents to append as an appendix to all manuals.
+#latex_appendices = []
+
+# If false, no module index is generated.
+#latex_domain_indices = True
+
+
+# -- Options for manual page output --------------------------------------------
+
+# One entry per manual page. List of tuples
+# (source start file, name, description, authors, manual section).
+man_pages = [
+ ('index', 'python-systemd', u'python-systemd Documentation',
+ [], 1)
+]
+
+# If true, show URL addresses after external links.
+#man_show_urls = False
+
+
+# -- Options for Texinfo output ------------------------------------------------
+
+# Grouping the document tree into Texinfo files. List of tuples
+# (source start file, target name, title, author,
+# dir menu entry, description, category)
+texinfo_documents = [
+ ('index', 'python-systemd', u'python-systemd Documentation',
+ u'David Strauss, Zbigniew Jędrzejewski-Szmek, Marti Raudsepp, Steven Hiscocks', 'python-systemd', 'One line description of project.',
+ 'Miscellaneous'),
+]
+
+# Documents to append as an appendix to all manuals.
+#texinfo_appendices = []
+
+# If false, no module index is generated.
+#texinfo_domain_indices = True
+
+# How to display URL addresses: 'footnote', 'no', or 'inline'.
+#texinfo_show_urls = 'footnote'
+
+
+# -- Options for Epub output ---------------------------------------------------
+
+# Bibliographic Dublin Core info.
+epub_title = u'python-systemd'
+epub_author = u'David Strauss, Zbigniew Jędrzejewski-Szmek, Marti Raudsepp, Steven Hiscocks'
+epub_publisher = u'David Strauss, Zbigniew Jędrzejewski-Szmek, Marti Raudsepp, Steven Hiscocks'
+epub_copyright = u'2013, David Strauss, Zbigniew Jędrzejewski-Szmek, Marti Raudsepp, Steven Hiscocks'
+
+# The language of the text. It defaults to the language option
+# or en if the language is not set.
+#epub_language = ''
+
+# The scheme of the identifier. Typical schemes are ISBN or URL.
+#epub_scheme = ''
+
+# The unique identifier of the text. This can be a ISBN number
+# or the project homepage.
+#epub_identifier = ''
+
+# A unique identification for the text.
+#epub_uid = ''
+
+# A tuple containing the cover image and cover page html template filenames.
+#epub_cover = ()
+
+# HTML files that should be inserted before the pages created by sphinx.
+# The format is a list of tuples containing the path and title.
+#epub_pre_files = []
+
+# HTML files shat should be inserted after the pages created by sphinx.
+# The format is a list of tuples containing the path and title.
+#epub_post_files = []
+
+# A list of files that should not be packed into the epub file.
+#epub_exclude_files = []
+
+# The depth of the table of contents in toc.ncx.
+#epub_tocdepth = 3
+
+# Allow duplicate toc entries.
+#epub_tocdup = True
+
+
+# Example configuration for intersphinx: refer to the Python standard library.
+intersphinx_mapping = {'http://docs.python.org/': None}
diff --git a/docs/daemon.rst b/docs/daemon.rst
new file mode 100644
index 0000000..0ad11ed
--- /dev/null
+++ b/docs/daemon.rst
@@ -0,0 +1,18 @@
+`systemd.daemon` module
+=======================
+
+.. automodule:: systemd.daemon
+ :members:
+ :undoc-members:
+ :inherited-members:
+
+ .. autoattribute:: systemd.daemon.LISTEN_FDS_START
+
+ .. autofunction:: _listen_fds
+ .. autofunction:: _is_fifo
+ .. autofunction:: _is_socket
+ .. autofunction:: _is_socket_unix
+ .. autofunction:: _is_socket_inet
+ .. autofunction:: _is_mq
+ .. autofunction:: notify
+ .. autofunction:: booted
diff --git a/docs/default.css b/docs/default.css
new file mode 100644
index 0000000..7c097d6
--- /dev/null
+++ b/docs/default.css
@@ -0,0 +1,196 @@
+@import url("basic.css");
+
+/* -- page layout ----------------------------------------------------------- */
+
+div.documentwrapper {
+ float: left;
+ width: 100%;
+}
+
+div.bodywrapper {
+ margin: 0 0 0 230px;
+}
+
+div.body {
+ background-color: #ffffff;
+ color: #000000;
+ padding: 0 20px 30px 20px;
+}
+
+div.footer {
+ color: #ffffff;
+ width: 100%;
+ padding: 9px 0 9px 0;
+ text-align: center;
+ font-size: 75%;
+}
+
+div.footer a {
+ color: #ffffff;
+ text-decoration: underline;
+}
+
+div.related {
+ background-color: #133f52;
+ line-height: 30px;
+ color: #ffffff;
+}
+
+div.related a {
+ color: #ffffff;
+}
+
+div.sphinxsidebar {
+ background-color: #dddddd;
+}
+
+div.sphinxsidebar p.topless {
+ margin: 5px 10px 10px 10px;
+}
+
+div.sphinxsidebar ul {
+ margin: 10px;
+ padding: 0;
+}
+
+div.sphinxsidebar input {
+ border: 1px solid #000000;
+ font-family: sans-serif;
+ font-size: 1em;
+}
+
+
+
+/* -- hyperlink styles ------------------------------------------------------ */
+
+a {
+ text-decoration: none;
+}
+
+a:hover {
+ text-decoration: underline;
+}
+
+
+
+/* -- body styles ----------------------------------------------------------- */
+
+div.body h1,
+div.body h2,
+div.body h3,
+div.body h4,
+div.body h5,
+div.body h6 {
+ font-family: 'Trebuchet MS', sans-serif;
+ background-color: #f2f2f2;
+ font-weight: normal;
+ color: #20435c;
+ border-bottom: 1px solid #ccc;
+ margin: 20px -20px 10px -20px;
+ padding: 3px 0 3px 10px;
+}
+
+div.body h1 { margin-top: 0; font-size: 200%; }
+div.body h2 { font-size: 160%; }
+div.body h3 { font-size: 140%; }
+div.body h4 { font-size: 120%; }
+div.body h5 { font-size: 110%; }
+div.body h6 { font-size: 100%; }
+
+a.headerlink {
+ color: #c60f0f;
+ font-size: 0.8em;
+ padding: 0 4px 0 4px;
+ text-decoration: none;
+}
+
+a.headerlink:hover {
+ background-color: #c60f0f;
+ color: white;
+}
+
+div.body p, div.body dd, div.body li {
+ text-align: justify;
+ line-height: 130%;
+}
+
+div.admonition p.admonition-title + p {
+ display: inline;
+}
+
+div.admonition p {
+ margin-bottom: 5px;
+}
+
+div.admonition pre {
+ margin-bottom: 5px;
+}
+
+div.admonition ul, div.admonition ol {
+ margin-bottom: 5px;
+}
+
+div.note {
+ background-color: #eee;
+ border: 1px solid #ccc;
+}
+
+div.seealso {
+ background-color: #ffc;
+ border: 1px solid #ff6;
+}
+
+div.topic {
+ background-color: #eee;
+}
+
+div.warning {
+ background-color: #ffe4e4;
+ border: 1px solid #f66;
+}
+
+p.admonition-title {
+ display: inline;
+}
+
+p.admonition-title:after {
+ content: ":";
+}
+
+pre {
+ padding: 5px;
+ background-color: #eeffcc;
+ color: #333333;
+ line-height: 120%;
+ border: 1px solid #ac9;
+ border-left: none;
+ border-right: none;
+}
+
+tt {
+ background-color: #ecf0f3;
+ padding: 0 1px 0 1px;
+ font-size: 0.95em;
+}
+
+th {
+ background-color: #ede;
+}
+
+.warning tt {
+ background: #efc2c2;
+}
+
+.note tt {
+ background: #d6d6d6;
+}
+
+.viewcode-back {
+ font-family: sans-serif;
+}
+
+div.viewcode-block:target {
+ background-color: #f4debf;
+ border-top: 1px solid #ac9;
+ border-bottom: 1px solid #ac9;
+}
diff --git a/docs/id128.rst b/docs/id128.rst
new file mode 100644
index 0000000..8146e9b
--- /dev/null
+++ b/docs/id128.rst
@@ -0,0 +1,53 @@
+`systemd.id128` module
+======================
+
+.. automodule:: systemd.id128
+ :members:
+ :undoc-members:
+ :inherited-members:
+
+ .. autogenerated, do not edit!
+ .. autoattribute:: systemd.id128.SD_MESSAGE_BACKTRACE
+ .. autoattribute:: systemd.id128.SD_MESSAGE_BOOTCHART
+ .. autoattribute:: systemd.id128.SD_MESSAGE_CONFIG_ERROR
+ .. autoattribute:: systemd.id128.SD_MESSAGE_COREDUMP
+ .. autoattribute:: systemd.id128.SD_MESSAGE_DNSSEC_DOWNGRADE
+ .. autoattribute:: systemd.id128.SD_MESSAGE_DNSSEC_FAILURE
+ .. autoattribute:: systemd.id128.SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_FORWARD_SYSLOG_MISSED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_HIBERNATE_KEY
+ .. autoattribute:: systemd.id128.SD_MESSAGE_INVALID_CONFIGURATION
+ .. autoattribute:: systemd.id128.SD_MESSAGE_JOURNAL_DROPPED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_JOURNAL_MISSED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_JOURNAL_START
+ .. autoattribute:: systemd.id128.SD_MESSAGE_JOURNAL_STOP
+ .. autoattribute:: systemd.id128.SD_MESSAGE_JOURNAL_USAGE
+ .. autoattribute:: systemd.id128.SD_MESSAGE_LID_CLOSED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_LID_OPENED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_MACHINE_START
+ .. autoattribute:: systemd.id128.SD_MESSAGE_MACHINE_STOP
+ .. autoattribute:: systemd.id128.SD_MESSAGE_OVERMOUNTING
+ .. autoattribute:: systemd.id128.SD_MESSAGE_POWER_KEY
+ .. autoattribute:: systemd.id128.SD_MESSAGE_SEAT_START
+ .. autoattribute:: systemd.id128.SD_MESSAGE_SEAT_STOP
+ .. autoattribute:: systemd.id128.SD_MESSAGE_SESSION_START
+ .. autoattribute:: systemd.id128.SD_MESSAGE_SESSION_STOP
+ .. autoattribute:: systemd.id128.SD_MESSAGE_SHUTDOWN
+ .. autoattribute:: systemd.id128.SD_MESSAGE_SLEEP_START
+ .. autoattribute:: systemd.id128.SD_MESSAGE_SLEEP_STOP
+ .. autoattribute:: systemd.id128.SD_MESSAGE_SPAWN_FAILED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_STARTUP_FINISHED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_SUSPEND_KEY
+ .. autoattribute:: systemd.id128.SD_MESSAGE_SYSTEM_DOCKED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_SYSTEM_UNDOCKED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_TIME_CHANGE
+ .. autoattribute:: systemd.id128.SD_MESSAGE_TIMEZONE_CHANGE
+ .. autoattribute:: systemd.id128.SD_MESSAGE_TRUNCATED_CORE
+ .. autoattribute:: systemd.id128.SD_MESSAGE_UNIT_FAILED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_UNIT_RELOADED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_UNIT_RELOADING
+ .. autoattribute:: systemd.id128.SD_MESSAGE_UNIT_STARTED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_UNIT_STARTING
+ .. autoattribute:: systemd.id128.SD_MESSAGE_UNIT_STOPPED
+ .. autoattribute:: systemd.id128.SD_MESSAGE_UNIT_STOPPING
+ .. autoattribute:: systemd.id128.SD_MESSAGE_USER_STARTUP_FINISHED
diff --git a/docs/index.rst b/docs/index.rst
new file mode 100644
index 0000000..07b478a
--- /dev/null
+++ b/docs/index.rst
@@ -0,0 +1,35 @@
+.. python-systemd documentation master file, created by
+ sphinx-quickstart on Sat Feb 9 13:49:42 2013.
+ You can adapt this file completely to your liking, but it should at least
+ contain the root `toctree` directive.
+
+.. _index:
+
+python-systemd package
+======================
+
+Contents:
+
+.. toctree::
+ :maxdepth: 2
+
+ journal
+ id128
+ daemon
+ login
+
+Links
+=====
+
+* `systemd man pages <http://www.freedesktop.org/software/systemd/man/index.html>`_
+* `systemd directives <http://www.freedesktop.org/software/systemd/man/systemd.directives.html>`_
+* git `repository <https://github.com/systemd/python-systemd>`_ of this package
+* `systemd <http://www.freedesktop.org/wiki/Software/systemd/>`_ home page
+* systemd git `repository <https://github.com/systemd/systemd>`__
+
+Indices and tables
+==================
+
+* :ref:`genindex`
+* :ref:`modindex`
+* :ref:`search`
diff --git a/docs/journal.rst b/docs/journal.rst
new file mode 100644
index 0000000..49c07b1
--- /dev/null
+++ b/docs/journal.rst
@@ -0,0 +1,99 @@
+`systemd.journal` module
+========================
+
+.. automodule:: systemd.journal
+ :members: send, sendv, stream, stream_fd
+ :undoc-members:
+
+`JournalHandler` class
+----------------------
+
+.. autoclass:: JournalHandler
+
+Accessing the Journal
+---------------------
+
+.. autoclass:: _Reader
+ :undoc-members:
+ :inherited-members:
+
+.. autoclass:: Reader
+ :undoc-members:
+ :inherited-members:
+
+ .. automethod:: __init__
+
+.. autofunction:: _get_catalog
+.. autofunction:: get_catalog
+
+.. autoclass:: Monotonic
+
+.. autoattribute:: systemd.journal.DEFAULT_CONVERTERS
+
+Example: polling for journal events
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This example shows that journal events can be waited for (using
+e.g. `poll`). This makes it easy to integrate Reader in an external
+event loop:
+
+ >>> import select
+ >>> from systemd import journal
+ >>> j = journal.Reader()
+ >>> j.seek_tail()
+ >>> journal.send('testing 1,2,3') # make sure we have something to read
+ >>> j.add_match('MESSAGE=testing 1,2,3')
+ >>> p = select.poll()
+ >>> p.register(j, j.get_events())
+ >>> p.poll() # doctest: +SKIP
+ [(3, 1)]
+ >>> j.get_next() # doctest: +SKIP
+ {'_AUDIT_LOGINUID': 1000,
+ '_CAP_EFFECTIVE': '0',
+ '_SELINUX_CONTEXT': 'unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023',
+ '_GID': 1000,
+ 'CODE_LINE': 1,
+ '_HOSTNAME': '...',
+ '_SYSTEMD_SESSION': 52,
+ '_SYSTEMD_OWNER_UID': 1000,
+ 'MESSAGE': 'testing 1,2,3',
+ '__MONOTONIC_TIMESTAMP':
+ journal.Monotonic(timestamp=datetime.timedelta(2, 76200, 811585),
+ bootid=UUID('958b7e26-df4c-453a-a0f9-a8406cb508f2')),
+ 'SYSLOG_IDENTIFIER': 'python3',
+ '_UID': 1000,
+ '_EXE': '/usr/bin/python3',
+ '_PID': 7733,
+ '_COMM': '...',
+ 'CODE_FUNC': '<module>',
+ 'CODE_FILE': '<doctest journal.rst[4]>',
+ '_SOURCE_REALTIME_TIMESTAMP':
+ datetime.datetime(2015, 9, 5, 13, 17, 4, 944355),
+ '__CURSOR': 's=...',
+ '_BOOT_ID': UUID('958b7e26-df4c-453a-a0f9-a8406cb508f2'),
+ '_CMDLINE': '/usr/bin/python3 ...',
+ '_MACHINE_ID': UUID('263bb31e-3e13-4062-9bdb-f1f4518999d2'),
+ '_SYSTEMD_SLICE': 'user-1000.slice',
+ '_AUDIT_SESSION': 52,
+ '__REALTIME_TIMESTAMP': datetime.datetime(2015, 9, 5, 13, 17, 4, 945110),
+ '_SYSTEMD_UNIT': 'session-52.scope',
+ '_SYSTEMD_CGROUP': '/user.slice/user-1000.slice/session-52.scope',
+ '_TRANSPORT': 'journal'}
+
+
+
+Journal access types
+~~~~~~~~~~~~~~~~~~~~
+
+.. autoattribute:: systemd.journal.LOCAL_ONLY
+.. autoattribute:: systemd.journal.RUNTIME_ONLY
+.. autoattribute:: systemd.journal.SYSTEM
+.. autoattribute:: systemd.journal.CURRENT_USER
+.. autoattribute:: systemd.journal.OS_ROOT
+
+Journal event types
+~~~~~~~~~~~~~~~~~~~
+
+.. autoattribute:: systemd.journal.NOP
+.. autoattribute:: systemd.journal.APPEND
+.. autoattribute:: systemd.journal.INVALIDATE
diff --git a/docs/layout.html b/docs/layout.html
new file mode 100644
index 0000000..f27e52f
--- /dev/null
+++ b/docs/layout.html
@@ -0,0 +1,16 @@
+{% extends "!layout.html" %}
+
+{% block relbar1 %}
+ <a href="index.html">python-systemd </a>·
+ <a href="http://www.freedesktop.org/software/systemd/man/index.html">man pages </a>·
+ <a href="http://www.freedesktop.org/software/systemd/man/systemd.directives.html">directives </a>
+
+ <span style="float:right">python-systemd {{release}}</span>
+ <hr />
+{% endblock %}
+
+{# remove the lower relbar #}
+{% block relbar2 %} {% endblock %}
+
+{# remove the footer #}
+{% block footer %} {% endblock %}
diff --git a/docs/login.rst b/docs/login.rst
new file mode 100644
index 0000000..23204e0
--- /dev/null
+++ b/docs/login.rst
@@ -0,0 +1,28 @@
+`systemd.login` module
+=======================
+
+.. automodule:: systemd.login
+ :members:
+
+.. autoclass:: Monitor
+ :undoc-members:
+ :inherited-members:
+
+Example: polling for events
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This example shows that session/uid/seat/machine events can be waited
+for (using e.g. `poll`). This makes it easy to integrate Monitor in an
+external event loop:
+
+ >>> import select
+ >>> from systemd import login
+ >>> m = login.Monitor("machine") # doctest: +SKIP
+ >>> p = select.poll()
+ >>> p.register(m, m.get_events()) # doctest: +SKIP
+ >>> login.machine_names() # doctest: +SKIP
+ []
+ >>> p.poll() # doctest: +SKIP
+ [(3, 1)]
+ >>> login.machine_names() # doctest: +SKIP
+ ['fedora-25']
diff --git a/pytest.ini b/pytest.ini
new file mode 100644
index 0000000..c5beb19
--- /dev/null
+++ b/pytest.ini
@@ -0,0 +1,3 @@
+[pytest]
+addopts = --doctest-modules --doctest-glob=*.rst --ignore=setup.py
+norecursedirs = .git build
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..983eecd
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,102 @@
+import sys, os
+from distutils.core import setup, Extension
+from subprocess import Popen, PIPE, check_output
+
+def call(*cmd):
+ cmd = Popen(cmd,
+ stdout=PIPE, stderr=PIPE,
+ universal_newlines=True)
+ if cmd.wait() == 0:
+ return cmd.returncode, cmd.stdout.read()
+ else:
+ return cmd.returncode, cmd.stderr.read()
+
+def pkgconfig(package, **kw):
+ pkg_version = package.replace('-', '_').upper() + '_VERSION'
+ flag_map = {'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries'}
+ pkgconf = os.getenv('PKG_CONFIG', 'pkg-config')
+ status, result = call(pkgconf, '--libs', '--cflags', package)
+ if status != 0:
+ return status, result
+ for token in result.split():
+ kw.setdefault(flag_map.get(token[:2]), []).append(token[2:])
+
+ # allow version detection to be overriden using environment variables
+ version = os.getenv(pkg_version)
+ if not version:
+ version = check_output([pkgconf, '--modversion', package],
+ universal_newlines=True).strip()
+ pair = (pkg_version, version)
+ defines = kw.setdefault('define_macros', [])
+ if pair not in defines:
+ defines.append(pair)
+ return status, kw
+
+def lib(*names, **kw):
+ if '--version' in sys.argv:
+ return {}
+ results = []
+ for name in names:
+ status, result = pkgconfig(name, **kw)
+ if status == 0:
+ return result
+ results.append(result)
+ sys.stderr.write('Cannot find ' + ' or '.join(names) + ':\n\n'
+ + '\n'.join(results) + '\n')
+ sys.exit(status)
+
+version = '234'
+defines = {'define_macros':[('PACKAGE_VERSION', '"{}"'.format(version))]}
+
+_journal = Extension('systemd/_journal',
+ sources = ['systemd/_journal.c',
+ 'systemd/pyutil.c'],
+ extra_compile_args=['-Werror=implicit-function-declaration'],
+ **lib('libsystemd', 'libsystemd-journal', **defines))
+_reader = Extension('systemd/_reader',
+ sources = ['systemd/_reader.c',
+ 'systemd/pyutil.c',
+ 'systemd/strv.c'],
+ extra_compile_args=['-Werror=implicit-function-declaration'],
+ **lib('libsystemd', 'libsystemd-journal', **defines))
+_daemon = Extension('systemd/_daemon',
+ sources = ['systemd/_daemon.c',
+ 'systemd/pyutil.c',
+ 'systemd/util.c'],
+ extra_compile_args=['-Werror=implicit-function-declaration'],
+ **lib('libsystemd', 'libsystemd-daemon', **defines))
+id128 = Extension('systemd/id128',
+ sources = ['systemd/id128.c',
+ 'systemd/pyutil.c'],
+ extra_compile_args=['-Werror=implicit-function-declaration'],
+ **lib('libsystemd', 'libsystemd-id128', **defines))
+login = Extension('systemd/login',
+ sources = ['systemd/login.c',
+ 'systemd/pyutil.c',
+ 'systemd/strv.c'],
+ extra_compile_args=['-Werror=implicit-function-declaration'],
+ **lib('libsystemd', 'libsystemd-login', **defines))
+setup (name = 'systemd-python',
+ version = version,
+ description = 'Python interface for libsystemd',
+ author_email = 'david@davidstrauss.net',
+ maintainer = 'systemd developers',
+ maintainer_email = 'systemd-devel@lists.freedesktop.org',
+ url = 'https://github.com/systemd/python-systemd',
+ license = 'LGPLv2+',
+ classifiers = [
+ 'Programming Language :: Python :: 2',
+ 'Programming Language :: Python :: 3',
+ 'Topic :: Software Development :: Libraries :: Python Modules',
+ 'Topic :: System :: Logging',
+ 'License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)',
+ ],
+ py_modules = ['systemd.journal', 'systemd.daemon',
+ 'systemd.test.test_daemon',
+ 'systemd.test.test_journal',
+ 'systemd.test.test_login'],
+ ext_modules = [_journal,
+ _reader,
+ _daemon,
+ id128,
+ login])
diff --git a/systemd/.gitignore b/systemd/.gitignore
new file mode 100644
index 0000000..d935ee8
--- /dev/null
+++ b/systemd/.gitignore
@@ -0,0 +1,2 @@
+*.py[oc]
+*.so
diff --git a/systemd/__init__.py b/systemd/__init__.py
new file mode 100644
index 0000000..3b5dbe1
--- /dev/null
+++ b/systemd/__init__.py
@@ -0,0 +1,17 @@
+# -*- Mode: python; indent-tabs-mode: nil -*- */
+#
+#
+# Copyright 2012 David Strauss
+#
+# python-systemd is free software; you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation; either version 2.1 of the License, or
+# (at your option) any later version.
+#
+# python-systemd is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
diff --git a/systemd/_daemon.c b/systemd/_daemon.c
new file mode 100644
index 0000000..fe6d890
--- /dev/null
+++ b/systemd/_daemon.c
@@ -0,0 +1,466 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+
+ Copyright 2013-2016 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
+
+ python-systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ python-systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#define PY_SSIZE_T_CLEAN
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wredundant-decls"
+#include <Python.h>
+#pragma GCC diagnostic pop
+
+#include <stdbool.h>
+#include <assert.h>
+#include <sys/socket.h>
+
+#include "systemd/sd-daemon.h"
+#include "pyutil.h"
+#include "macro.h"
+#include "util.h"
+
+#if LIBSYSTEMD_VERSION >= 214
+# define HAVE_PID_NOTIFY
+#endif
+
+#if LIBSYSTEMD_VERSION >= 219
+# define HAVE_PID_NOTIFY_WITH_FDS
+#endif
+
+#if LIBSYSTEMD_VERSION >= 233
+# define HAVE_IS_SOCKET_SOCKADDR
+#endif
+
+PyDoc_STRVAR(module__doc__,
+ "Python interface to the libsystemd-daemon library.\n\n"
+ "Provides _listen_fds, notify, booted, and is_* functions\n"
+ "which wrap sd_listen_fds, sd_notify, sd_booted, sd_is_* and\n"
+ "useful for socket activation and checking if the system is\n"
+ "running under systemd."
+);
+
+PyDoc_STRVAR(booted__doc__,
+ "booted() -> bool\n\n"
+ "Return True iff this system is running under systemd.\n"
+ "Wraps sd_booted(3)."
+);
+
+static PyObject* booted(PyObject *self, PyObject *args) {
+ int r;
+ assert(args == NULL);
+
+ r = sd_booted();
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return PyBool_FromLong(r);
+}
+
+static inline void PyMem_Free_intp(int **p) {
+ PyMem_Free(*p);
+}
+
+PyDoc_STRVAR(notify__doc__,
+ "notify(status, unset_environment=False, pid=0, fds=None) -> bool\n\n"
+ "Send a message to the init system about a status change.\n"
+ "Wraps sd_notify(3).");
+
+static PyObject* notify(PyObject *self, PyObject *args, PyObject *keywds) {
+ int r;
+ const char* msg;
+ int unset = false, n_fds;
+ int _pid = 0;
+ pid_t pid;
+ PyObject *fds = NULL;
+ _cleanup_(PyMem_Free_intp) int *arr = NULL;
+
+ static const char* const kwlist[] = {
+ "status",
+ "unset_environment",
+ "pid",
+ "fds",
+ NULL,
+ };
+#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 3
+ if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|piO:notify",
+ (char**) kwlist, &msg, &unset, &_pid, &fds))
+ return NULL;
+#else
+ PyObject *obj = NULL;
+ if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|OiO:notify",
+ (char**) kwlist, &msg, &obj, &_pid, &fds))
+ return NULL;
+ if (obj != NULL)
+ unset = PyObject_IsTrue(obj);
+ if (unset < 0)
+ return NULL;
+#endif
+ pid = _pid;
+ if (pid < 0 || pid != _pid) {
+ PyErr_SetString(PyExc_OverflowError, "Bad pid_t");
+ return NULL;
+ }
+
+ if (fds != NULL) {
+ Py_ssize_t i, len;
+
+ len = PySequence_Length(fds);
+ if (len < 0)
+ return NULL;
+
+ arr = PyMem_NEW(int, len);
+ if (!fds)
+ return NULL;
+
+ for (i = 0; i < len; i++) {
+ PyObject *item = PySequence_GetItem(fds, i);
+ if (!item)
+ return NULL;
+
+ long value = PyLong_AsLong(item);
+ if (PyErr_Occurred())
+ return NULL;
+
+ arr[i] = value;
+ if (arr[i] != value) {
+ PyErr_SetString(PyExc_OverflowError, "Value to large for an integer");
+ return NULL;
+ }
+ }
+
+ n_fds = len;
+ }
+
+ if (pid == 0 && fds == NULL)
+ r = sd_notify(unset, msg);
+ else if (fds == NULL) {
+#ifdef HAVE_PID_NOTIFY
+ r = sd_pid_notify(pid, unset, msg);
+#else
+ set_error(-ENOSYS, NULL, "Compiled without support for sd_pid_notify");
+ return NULL;
+#endif
+ } else {
+#ifdef HAVE_PID_NOTIFY_WITH_FDS
+ r = sd_pid_notify_with_fds(pid, unset, msg, arr, n_fds);
+#else
+ set_error(-ENOSYS, NULL, "Compiled without support for sd_pid_notify_with_fds");
+ return NULL;
+#endif
+ }
+
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return PyBool_FromLong(r);
+}
+
+
+PyDoc_STRVAR(listen_fds__doc__,
+ "_listen_fds(unset_environment=True) -> int\n\n"
+ "Return the number of descriptors passed to this process by the init system\n"
+ "as part of the socket-based activation logic.\n"
+ "Wraps sd_listen_fds(3)."
+);
+
+static PyObject* listen_fds(PyObject *self, PyObject *args, PyObject *keywds) {
+ int r;
+ int unset = true;
+
+ static const char* const kwlist[] = {"unset_environment", NULL};
+#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 3
+ if (!PyArg_ParseTupleAndKeywords(args, keywds, "|p:_listen_fds",
+ (char**) kwlist, &unset))
+ return NULL;
+#else
+ PyObject *obj = NULL;
+ if (!PyArg_ParseTupleAndKeywords(args, keywds, "|O:_listen_fds",
+ (char**) kwlist, &obj))
+ return NULL;
+ if (obj != NULL)
+ unset = PyObject_IsTrue(obj);
+ if (unset < 0)
+ return NULL;
+#endif
+
+ r = sd_listen_fds(unset);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return long_FromLong(r);
+}
+
+PyDoc_STRVAR(is_fifo__doc__,
+ "_is_fifo(fd, path) -> bool\n\n"
+ "Returns True iff the descriptor refers to a FIFO or a pipe.\n"
+ "Wraps sd_is_fifo(3)."
+);
+
+
+static PyObject* is_fifo(PyObject *self, PyObject *args) {
+ int r;
+ int fd;
+ const char *path = NULL;
+
+#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
+ _cleanup_Py_DECREF_ PyObject *_path = NULL;
+ if (!PyArg_ParseTuple(args, "i|O&:_is_fifo",
+ &fd, Unicode_FSConverter, &_path))
+ return NULL;
+ if (_path)
+ path = PyBytes_AsString(_path);
+#else
+ if (!PyArg_ParseTuple(args, "i|z:_is_fifo", &fd, &path))
+ return NULL;
+#endif
+
+ r = sd_is_fifo(fd, path);
+ if (set_error(r, path, NULL) < 0)
+ return NULL;
+
+ return PyBool_FromLong(r);
+}
+
+
+PyDoc_STRVAR(is_mq__doc__,
+ "_is_mq(fd, path) -> bool\n\n"
+ "Returns True iff the descriptor refers to a POSIX message queue.\n"
+ "Wraps sd_is_mq(3)."
+);
+
+static PyObject* is_mq(PyObject *self, PyObject *args) {
+ int r;
+ int fd;
+ const char *path = NULL;
+
+#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
+ _cleanup_Py_DECREF_ PyObject *_path = NULL;
+ if (!PyArg_ParseTuple(args, "i|O&:_is_mq",
+ &fd, Unicode_FSConverter, &_path))
+ return NULL;
+ if (_path)
+ path = PyBytes_AsString(_path);
+#else
+ if (!PyArg_ParseTuple(args, "i|z:_is_mq", &fd, &path))
+ return NULL;
+#endif
+
+ r = sd_is_mq(fd, path);
+ if (set_error(r, path, NULL) < 0)
+ return NULL;
+
+ return PyBool_FromLong(r);
+}
+
+
+
+PyDoc_STRVAR(is_socket__doc__,
+ "_is_socket(fd, family=AF_UNSPEC, type=0, listening=-1) -> bool\n\n"
+ "Returns True iff the descriptor refers to a socket.\n"
+ "Wraps sd_is_socket(3).\n\n"
+ "Constants for `family` are defined in the socket module."
+);
+
+static PyObject* is_socket(PyObject *self, PyObject *args) {
+ int r;
+ int fd, family = AF_UNSPEC, type = 0, listening = -1;
+
+ if (!PyArg_ParseTuple(args, "i|iii:_is_socket",
+ &fd, &family, &type, &listening))
+ return NULL;
+
+ r = sd_is_socket(fd, family, type, listening);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return PyBool_FromLong(r);
+}
+
+
+PyDoc_STRVAR(is_socket_inet__doc__,
+ "_is_socket_inet(fd, family=AF_UNSPEC, type=0, listening=-1, port=0) -> bool\n\n"
+ "Wraps sd_is_socket_inet(3).\n\n"
+ "Constants for `family` are defined in the socket module."
+);
+
+static PyObject* is_socket_inet(PyObject *self, PyObject *args) {
+ int r;
+ int fd, family = AF_UNSPEC, type = 0, listening = -1, port = 0;
+
+ if (!PyArg_ParseTuple(args, "i|iiii:_is_socket_inet",
+ &fd, &family, &type, &listening, &port))
+ return NULL;
+
+ if (port < 0 || port > UINT16_MAX) {
+ set_error(-EINVAL, NULL, "port must fit into uint16_t");
+ return NULL;
+ }
+
+ r = sd_is_socket_inet(fd, family, type, listening, (uint16_t) port);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return PyBool_FromLong(r);
+}
+
+PyDoc_STRVAR(is_socket_sockaddr__doc__,
+ "_is_socket_sockaddr(fd, address, type=0, flowinfo=0, listening=-1) -> bool\n\n"
+ "Wraps sd_is_socket_inet_sockaddr(3).\n"
+#ifdef HAVE_IS_SOCKET_SOCKADDR
+ "`address` is a systemd-style numerical IPv4 or IPv6 address as used in\n"
+ "ListenStream=. A port may be included after a colon (\":\"). See\n"
+ "systemd.socket(5) for details.\n\n"
+ "Constants for `family` are defined in the socket module."
+#else
+ "NOT SUPPORTED: compiled without support sd_socket_sockaddr"
+#endif
+);
+
+static PyObject* is_socket_sockaddr(PyObject *self, PyObject *args) {
+ int r;
+ int fd, type = 0, flowinfo = 0, listening = -1;
+ const char *address;
+ union sockaddr_union addr = {};
+ unsigned addr_len;
+
+ if (!PyArg_ParseTuple(args, "is|iii:_is_socket_sockaddr",
+ &fd,
+ &address,
+ &type,
+ &flowinfo,
+ &listening))
+ return NULL;
+
+ r = parse_sockaddr(address, &addr, &addr_len);
+ if (r < 0) {
+ set_error(r, NULL, "Cannot parse address");
+ return NULL;
+ }
+
+ if (flowinfo != 0) {
+ if (addr.sa.sa_family != AF_INET6) {
+ set_error(-EINVAL, NULL, "flowinfo is only applicable to IPv6 addresses");
+ return NULL;
+ }
+
+ addr.in6.sin6_flowinfo = flowinfo;
+ }
+
+#ifdef HAVE_IS_SOCKET_SOCKADDR
+ r = sd_is_socket_sockaddr(fd, type, &addr.sa, addr_len, listening);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return PyBool_FromLong(r);
+#else
+ set_error(-ENOSYS, NULL, "Compiled without support for sd_is_socket_sockaddr");
+ return NULL;
+#endif
+}
+
+PyDoc_STRVAR(is_socket_unix__doc__,
+ "_is_socket_unix(fd, type, listening, path) -> bool\n\n"
+ "Wraps sd_is_socket_unix(3)."
+);
+
+static PyObject* is_socket_unix(PyObject *self, PyObject *args) {
+ int r;
+ int fd, type = 0, listening = -1;
+ char* path = NULL;
+ Py_ssize_t length = 0;
+
+#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
+ _cleanup_Py_DECREF_ PyObject *_path = NULL;
+ if (!PyArg_ParseTuple(args, "i|iiO&:_is_socket_unix",
+ &fd, &type, &listening, Unicode_FSConverter, &_path))
+ return NULL;
+ if (_path) {
+ assert(PyBytes_Check(_path));
+ if (PyBytes_AsStringAndSize(_path, &path, &length))
+ return NULL;
+ }
+#else
+ if (!PyArg_ParseTuple(args, "i|iiz#:_is_socket_unix",
+ &fd, &type, &listening, &path, &length))
+ return NULL;
+#endif
+
+ r = sd_is_socket_unix(fd, type, listening, path, length);
+ if (set_error(r, path, NULL) < 0)
+ return NULL;
+
+ return PyBool_FromLong(r);
+}
+
+
+static PyMethodDef methods[] = {
+ { "booted", booted, METH_NOARGS, booted__doc__},
+ { "notify", (PyCFunction) notify, METH_VARARGS | METH_KEYWORDS, notify__doc__},
+ { "_listen_fds", (PyCFunction) listen_fds, METH_VARARGS | METH_KEYWORDS, listen_fds__doc__},
+ { "_is_fifo", is_fifo, METH_VARARGS, is_fifo__doc__},
+ { "_is_mq", is_mq, METH_VARARGS, is_mq__doc__},
+ { "_is_socket", is_socket, METH_VARARGS, is_socket__doc__},
+ { "_is_socket_inet", is_socket_inet, METH_VARARGS, is_socket_inet__doc__},
+ { "_is_socket_sockaddr", is_socket_sockaddr, METH_VARARGS, is_socket_sockaddr__doc__},
+ { "_is_socket_unix", is_socket_unix, METH_VARARGS, is_socket_unix__doc__},
+ {} /* Sentinel */
+};
+
+#if PY_MAJOR_VERSION < 3
+
+DISABLE_WARNING_MISSING_PROTOTYPES;
+PyMODINIT_FUNC init_daemon(void) {
+ PyObject *m;
+
+ m = Py_InitModule3("_daemon", methods, module__doc__);
+ if (m == NULL)
+ return;
+
+ PyModule_AddIntConstant(m, "LISTEN_FDS_START", SD_LISTEN_FDS_START);
+ PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
+}
+REENABLE_WARNING;
+
+#else
+
+static struct PyModuleDef module = {
+ PyModuleDef_HEAD_INIT,
+ "_daemon", /* name of module */
+ module__doc__, /* module documentation, may be NULL */
+ 0, /* size of per-interpreter state of the module */
+ methods
+};
+
+DISABLE_WARNING_MISSING_PROTOTYPES;
+PyMODINIT_FUNC PyInit__daemon(void) {
+ PyObject *m;
+
+ m = PyModule_Create(&module);
+ if (m == NULL)
+ return NULL;
+
+ if (PyModule_AddIntConstant(m, "LISTEN_FDS_START", SD_LISTEN_FDS_START) ||
+ PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
+ Py_DECREF(m);
+ return NULL;
+ }
+
+ return m;
+}
+REENABLE_WARNING;
+
+#endif
diff --git a/systemd/_journal.c b/systemd/_journal.c
new file mode 100644
index 0000000..52f1e7f
--- /dev/null
+++ b/systemd/_journal.c
@@ -0,0 +1,157 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+
+ Copyright 2012 David Strauss <david@davidstrauss.net>
+
+ python-systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ python-systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <Python.h>
+
+#include <alloca.h>
+
+#define SD_JOURNAL_SUPPRESS_LOCATION
+#include "systemd/sd-journal.h"
+
+#include "macro.h"
+
+PyDoc_STRVAR(journal_sendv__doc__,
+ "sendv('FIELD=value', 'FIELD=value', ...) -> None\n\n"
+ "Send an entry to the journal."
+);
+
+static PyObject *journal_sendv(PyObject *self, PyObject *args) {
+ struct iovec *iov = NULL;
+ int argc;
+ int i, r;
+ PyObject *ret = NULL;
+ PyObject **encoded;
+
+ /* Allocate an array for the argument strings */
+ argc = PyTuple_Size(args);
+ encoded = alloca0(argc * sizeof(PyObject*));
+
+ /* Allocate sufficient iovector space for the arguments. */
+ iov = alloca(argc * sizeof(struct iovec));
+
+ /* Iterate through the Python arguments and fill the iovector. */
+ for (i = 0; i < argc; ++i) {
+ PyObject *item = PyTuple_GetItem(args, i);
+ char *stritem;
+ Py_ssize_t length;
+
+ if (PyUnicode_Check(item)) {
+ encoded[i] = PyUnicode_AsEncodedString(item, "utf-8", "strict");
+ if (encoded[i] == NULL)
+ goto out;
+ item = encoded[i];
+ }
+ if (PyBytes_AsStringAndSize(item, &stritem, &length))
+ goto out;
+
+ iov[i].iov_base = stritem;
+ iov[i].iov_len = length;
+ }
+
+ /* Send the iovector to the journal. */
+ r = sd_journal_sendv(iov, argc);
+ if (r < 0) {
+ errno = -r;
+ PyErr_SetFromErrno(PyExc_IOError);
+ goto out;
+ }
+
+ /* End with success. */
+ Py_INCREF(Py_None);
+ ret = Py_None;
+
+out:
+ for (i = 0; i < argc; ++i)
+ Py_XDECREF(encoded[i]);
+
+ return ret;
+}
+
+PyDoc_STRVAR(journal_stream_fd__doc__,
+ "stream_fd(identifier, priority, level_prefix) -> fd\n\n"
+ "Open a stream to journal by calling sd_journal_stream_fd(3)."
+);
+
+static PyObject* journal_stream_fd(PyObject *self, PyObject *args) {
+ const char* identifier;
+ int priority, level_prefix;
+ int fd;
+
+ if (!PyArg_ParseTuple(args, "sii:stream_fd",
+ &identifier, &priority, &level_prefix))
+ return NULL;
+
+ fd = sd_journal_stream_fd(identifier, priority, level_prefix);
+ if (fd < 0) {
+ errno = -fd;
+ return PyErr_SetFromErrno(PyExc_IOError);
+ }
+
+ return PyLong_FromLong(fd);
+}
+
+static PyMethodDef methods[] = {
+ { "sendv", journal_sendv, METH_VARARGS, journal_sendv__doc__ },
+ { "stream_fd", journal_stream_fd, METH_VARARGS, journal_stream_fd__doc__ },
+ { NULL, NULL, 0, NULL } /* Sentinel */
+};
+
+#if PY_MAJOR_VERSION < 3
+
+DISABLE_WARNING_MISSING_PROTOTYPES;
+PyMODINIT_FUNC init_journal(void) {
+ PyObject *m;
+
+ m = Py_InitModule("_journal", methods);
+ if (m == NULL)
+ return;
+
+ PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
+}
+REENABLE_WARNING;
+
+#else
+
+static struct PyModuleDef module = {
+ PyModuleDef_HEAD_INIT,
+ "_journal", /* name of module */
+ NULL, /* module documentation, may be NULL */
+ -1, /* size of per-interpreter state of the module */
+ methods
+};
+
+DISABLE_WARNING_MISSING_PROTOTYPES;
+PyMODINIT_FUNC PyInit__journal(void) {
+ PyObject *m;
+
+ m = PyModule_Create(&module);
+ if (m == NULL)
+ return NULL;
+
+ if (PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
+ Py_DECREF(m);
+ return NULL;
+ }
+
+ return m;
+}
+REENABLE_WARNING;
+
+#endif
diff --git a/systemd/_reader.c b/systemd/_reader.c
new file mode 100644
index 0000000..5b7e191
--- /dev/null
+++ b/systemd/_reader.c
@@ -0,0 +1,1354 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+
+ Copyright 2013 Steven Hiscocks, Zbigniew Jędrzejewski-Szmek
+
+ python-systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ python-systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <Python.h>
+#include <structmember.h>
+#include <datetime.h>
+#include <time.h>
+#include <stdio.h>
+#include <stdbool.h>
+
+#include "systemd/sd-journal.h"
+
+#include "pyutil.h"
+#include "macro.h"
+#include "strv.h"
+
+#if defined(LIBSYSTEMD_VERSION) || LIBSYSTEMD_JOURNAL_VERSION > 204
+# define HAVE_JOURNAL_OPEN_FILES
+#else
+# define SD_JOURNAL_SYSTEM (1 << 2)
+# define SD_JOURNAL_CURRENT_USER (1 << 3)
+#endif
+
+#if LIBSYSTEMD_VERSION >= 229
+# define HAVE_ENUMERATE_FIELDS
+# define HAVE_HAS_RUNTIME_FILES
+# define HAVE_HAS_PERSISTENT_FILES
+#endif
+
+#if LIBSYSTEMD_VERSION >= 230
+# define HAVE_JOURNAL_OPEN_DIRECTORY_FD
+#else
+# define SD_JOURNAL_OS_ROOT (1 << 4)
+#endif
+
+typedef struct {
+ PyObject_HEAD
+ sd_journal *j;
+} Reader;
+static PyTypeObject ReaderType;
+
+PyDoc_STRVAR(module__doc__,
+ "Class to reads the systemd journal similar to journalctl.");
+
+
+#if PY_MAJOR_VERSION >= 3
+static PyTypeObject MonotonicType;
+
+PyDoc_STRVAR(MonotonicType__doc__,
+ "A tuple of (timestamp, bootid) for holding monotonic timestamps");
+
+static PyStructSequence_Field MonotonicType_fields[] = {
+ {(char*) "timestamp", (char*) "Time"},
+ {(char*) "bootid", (char*) "Unique identifier of the boot"},
+ {} /* Sentinel */
+};
+
+static PyStructSequence_Desc Monotonic_desc = {
+ (char*) "journal.Monotonic",
+ MonotonicType__doc__,
+ MonotonicType_fields,
+ 2,
+};
+#endif
+
+/**
+ * Convert a str or bytes object into a C-string path.
+ * Returns NULL on error.
+ */
+static char* convert_path(PyObject *path, PyObject **bytes) {
+#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
+ int r;
+
+ r = PyUnicode_FSConverter(path, bytes);
+ if (r == 0)
+ return NULL;
+
+ return PyBytes_AsString(*bytes);
+#else
+ return PyString_AsString(path);
+#endif
+}
+
+/**
+ * Return NULL is obj is None, and the object otherwise.
+ */
+static int null_converter(PyObject* obj, void *_result) {
+ PyObject **result = _result;
+
+ assert(result);
+
+ if (!obj)
+ return 0;
+
+ if (obj == Py_None)
+ *result = NULL;
+ else
+ *result = obj;
+ return 1;
+}
+
+/**
+ * Convert a Python sequence object into a strv (char**).
+ */
+static int strv_converter(PyObject* obj, void *_result) {
+ char ***result = _result;
+ Py_ssize_t i, len;
+
+ assert(result);
+
+ if (!PySequence_Check(obj))
+ return 0;
+
+ len = PySequence_Length(obj);
+ *result = new0(char*, len + 1);
+ if (!*result) {
+ set_error(-ENOMEM, NULL, NULL);
+ return 0;
+ }
+
+ for (i = 0; i < len; i++) {
+ PyObject *item;
+ _cleanup_Py_DECREF_ PyObject *bytes = NULL;
+ char *s;
+
+ item = PySequence_ITEM(obj, i);
+ s = convert_path(item, &bytes);
+ if (!s)
+ goto cleanup;
+
+ s = strdup(s);
+ if (!s) {
+ set_error(-ENOMEM, NULL, NULL);
+ goto cleanup;
+ }
+
+ (*result)[i] = s;
+ }
+
+ return 1;
+
+cleanup:
+ strv_free(*result);
+ *result = NULL;
+
+ return 0;
+}
+
+static int long_as_fd(PyObject *obj, int *fd) {
+ long num = long_AsLong(obj);
+ if (PyErr_Occurred())
+ return -1;
+
+ if ((int) num != num) {
+ PyErr_SetString(PyExc_OverflowError, "Value too large");
+ return -1;
+ }
+
+ *fd = (int) num;
+ return 0;
+}
+
+/**
+ * Convert a Python sequence object into an int array.
+ */
+static int intlist_converter(PyObject* obj, int **_result, size_t *_len) {
+ _cleanup_free_ int *result = NULL;
+ Py_ssize_t i, len;
+
+ assert(_result);
+ assert(_len);
+
+ if (!PySequence_Check(obj))
+ return 0;
+
+ len = PySequence_Length(obj);
+ result = new0(int, len);
+ if (!result) {
+ set_error(-ENOMEM, NULL, NULL);
+ return 0;
+ }
+
+ for (i = 0; i < len; i++) {
+ PyObject *item;
+ int fd;
+
+ item = PySequence_ITEM(obj, i);
+ if (long_as_fd(item, &fd) < 0)
+ return 0;
+
+ result[i] = fd;
+ }
+
+ *_result = result;
+ *_len = len;
+ result = NULL;
+ return 1;
+}
+
+static void Reader_dealloc(Reader* self) {
+ sd_journal_close(self->j);
+ Py_TYPE(self)->tp_free((PyObject*)self);
+}
+
+PyDoc_STRVAR(Reader__doc__,
+ "_Reader([flags | path | files]) -> ...\n\n"
+ "_Reader allows filtering and retrieval of Journal entries.\n"
+ "Note: this is a low-level interface, and probably not what you\n"
+ "want, use systemd.journal.Reader instead.\n\n"
+ "Argument `flags` sets open flags of the journal, which can be one of, or an ORed\n"
+ "combination of constants: LOCAL_ONLY (default) opens journal on local machine only;\n"
+ "RUNTIME_ONLY opens only volatile journal files; and SYSTEM opens journal files of\n"
+ "system services and the kernel, CURRENT_USER opens files of the current user; and\n"
+ "OS_ROOT is used to open the journal from directories relative to the specified\n"
+ "directory path or file descriptor.\n"
+ "\n"
+ "Instead of opening the system journal, argument `path` may specify a directory\n"
+ "which contains the journal. It maybe be either a file system path (a string), or\n"
+ "a file descriptor (an integer). Alternatively, argument `files` may specify a list\n"
+ "of journal file names. Note that `flags`, `path`, `files`, `directory_fd` are\n"
+ "exclusive.\n\n"
+ "_Reader implements the context manager protocol: the journal will be closed when\n"
+ "exiting the block.");
+static int Reader_init(Reader *self, PyObject *args, PyObject *keywds) {
+ unsigned flags = SD_JOURNAL_LOCAL_ONLY;
+ PyObject *_path = NULL, *_files = NULL;
+ int r;
+
+ static const char* const kwlist[] = {"flags", "path", "files", NULL};
+ if (!PyArg_ParseTupleAndKeywords(args, keywds, "|iO&O&:__init__", (char**) kwlist,
+ &flags,
+ null_converter, &_path,
+ null_converter, &_files))
+ return -1;
+
+ if (!!_path + !!_files > 1) {
+ PyErr_SetString(PyExc_ValueError,
+ "path and files cannot be specified simultaneously");
+ return -1;
+ }
+
+ if (_path) {
+ if (long_Check(_path)) {
+ int fd;
+
+ if (long_as_fd(_path, &fd) < 0)
+ return -1;
+
+#ifdef HAVE_JOURNAL_OPEN_DIRECTORY_FD
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_open_directory_fd(&self->j, (int) fd, flags);
+ Py_END_ALLOW_THREADS
+#else
+ r = -ENOSYS;
+#endif
+ } else {
+ char *path = NULL;
+ _cleanup_Py_DECREF_ PyObject *path_bytes = NULL;
+
+ path = convert_path(_path, &path_bytes);
+ if (!path)
+ return -1;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_open_directory(&self->j, path, flags);
+ Py_END_ALLOW_THREADS
+ }
+ } else if (_files) {
+ _cleanup_Py_DECREF_ PyObject *item0 = NULL;
+
+ item0 = PySequence_GetItem(_files, 0);
+ if (item0 == NULL || !PyLong_Check(item0)) {
+ _cleanup_strv_free_ char **files = NULL;
+
+ if (!strv_converter(_files, &files))
+ return -1;
+
+#ifdef HAVE_JOURNAL_OPEN_FILES
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_open_files(&self->j, (const char**) files, flags);
+ Py_END_ALLOW_THREADS
+#else
+ r = -ENOSYS;
+#endif
+ } else {
+ _cleanup_free_ int *fds = NULL;
+ size_t n_fds;
+
+ if (!intlist_converter(_files, &fds, &n_fds))
+ return -1;
+
+#ifdef HAVE_JOURNAL_OPEN_DIRECTORY_FD
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_open_files_fd(&self->j, fds, n_fds, flags);
+ Py_END_ALLOW_THREADS
+#else
+ r = -ENOSYS;
+#endif
+ }
+ } else {
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_open(&self->j, flags);
+ Py_END_ALLOW_THREADS
+ }
+
+ return set_error(r, NULL, "Opening the journal failed");
+}
+
+PyDoc_STRVAR(Reader_fileno__doc__,
+ "fileno() -> int\n\n"
+ "Get a file descriptor to poll for changes in the journal.\n"
+ "This method invokes sd_journal_get_fd().\n"
+ "See man:sd_journal_get_fd(3).");
+static PyObject* Reader_fileno(Reader *self, PyObject *args) {
+ int fd;
+
+ fd = sd_journal_get_fd(self->j);
+ set_error(fd, NULL, NULL);
+ if (fd < 0)
+ return NULL;
+ return long_FromLong(fd);
+}
+
+PyDoc_STRVAR(Reader_reliable_fd__doc__,
+ "reliable_fd() -> bool\n\n"
+ "Returns True iff the journal can be polled reliably.\n"
+ "This method invokes sd_journal_reliable_fd().\n"
+ "See man:sd_journal_reliable_fd(3).");
+static PyObject* Reader_reliable_fd(Reader *self, PyObject *args) {
+ int r;
+
+ r = sd_journal_reliable_fd(self->j);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+ return PyBool_FromLong(r);
+}
+
+PyDoc_STRVAR(Reader_get_events__doc__,
+ "get_events() -> int\n\n"
+ "Returns a mask of poll() events to wait for on the file\n"
+ "descriptor returned by .fileno().\n\n"
+ "See man:sd_journal_get_events(3) for further discussion.");
+static PyObject* Reader_get_events(Reader *self, PyObject *args) {
+ int r;
+
+ r = sd_journal_get_events(self->j);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+ return long_FromLong(r);
+}
+
+PyDoc_STRVAR(Reader_get_timeout__doc__,
+ "get_timeout() -> int or None\n\n"
+ "Returns a timeout value for usage in poll(), the time since the\n"
+ "epoch of clock_gettime(2) in microseconds, or None if no timeout\n"
+ "is necessary.\n\n"
+ "The return value must be converted to a relative timeout in\n"
+ "milliseconds if it is to be used as an argument for poll().\n"
+ "See man:sd_journal_get_timeout(3) for further discussion.");
+static PyObject* Reader_get_timeout(Reader *self, PyObject *args) {
+ int r;
+ uint64_t t;
+
+ r = sd_journal_get_timeout(self->j, &t);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ if (t == (uint64_t) -1)
+ Py_RETURN_NONE;
+
+ assert_cc(sizeof(unsigned long long) == sizeof(t));
+ return PyLong_FromUnsignedLongLong(t);
+}
+
+PyDoc_STRVAR(Reader_get_timeout_ms__doc__,
+ "get_timeout_ms() -> int\n\n"
+ "Returns a timeout value suitable for usage in poll(), the value\n"
+ "returned by .get_timeout() converted to relative ms, or -1 if\n"
+ "no timeout is necessary.");
+static PyObject* Reader_get_timeout_ms(Reader *self, PyObject *args) {
+ int r;
+ uint64_t t;
+
+ r = sd_journal_get_timeout(self->j, &t);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return absolute_timeout(t);
+}
+
+PyDoc_STRVAR(Reader_close__doc__,
+ "close() -> None\n\n"
+ "Free resources allocated by this Reader object.\n"
+ "This method invokes sd_journal_close().\n"
+ "See man:sd_journal_close(3).");
+static PyObject* Reader_close(Reader *self, PyObject *args) {
+ assert(self);
+ assert(!args);
+
+ sd_journal_close(self->j);
+ self->j = NULL;
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(Reader_get_usage__doc__,
+ "get_usage() -> int\n\n"
+ "Returns the total disk space currently used by journal\n"
+ "files (in bytes). If `SD_JOURNAL_LOCAL_ONLY` was\n"
+ "passed when opening the journal this value will only reflect\n"
+ "the size of journal files of the local host, otherwise\n"
+ "of all hosts.\n\n"
+ "This method invokes sd_journal_get_usage().\n"
+ "See man:sd_journal_get_usage(3).");
+static PyObject* Reader_get_usage(Reader *self, PyObject *args) {
+ int r;
+ uint64_t bytes;
+
+ r = sd_journal_get_usage(self->j, &bytes);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ assert_cc(sizeof(unsigned long long) == sizeof(bytes));
+ return PyLong_FromUnsignedLongLong(bytes);
+}
+
+PyDoc_STRVAR(Reader___enter____doc__,
+ "__enter__() -> self\n\n"
+ "Part of the context manager protocol.\n"
+ "Returns self.\n");
+static PyObject* Reader___enter__(PyObject *self, PyObject *args) {
+ assert(self);
+ assert(!args);
+
+ Py_INCREF(self);
+ return self;
+}
+
+PyDoc_STRVAR(Reader___exit____doc__,
+ "__exit__(type, value, traceback) -> None\n\n"
+ "Part of the context manager protocol.\n"
+ "Closes the journal.\n");
+static PyObject* Reader___exit__(Reader *self, PyObject *args) {
+ return Reader_close(self, NULL);
+}
+
+PyDoc_STRVAR(Reader_next__doc__,
+ "next([skip]) -> bool\n\n"
+ "Go to the next log entry. Optional skip value means to go to\n"
+ "the `skip`\\-th log entry.\n"
+ "Returns False if at end of file, True otherwise.");
+static PyObject* Reader_next(Reader *self, PyObject *args) {
+ int64_t skip = 1LL;
+ int r = -EUCLEAN;
+
+ if (!PyArg_ParseTuple(args, "|L:next", &skip))
+ return NULL;
+
+ if (skip == 0LL) {
+ PyErr_SetString(PyExc_ValueError, "skip must be nonzero");
+ return NULL;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ if (skip == 1LL)
+ r = sd_journal_next(self->j);
+ else if (skip == -1LL)
+ r = sd_journal_previous(self->j);
+ else if (skip > 1LL)
+ r = sd_journal_next_skip(self->j, skip);
+ else if (skip < -1LL)
+ r = sd_journal_previous_skip(self->j, -skip);
+ else
+ assert(!"should be here");
+ Py_END_ALLOW_THREADS
+
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+ return PyBool_FromLong(r);
+}
+
+PyDoc_STRVAR(Reader_previous__doc__,
+ "previous([skip]) -> bool\n\n"
+ "Go to the previous log entry. Optional skip value means to \n"
+ "go to the `skip`\\-th previous log entry.\n"
+ "Returns False if at start of file, True otherwise.");
+static PyObject* Reader_previous(Reader *self, PyObject *args) {
+ int64_t skip = 1LL;
+ if (!PyArg_ParseTuple(args, "|L:previous", &skip))
+ return NULL;
+
+ return PyObject_CallMethod((PyObject *)self, (char*) "_next",
+ (char*) "L", -skip);
+}
+
+static int extract(const char* msg, size_t msg_len,
+ PyObject **key, PyObject **value) {
+ PyObject *k = NULL, *v;
+ const char *delim_ptr;
+
+ delim_ptr = memchr(msg, '=', msg_len);
+ if (!delim_ptr) {
+ PyErr_SetString(PyExc_OSError,
+ "journal gave us a field without '='");
+ return -1;
+ }
+
+ if (key) {
+ k = unicode_FromStringAndSize(msg, delim_ptr - (const char*) msg);
+ if (!k)
+ return -1;
+ }
+
+ if (value) {
+ v = PyBytes_FromStringAndSize(delim_ptr + 1,
+ (const char*) msg + msg_len - (delim_ptr + 1));
+ if (!v) {
+ Py_XDECREF(k);
+ return -1;
+ }
+
+ *value = v;
+ }
+
+ if (key)
+ *key = k;
+
+ return 0;
+}
+
+PyDoc_STRVAR(Reader_get__doc__,
+ "get(str) -> str\n\n"
+ "Return data associated with this key in current log entry.\n"
+ "Throws KeyError is the data is not available.");
+static PyObject* Reader_get(Reader *self, PyObject *args) {
+ const char* field;
+ const void* msg;
+ size_t msg_len;
+ PyObject *value;
+ int r;
+
+ assert(self);
+ assert(args);
+
+ if (!PyArg_ParseTuple(args, "s:get", &field))
+ return NULL;
+
+ r = sd_journal_get_data(self->j, field, &msg, &msg_len);
+ if (r == -ENOENT) {
+ PyErr_SetString(PyExc_KeyError, field);
+ return NULL;
+ }
+ if (set_error(r, NULL, "field name is not valid") < 0)
+ return NULL;
+
+ r = extract(msg, msg_len, NULL, &value);
+ if (r < 0)
+ return NULL;
+ return value;
+}
+
+PyDoc_STRVAR(Reader_get_all__doc__,
+ "_get_all() -> dict\n\n"
+ "Return dictionary of the current log entry.");
+static PyObject* Reader_get_all(Reader *self, PyObject *args) {
+ PyObject *dict;
+ const void *msg;
+ size_t msg_len;
+ int r;
+
+ dict = PyDict_New();
+ if (!dict)
+ return NULL;
+
+ SD_JOURNAL_FOREACH_DATA(self->j, msg, msg_len) {
+ _cleanup_Py_DECREF_ PyObject *key = NULL, *value = NULL;
+
+ r = extract(msg, msg_len, &key, &value);
+ if (r < 0)
+ goto error;
+
+ if (PyDict_Contains(dict, key)) {
+ PyObject *cur_value = PyDict_GetItem(dict, key);
+
+ if (PyList_CheckExact(cur_value)) {
+ r = PyList_Append(cur_value, value);
+ if (r < 0)
+ goto error;
+ } else {
+ _cleanup_Py_DECREF_ PyObject *tmp_list = PyList_New(0);
+ if (!tmp_list)
+ goto error;
+
+ r = PyList_Append(tmp_list, cur_value);
+ if (r < 0)
+ goto error;
+
+ r = PyList_Append(tmp_list, value);
+ if (r < 0)
+ goto error;
+
+ r = PyDict_SetItem(dict, key, tmp_list);
+ if (r < 0)
+ goto error;
+ }
+ } else {
+ r = PyDict_SetItem(dict, key, value);
+ if (r < 0)
+ goto error;
+ }
+ }
+
+ return dict;
+
+error:
+ Py_DECREF(dict);
+ return NULL;
+}
+
+PyDoc_STRVAR(Reader_get_realtime__doc__,
+ "get_realtime() -> int\n\n"
+ "Return the realtime timestamp for the current journal entry\n"
+ "in microseconds.\n\n"
+ "Wraps sd_journal_get_realtime_usec().\n"
+ "See man:sd_journal_get_realtime_usec(3).");
+static PyObject* Reader_get_realtime(Reader *self, PyObject *args) {
+ uint64_t timestamp;
+ int r;
+
+ assert(self);
+ assert(!args);
+
+ r = sd_journal_get_realtime_usec(self->j, &timestamp);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ assert_cc(sizeof(unsigned long long) == sizeof(timestamp));
+ return PyLong_FromUnsignedLongLong(timestamp);
+}
+
+PyDoc_STRVAR(Reader_get_monotonic__doc__,
+ "get_monotonic() -> (timestamp, bootid)\n\n"
+ "Return the monotonic timestamp for the current journal entry\n"
+ "as a tuple of time in microseconds and bootid.\n\n"
+ "Wraps sd_journal_get_monotonic_usec().\n"
+ "See man:sd_journal_get_monotonic_usec(3).");
+static PyObject* Reader_get_monotonic(Reader *self, PyObject *args) {
+ uint64_t timestamp;
+ sd_id128_t id;
+ PyObject *monotonic, *bootid, *tuple;
+ int r;
+
+ assert(self);
+ assert(!args);
+
+ r = sd_journal_get_monotonic_usec(self->j, &timestamp, &id);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ assert_cc(sizeof(unsigned long long) == sizeof(timestamp));
+ monotonic = PyLong_FromUnsignedLongLong(timestamp);
+ bootid = PyBytes_FromStringAndSize((const char*) &id.bytes, sizeof(id.bytes));
+#if PY_MAJOR_VERSION >= 3
+ tuple = PyStructSequence_New(&MonotonicType);
+#else
+ tuple = PyTuple_New(2);
+#endif
+ if (!monotonic || !bootid || !tuple) {
+ Py_XDECREF(monotonic);
+ Py_XDECREF(bootid);
+ Py_XDECREF(tuple);
+ return NULL;
+ }
+
+#if PY_MAJOR_VERSION >= 3
+ PyStructSequence_SET_ITEM(tuple, 0, monotonic);
+ PyStructSequence_SET_ITEM(tuple, 1, bootid);
+#else
+ PyTuple_SET_ITEM(tuple, 0, monotonic);
+ PyTuple_SET_ITEM(tuple, 1, bootid);
+#endif
+
+ return tuple;
+}
+
+PyDoc_STRVAR(Reader_add_match__doc__,
+ "add_match(match) -> None\n\n"
+ "Add a match to filter journal log entries. All matches of different\n"
+ "fields are combined with logical AND, and matches of the same field\n"
+ "are automatically combined with logical OR.\n"
+ "Match is a string of the form \"FIELD=value\".");
+static PyObject* Reader_add_match(Reader *self, PyObject *args, PyObject *keywds) {
+ char *match;
+ int match_len, r;
+ if (!PyArg_ParseTuple(args, "s#:add_match", &match, &match_len))
+ return NULL;
+
+ r = sd_journal_add_match(self->j, match, match_len);
+ if (set_error(r, NULL, "Invalid match") < 0)
+ return NULL;
+
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(Reader_add_disjunction__doc__,
+ "add_disjunction() -> None\n\n"
+ "Inserts a logical OR between matches added since previous\n"
+ "add_disjunction() or add_conjunction() and the next\n"
+ "add_disjunction() or add_conjunction().\n\n"
+ "See man:sd_journal_add_disjunction(3) for explanation.");
+static PyObject* Reader_add_disjunction(Reader *self, PyObject *args) {
+ int r;
+ r = sd_journal_add_disjunction(self->j);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(Reader_add_conjunction__doc__,
+ "add_conjunction() -> None\n\n"
+ "Inserts a logical AND between matches added since previous\n"
+ "add_disjunction() or add_conjunction() and the next\n"
+ "add_disjunction() or add_conjunction().\n\n"
+ "See man:sd_journal_add_disjunction(3) for explanation.");
+static PyObject* Reader_add_conjunction(Reader *self, PyObject *args) {
+ int r;
+ r = sd_journal_add_conjunction(self->j);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(Reader_flush_matches__doc__,
+ "flush_matches() -> None\n\n"
+ "Clear all current match filters.");
+static PyObject* Reader_flush_matches(Reader *self, PyObject *args) {
+ sd_journal_flush_matches(self->j);
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(Reader_seek_head__doc__,
+ "seek_head() -> None\n\n"
+ "Jump to the beginning of the journal.\n"
+ "This method invokes sd_journal_seek_head().\n"
+ "See man:sd_journal_seek_head(3).");
+static PyObject* Reader_seek_head(Reader *self, PyObject *args) {
+ int r;
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_seek_head(self->j);
+ Py_END_ALLOW_THREADS
+
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(Reader_seek_tail__doc__,
+ "seek_tail() -> None\n\n"
+ "Jump to the end of the journal.\n"
+ "This method invokes sd_journal_seek_tail().\n"
+ "See man:sd_journal_seek_tail(3).");
+static PyObject* Reader_seek_tail(Reader *self, PyObject *args) {
+ int r;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_seek_tail(self->j);
+ Py_END_ALLOW_THREADS
+
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(Reader_seek_realtime__doc__,
+ "seek_realtime(realtime) -> None\n\n"
+ "Seek to nearest matching journal entry to `realtime`. Argument\n"
+ "`realtime` in specified in seconds.");
+static PyObject* Reader_seek_realtime(Reader *self, PyObject *args) {
+ uint64_t timestamp;
+ int r;
+
+ if (!PyArg_ParseTuple(args, "K:seek_realtime", &timestamp))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_seek_realtime_usec(self->j, timestamp);
+ Py_END_ALLOW_THREADS
+
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(Reader_seek_monotonic__doc__,
+ "seek_monotonic(monotonic[, bootid]) -> None\n\n"
+ "Seek to nearest matching journal entry to `monotonic`. Argument\n"
+ "`monotonic` is an timestamp from boot in microseconds.\n"
+ "Argument `bootid` is a string representing which boot the\n"
+ "monotonic time is reference to. Defaults to current bootid.");
+static PyObject* Reader_seek_monotonic(Reader *self, PyObject *args) {
+ char *bootid = NULL;
+ uint64_t timestamp;
+ sd_id128_t id;
+ int r;
+
+ if (!PyArg_ParseTuple(args, "K|z:seek_monotonic", &timestamp, &bootid))
+ return NULL;
+
+ if (bootid) {
+ r = sd_id128_from_string(bootid, &id);
+ if (set_error(r, NULL, "Invalid bootid") < 0)
+ return NULL;
+ } else {
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_id128_get_boot(&id);
+ Py_END_ALLOW_THREADS
+
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_seek_monotonic_usec(self->j, id, timestamp);
+ Py_END_ALLOW_THREADS
+
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ Py_RETURN_NONE;
+}
+
+
+PyDoc_STRVAR(Reader_process__doc__,
+ "process() -> state change (integer)\n\n"
+ "Process events and reset the readable state of the file\n"
+ "descriptor returned by .fileno().\n\n"
+ "Will return constants: NOP if no change; APPEND if new\n"
+ "entries have been added to the end of the journal; and\n"
+ "INVALIDATE if journal files have been added or removed.\n\n"
+ "See man:sd_journal_process(3) for further discussion.");
+static PyObject* Reader_process(Reader *self, PyObject *args) {
+ int r;
+
+ assert(!args);
+
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_process(self->j);
+ Py_END_ALLOW_THREADS
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return long_FromLong(r);
+}
+
+PyDoc_STRVAR(Reader_wait__doc__,
+ "wait([timeout]) -> state change (integer)\n\n"
+ "Wait for a change in the journal. Argument `timeout` specifies\n"
+ "the maximum number of microseconds to wait before returning\n"
+ "regardless of whether the journal has changed. If `timeout` is -1,\n"
+ "then block forever.\n\n"
+ "Will return constants: NOP if no change; APPEND if new\n"
+ "entries have been added to the end of the journal; and\n"
+ "INVALIDATE if journal files have been added or removed.\n\n"
+ "See man:sd_journal_wait(3) for further discussion.");
+static PyObject* Reader_wait(Reader *self, PyObject *args) {
+ int r;
+ int64_t timeout;
+
+ if (!PyArg_ParseTuple(args, "|L:wait", &timeout))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_wait(self->j, timeout);
+ Py_END_ALLOW_THREADS
+
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return long_FromLong(r);
+}
+
+PyDoc_STRVAR(Reader_seek_cursor__doc__,
+ "seek_cursor(cursor) -> None\n\n"
+ "Seek to journal entry by given unique reference `cursor`.");
+static PyObject* Reader_seek_cursor(Reader *self, PyObject *args) {
+ const char *cursor;
+ int r;
+
+ if (!PyArg_ParseTuple(args, "s:seek_cursor", &cursor))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_seek_cursor(self->j, cursor);
+ Py_END_ALLOW_THREADS
+
+ if (set_error(r, NULL, "Invalid cursor") < 0)
+ return NULL;
+
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(Reader_get_cursor__doc__,
+ "get_cursor() -> str\n\n"
+ "Return a cursor string for the current journal entry.\n\n"
+ "Wraps sd_journal_get_cursor(). See man:sd_journal_get_cursor(3).");
+static PyObject* Reader_get_cursor(Reader *self, PyObject *args) {
+ _cleanup_free_ char *cursor = NULL;
+ int r;
+
+ assert(self);
+ assert(!args);
+
+ r = sd_journal_get_cursor(self->j, &cursor);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return unicode_FromString(cursor);
+}
+
+PyDoc_STRVAR(Reader_test_cursor__doc__,
+ "test_cursor(str) -> bool\n\n"
+ "Test whether the cursor string matches current journal entry.\n\n"
+ "Wraps sd_journal_test_cursor(). See man:sd_journal_test_cursor(3).");
+static PyObject* Reader_test_cursor(Reader *self, PyObject *args) {
+ const char *cursor;
+ int r;
+
+ assert(self);
+ assert(args);
+
+ if (!PyArg_ParseTuple(args, "s:test_cursor", &cursor))
+ return NULL;
+
+ r = sd_journal_test_cursor(self->j, cursor);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return PyBool_FromLong(r);
+}
+
+PyDoc_STRVAR(Reader_query_unique__doc__,
+ "query_unique(field) -> a set of values\n\n"
+ "Return a set of unique values appearing in journal for the\n"
+ "given `field`. Note this does not respect any journal matches.\n"
+ "See sd_journal_query_unique(3).");
+static PyObject* Reader_query_unique(Reader *self, PyObject *args) {
+ char *query;
+ int r;
+ const void *uniq;
+ size_t uniq_len;
+ _cleanup_Py_DECREF_ PyObject *_value_set = NULL, *key = NULL;
+ PyObject *value_set;
+
+ if (!PyArg_ParseTuple(args, "s:query_unique", &query))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_query_unique(self->j, query);
+ Py_END_ALLOW_THREADS
+
+ if (set_error(r, NULL, "Invalid field name") < 0)
+ return NULL;
+
+ value_set = _value_set = PySet_New(0);
+ if (!value_set)
+ return NULL;
+
+ key = unicode_FromString(query);
+ if (!key)
+ return NULL;
+
+ SD_JOURNAL_FOREACH_UNIQUE(self->j, uniq, uniq_len) {
+ const char *delim_ptr;
+ _cleanup_Py_DECREF_ PyObject *value = NULL;
+
+ delim_ptr = memchr(uniq, '=', uniq_len);
+ if (!delim_ptr) {
+ set_error(-EINVAL, NULL, "Invalid field in the journal");
+ return NULL;
+ }
+
+ value = PyBytes_FromStringAndSize(
+ delim_ptr + 1,
+ (const char*) uniq + uniq_len - (delim_ptr + 1));
+ if (!value)
+ return NULL;
+
+ if (PySet_Add(value_set, value) < 0)
+ return NULL;
+ }
+
+ _value_set = NULL;
+ return value_set;
+}
+
+PyDoc_STRVAR(Reader_enumerate_fields__doc__,
+ "enumerate_fields(field) -> a set of values\n\n"
+ "Return a set of field names appearing in the journal.\n"
+ "See sd_journal_enumerate_fields(3).");
+static PyObject* Reader_enumerate_fields(Reader *self, PyObject *args) {
+#ifdef HAVE_ENUMERATE_FIELDS
+ _cleanup_Py_DECREF_ PyObject *_value_set = NULL;
+ PyObject *value_set;
+ int r;
+
+ value_set = _value_set = PySet_New(0);
+ if (!value_set)
+ return NULL;
+
+ sd_journal_restart_fields(self->j);
+
+ while (true) {
+ const char *field;
+ _cleanup_Py_DECREF_ PyObject *value = NULL;
+
+ r = sd_journal_enumerate_fields(self->j, &field);
+ if (r == 0)
+ break;
+ if (set_error(r, NULL, "Field enumeration failed") < 0)
+ return NULL;
+
+ value = PyUnicode_FromString(field);
+ if (!value)
+ return NULL;
+
+ if (PySet_Add(value_set, value) < 0)
+ return NULL;
+ }
+
+ _value_set = NULL;
+ return value_set;
+#else
+ set_error(-ENOSYS, NULL, "Compiled without support for sd_journal_enumerate_fields");
+ return NULL;
+#endif
+}
+
+PyDoc_STRVAR(Reader_has_runtime_files__doc__,
+ "has_runtime_files(str) -> bool\n\n"
+ "Returns true if runtime journal files have been found.\n\n"
+ "See man:sd_journal_test_cursor(3).");
+static PyObject* Reader_has_runtime_files(Reader *self, PyObject *args) {
+#ifdef HAVE_ENUMERATE_FIELDS
+ int r;
+
+ assert(self);
+
+ r = sd_journal_has_runtime_files(self->j);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return PyBool_FromLong(r);
+#else
+ set_error(-ENOSYS, NULL, "Compiled without support for sd_journal_has_runtime_files");
+ return NULL;
+#endif
+}
+
+PyDoc_STRVAR(Reader_has_persistent_files__doc__,
+ "has_persistent_files(str) -> bool\n\n"
+ "Returns true if persistent journal files have been found.\n\n"
+ "See man:sd_journal_test_cursor(3).");
+static PyObject* Reader_has_persistent_files(Reader *self, PyObject *args) {
+#ifdef HAVE_ENUMERATE_FIELDS
+ int r;
+
+ assert(self);
+
+ r = sd_journal_has_persistent_files(self->j);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return PyBool_FromLong(r);
+#else
+ set_error(-ENOSYS, NULL, "Compiled without support for sd_journal_has_persistent_files");
+ return NULL;
+#endif
+}
+
+PyDoc_STRVAR(Reader_get_catalog__doc__,
+ "get_catalog() -> str\n\n"
+ "Retrieve a message catalog entry for the current journal entry.\n"
+ "Will throw IndexError if the entry has no MESSAGE_ID\n"
+ "and KeyError is the id is specified, but hasn't been found\n"
+ "in the catalog.\n\n"
+ "Wraps man:sd_journal_get_catalog(3).");
+static PyObject* Reader_get_catalog(Reader *self, PyObject *args) {
+ int r;
+ _cleanup_free_ char *msg = NULL;
+
+ assert(self);
+ assert(!args);
+
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_get_catalog(self->j, &msg);
+ Py_END_ALLOW_THREADS
+
+ if (r == -ENOENT) {
+ const void* mid;
+ size_t mid_len;
+
+ r = sd_journal_get_data(self->j, "MESSAGE_ID", &mid, &mid_len);
+ if (r == 0) {
+ const size_t l = sizeof("MESSAGE_ID");
+ assert(mid_len > l);
+ PyErr_Format(PyExc_KeyError, "%.*s", (int) (mid_len - l),
+ (const char*) mid + l);
+ } else if (r == -ENOENT)
+ PyErr_SetString(PyExc_IndexError, "no MESSAGE_ID field");
+ else
+ set_error(r, NULL, NULL);
+ return NULL;
+ }
+
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return unicode_FromString(msg);
+}
+
+PyDoc_STRVAR(get_catalog__doc__,
+ "get_catalog(id128) -> str\n\n"
+ "Retrieve a message catalog entry for the given id.\n"
+ "Wraps man:sd_journal_get_catalog_for_message_id(3).");
+static PyObject* get_catalog(PyObject *self, PyObject *args) {
+ int r;
+ char *id_ = NULL;
+ sd_id128_t id;
+ _cleanup_free_ char *msg = NULL;
+
+ assert(args);
+
+ if (!PyArg_ParseTuple(args, "z:get_catalog", &id_))
+ return NULL;
+
+ r = sd_id128_from_string(id_, &id);
+ if (set_error(r, NULL, "Invalid id128") < 0)
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_get_catalog_for_message_id(id, &msg);
+ Py_END_ALLOW_THREADS
+
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return unicode_FromString(msg);
+}
+
+PyDoc_STRVAR(data_threshold__doc__,
+ "Threshold for field size truncation in bytes.\n\n"
+ "Fields longer than this will be truncated to the threshold size.\n"
+ "Defaults to 64Kb.");
+
+static PyObject* Reader_get_data_threshold(Reader *self, void *closure) {
+ size_t cvalue;
+ int r;
+
+ r = sd_journal_get_data_threshold(self->j, &cvalue);
+ if (set_error(r, NULL, NULL) < 0)
+ return NULL;
+
+ return long_FromSize_t(cvalue);
+}
+
+static int Reader_set_data_threshold(Reader *self, PyObject *value, void *closure) {
+ int r;
+
+ if (value == NULL) {
+ PyErr_SetString(PyExc_AttributeError, "Cannot delete data threshold");
+ return -1;
+ }
+
+ if (!long_Check(value)){
+ PyErr_SetString(PyExc_TypeError, "Data threshold must be an int");
+ return -1;
+ }
+
+ r = sd_journal_set_data_threshold(self->j, (size_t) long_AsLong(value));
+ return set_error(r, NULL, NULL);
+}
+
+PyDoc_STRVAR(closed__doc__,
+ "True iff journal is closed");
+static PyObject* Reader_get_closed(Reader *self, void *closure) {
+ return PyBool_FromLong(self->j == NULL);
+}
+
+static PyGetSetDef Reader_getsetters[] = {
+ { (char*) "data_threshold",
+ (getter) Reader_get_data_threshold,
+ (setter) Reader_set_data_threshold,
+ (char*) data_threshold__doc__,
+ NULL },
+ { (char*) "closed",
+ (getter) Reader_get_closed,
+ NULL,
+ (char*) closed__doc__,
+ NULL },
+ {} /* Sentinel */
+};
+
+static PyMethodDef Reader_methods[] = {
+ {"fileno", (PyCFunction) Reader_fileno, METH_NOARGS, Reader_fileno__doc__},
+ {"reliable_fd", (PyCFunction) Reader_reliable_fd, METH_NOARGS, Reader_reliable_fd__doc__},
+ {"get_events", (PyCFunction) Reader_get_events, METH_NOARGS, Reader_get_events__doc__},
+ {"get_timeout", (PyCFunction) Reader_get_timeout, METH_NOARGS, Reader_get_timeout__doc__},
+ {"get_timeout_ms", (PyCFunction) Reader_get_timeout_ms, METH_NOARGS, Reader_get_timeout_ms__doc__},
+ {"close", (PyCFunction) Reader_close, METH_NOARGS, Reader_close__doc__},
+ {"get_usage", (PyCFunction) Reader_get_usage, METH_NOARGS, Reader_get_usage__doc__},
+ {"__enter__", (PyCFunction) Reader___enter__, METH_NOARGS, Reader___enter____doc__},
+ {"__exit__", (PyCFunction) Reader___exit__, METH_VARARGS, Reader___exit____doc__},
+ {"_next", (PyCFunction) Reader_next, METH_VARARGS, Reader_next__doc__},
+ {"_previous", (PyCFunction) Reader_previous, METH_VARARGS, Reader_previous__doc__},
+ {"_get", (PyCFunction) Reader_get, METH_VARARGS, Reader_get__doc__},
+ {"_get_all", (PyCFunction) Reader_get_all, METH_NOARGS, Reader_get_all__doc__},
+ {"_get_realtime", (PyCFunction) Reader_get_realtime, METH_NOARGS, Reader_get_realtime__doc__},
+ {"_get_monotonic", (PyCFunction) Reader_get_monotonic, METH_NOARGS, Reader_get_monotonic__doc__},
+ {"add_match", (PyCFunction) Reader_add_match, METH_VARARGS|METH_KEYWORDS, Reader_add_match__doc__},
+ {"add_disjunction", (PyCFunction) Reader_add_disjunction, METH_NOARGS, Reader_add_disjunction__doc__},
+ {"add_conjunction", (PyCFunction) Reader_add_conjunction, METH_NOARGS, Reader_add_conjunction__doc__},
+ {"flush_matches", (PyCFunction) Reader_flush_matches, METH_NOARGS, Reader_flush_matches__doc__},
+ {"seek_head", (PyCFunction) Reader_seek_head, METH_NOARGS, Reader_seek_head__doc__},
+ {"seek_tail", (PyCFunction) Reader_seek_tail, METH_NOARGS, Reader_seek_tail__doc__},
+ {"seek_realtime", (PyCFunction) Reader_seek_realtime, METH_VARARGS, Reader_seek_realtime__doc__},
+ {"seek_monotonic", (PyCFunction) Reader_seek_monotonic, METH_VARARGS, Reader_seek_monotonic__doc__},
+ {"process", (PyCFunction) Reader_process, METH_NOARGS, Reader_process__doc__},
+ {"wait", (PyCFunction) Reader_wait, METH_VARARGS, Reader_wait__doc__},
+ {"seek_cursor", (PyCFunction) Reader_seek_cursor, METH_VARARGS, Reader_seek_cursor__doc__},
+ {"_get_cursor", (PyCFunction) Reader_get_cursor, METH_NOARGS, Reader_get_cursor__doc__},
+ {"test_cursor", (PyCFunction) Reader_test_cursor, METH_VARARGS, Reader_test_cursor__doc__},
+ {"query_unique", (PyCFunction) Reader_query_unique, METH_VARARGS, Reader_query_unique__doc__},
+ {"enumerate_fields", (PyCFunction) Reader_enumerate_fields, METH_NOARGS, Reader_enumerate_fields__doc__},
+ {"has_runtime_files", (PyCFunction) Reader_has_runtime_files, METH_NOARGS, Reader_has_runtime_files__doc__},
+ {"has_persistent_files", (PyCFunction) Reader_has_persistent_files, METH_NOARGS, Reader_has_persistent_files__doc__},
+ {"get_catalog", (PyCFunction) Reader_get_catalog, METH_NOARGS, Reader_get_catalog__doc__},
+ {} /* Sentinel */
+};
+
+static PyTypeObject ReaderType = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ .tp_name = "_reader._Reader",
+ .tp_basicsize = sizeof(Reader),
+ .tp_dealloc = (destructor) Reader_dealloc,
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ .tp_doc = Reader__doc__,
+ .tp_methods = Reader_methods,
+ .tp_getset = Reader_getsetters,
+ .tp_init = (initproc) Reader_init,
+ .tp_new = PyType_GenericNew,
+};
+
+static PyMethodDef methods[] = {
+ { "_get_catalog", get_catalog, METH_VARARGS, get_catalog__doc__},
+ {} /* Sentinel */
+};
+
+#if PY_MAJOR_VERSION >= 3
+static PyModuleDef module = {
+ PyModuleDef_HEAD_INIT,
+ "_reader",
+ module__doc__,
+ -1,
+ methods,
+};
+#endif
+
+#if PY_MAJOR_VERSION >= 3
+static bool initialized = false;
+#endif
+
+DISABLE_WARNING_MISSING_PROTOTYPES;
+
+PyMODINIT_FUNC
+#if PY_MAJOR_VERSION >= 3
+PyInit__reader(void)
+#else
+init_reader(void)
+#endif
+{
+ PyObject* m;
+
+ PyDateTime_IMPORT;
+
+ if (PyType_Ready(&ReaderType) < 0)
+#if PY_MAJOR_VERSION >= 3
+ return NULL;
+#else
+ return;
+#endif
+
+#if PY_MAJOR_VERSION >= 3
+ m = PyModule_Create(&module);
+ if (m == NULL)
+ return NULL;
+
+ if (!initialized) {
+ PyStructSequence_InitType(&MonotonicType, &Monotonic_desc);
+ initialized = true;
+ }
+#else
+ m = Py_InitModule3("_reader", methods, module__doc__);
+ if (m == NULL)
+ return;
+#endif
+
+ Py_INCREF(&ReaderType);
+#if PY_MAJOR_VERSION >= 3
+ Py_INCREF(&MonotonicType);
+#endif
+ if (PyModule_AddObject(m, "_Reader", (PyObject *) &ReaderType) ||
+#if PY_MAJOR_VERSION >= 3
+ PyModule_AddObject(m, "Monotonic", (PyObject*) &MonotonicType) ||
+#endif
+ PyModule_AddIntConstant(m, "NOP", SD_JOURNAL_NOP) ||
+ PyModule_AddIntConstant(m, "APPEND", SD_JOURNAL_APPEND) ||
+ PyModule_AddIntConstant(m, "INVALIDATE", SD_JOURNAL_INVALIDATE) ||
+ PyModule_AddIntConstant(m, "LOCAL_ONLY", SD_JOURNAL_LOCAL_ONLY) ||
+ PyModule_AddIntConstant(m, "RUNTIME_ONLY", SD_JOURNAL_RUNTIME_ONLY) ||
+ PyModule_AddIntConstant(m, "SYSTEM", SD_JOURNAL_SYSTEM) ||
+ PyModule_AddIntConstant(m, "SYSTEM_ONLY", SD_JOURNAL_SYSTEM_ONLY) ||
+ PyModule_AddIntConstant(m, "CURRENT_USER", SD_JOURNAL_CURRENT_USER) ||
+ PyModule_AddIntConstant(m, "OS_ROOT", SD_JOURNAL_OS_ROOT) ||
+ PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
+#if PY_MAJOR_VERSION >= 3
+ Py_DECREF(m);
+ return NULL;
+#endif
+ }
+
+#if PY_MAJOR_VERSION >= 3
+ return m;
+#endif
+}
+
+REENABLE_WARNING;
diff --git a/systemd/daemon.py b/systemd/daemon.py
new file mode 100644
index 0000000..625219c
--- /dev/null
+++ b/systemd/daemon.py
@@ -0,0 +1,71 @@
+from socket import AF_UNSPEC as _AF_UNSPEC
+
+from ._daemon import (__version__,
+ booted,
+ notify,
+ _listen_fds,
+ _is_fifo,
+ _is_socket,
+ _is_socket_inet,
+ _is_socket_sockaddr,
+ _is_socket_unix,
+ _is_mq,
+ LISTEN_FDS_START)
+
+def _convert_fileobj(fileobj):
+ try:
+ return fileobj.fileno()
+ except AttributeError:
+ return fileobj
+
+def is_fifo(fileobj, path=None):
+ fd = _convert_fileobj(fileobj)
+ return _is_fifo(fd, path)
+
+def is_socket(fileobj, family=_AF_UNSPEC, type=0, listening=-1):
+ fd = _convert_fileobj(fileobj)
+ return _is_socket(fd, family, type, listening)
+
+def is_socket_inet(fileobj, family=_AF_UNSPEC, type=0, listening=-1, port=0):
+ fd = _convert_fileobj(fileobj)
+ return _is_socket_inet(fd, family, type, listening, port)
+
+def is_socket_sockaddr(fileobj, address, type=0, flowinfo=0, listening=-1):
+ """Check socket type, address and/or port, flowinfo, listening state.
+
+ Wraps sd_is_socket_inet_sockaddr(3).
+
+ `address` is a systemd-style numerical IPv4 or IPv6 address as used in
+ ListenStream=. A port may be included after a colon (":").
+ See systemd.socket(5) for details.
+
+ Constants for `family` are defined in the socket module.
+ """
+ fd = _convert_fileobj(fileobj)
+ return _is_socket_sockaddr(fd, address, type, flowinfo, listening)
+
+def is_socket_unix(fileobj, type=0, listening=-1, path=None):
+ fd = _convert_fileobj(fileobj)
+ return _is_socket_unix(fd, type, listening, path)
+
+def is_mq(fileobj, path=None):
+ fd = _convert_fileobj(fileobj)
+ return _is_mq(fd, path)
+
+def listen_fds(unset_environment=True):
+ """Return a list of socket activated descriptors
+
+ Example::
+
+ (in primary window)
+ $ systemd-activate -l 2000 python3 -c \\
+ 'from systemd.daemon import listen_fds; print(listen_fds())'
+ (in another window)
+ $ telnet localhost 2000
+ (in primary window)
+ ...
+ Execing python3 (...)
+ [3]
+ """
+ num = _listen_fds(unset_environment)
+ return list(range(LISTEN_FDS_START, LISTEN_FDS_START + num))
diff --git a/systemd/id128-constants.h b/systemd/id128-constants.h
new file mode 100644
index 0000000..6de8d82
--- /dev/null
+++ b/systemd/id128-constants.h
@@ -0,0 +1,44 @@
+add_id(m, "SD_MESSAGE_BACKTRACE", SD_MESSAGE_BACKTRACE) JOINER
+add_id(m, "SD_MESSAGE_BOOTCHART", SD_MESSAGE_BOOTCHART) JOINER
+add_id(m, "SD_MESSAGE_CONFIG_ERROR", SD_MESSAGE_CONFIG_ERROR) JOINER
+add_id(m, "SD_MESSAGE_COREDUMP", SD_MESSAGE_COREDUMP) JOINER
+add_id(m, "SD_MESSAGE_DNSSEC_DOWNGRADE", SD_MESSAGE_DNSSEC_DOWNGRADE) JOINER
+add_id(m, "SD_MESSAGE_DNSSEC_FAILURE", SD_MESSAGE_DNSSEC_FAILURE) JOINER
+add_id(m, "SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED", SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED) JOINER
+add_id(m, "SD_MESSAGE_FORWARD_SYSLOG_MISSED", SD_MESSAGE_FORWARD_SYSLOG_MISSED) JOINER
+add_id(m, "SD_MESSAGE_HIBERNATE_KEY", SD_MESSAGE_HIBERNATE_KEY) JOINER
+add_id(m, "SD_MESSAGE_INVALID_CONFIGURATION", SD_MESSAGE_INVALID_CONFIGURATION) JOINER
+add_id(m, "SD_MESSAGE_JOURNAL_DROPPED", SD_MESSAGE_JOURNAL_DROPPED) JOINER
+add_id(m, "SD_MESSAGE_JOURNAL_MISSED", SD_MESSAGE_JOURNAL_MISSED) JOINER
+add_id(m, "SD_MESSAGE_JOURNAL_START", SD_MESSAGE_JOURNAL_START) JOINER
+add_id(m, "SD_MESSAGE_JOURNAL_STOP", SD_MESSAGE_JOURNAL_STOP) JOINER
+add_id(m, "SD_MESSAGE_JOURNAL_USAGE", SD_MESSAGE_JOURNAL_USAGE) JOINER
+add_id(m, "SD_MESSAGE_LID_CLOSED", SD_MESSAGE_LID_CLOSED) JOINER
+add_id(m, "SD_MESSAGE_LID_OPENED", SD_MESSAGE_LID_OPENED) JOINER
+add_id(m, "SD_MESSAGE_MACHINE_START", SD_MESSAGE_MACHINE_START) JOINER
+add_id(m, "SD_MESSAGE_MACHINE_STOP", SD_MESSAGE_MACHINE_STOP) JOINER
+add_id(m, "SD_MESSAGE_OVERMOUNTING", SD_MESSAGE_OVERMOUNTING) JOINER
+add_id(m, "SD_MESSAGE_POWER_KEY", SD_MESSAGE_POWER_KEY) JOINER
+add_id(m, "SD_MESSAGE_SEAT_START", SD_MESSAGE_SEAT_START) JOINER
+add_id(m, "SD_MESSAGE_SEAT_STOP", SD_MESSAGE_SEAT_STOP) JOINER
+add_id(m, "SD_MESSAGE_SESSION_START", SD_MESSAGE_SESSION_START) JOINER
+add_id(m, "SD_MESSAGE_SESSION_STOP", SD_MESSAGE_SESSION_STOP) JOINER
+add_id(m, "SD_MESSAGE_SHUTDOWN", SD_MESSAGE_SHUTDOWN) JOINER
+add_id(m, "SD_MESSAGE_SLEEP_START", SD_MESSAGE_SLEEP_START) JOINER
+add_id(m, "SD_MESSAGE_SLEEP_STOP", SD_MESSAGE_SLEEP_STOP) JOINER
+add_id(m, "SD_MESSAGE_SPAWN_FAILED", SD_MESSAGE_SPAWN_FAILED) JOINER
+add_id(m, "SD_MESSAGE_STARTUP_FINISHED", SD_MESSAGE_STARTUP_FINISHED) JOINER
+add_id(m, "SD_MESSAGE_SUSPEND_KEY", SD_MESSAGE_SUSPEND_KEY) JOINER
+add_id(m, "SD_MESSAGE_SYSTEM_DOCKED", SD_MESSAGE_SYSTEM_DOCKED) JOINER
+add_id(m, "SD_MESSAGE_SYSTEM_UNDOCKED", SD_MESSAGE_SYSTEM_UNDOCKED) JOINER
+add_id(m, "SD_MESSAGE_TIME_CHANGE", SD_MESSAGE_TIME_CHANGE) JOINER
+add_id(m, "SD_MESSAGE_TIMEZONE_CHANGE", SD_MESSAGE_TIMEZONE_CHANGE) JOINER
+add_id(m, "SD_MESSAGE_TRUNCATED_CORE", SD_MESSAGE_TRUNCATED_CORE) JOINER
+add_id(m, "SD_MESSAGE_UNIT_FAILED", SD_MESSAGE_UNIT_FAILED) JOINER
+add_id(m, "SD_MESSAGE_UNIT_RELOADED", SD_MESSAGE_UNIT_RELOADED) JOINER
+add_id(m, "SD_MESSAGE_UNIT_RELOADING", SD_MESSAGE_UNIT_RELOADING) JOINER
+add_id(m, "SD_MESSAGE_UNIT_STARTED", SD_MESSAGE_UNIT_STARTED) JOINER
+add_id(m, "SD_MESSAGE_UNIT_STARTING", SD_MESSAGE_UNIT_STARTING) JOINER
+add_id(m, "SD_MESSAGE_UNIT_STOPPED", SD_MESSAGE_UNIT_STOPPED) JOINER
+add_id(m, "SD_MESSAGE_UNIT_STOPPING", SD_MESSAGE_UNIT_STOPPING) JOINER
+add_id(m, "SD_MESSAGE_USER_STARTUP_FINISHED", SD_MESSAGE_USER_STARTUP_FINISHED) JOINER
diff --git a/systemd/id128-defines.h b/systemd/id128-defines.h
new file mode 100644
index 0000000..a8d06ef
--- /dev/null
+++ b/systemd/id128-defines.h
@@ -0,0 +1,44 @@
+#define SD_MESSAGE_BACKTRACE SD_ID128_MAKE(1f,4e,0a,44,a8,86,49,93,9a,ae,a3,4f,c6,da,8c,95)
+#define SD_MESSAGE_BOOTCHART SD_ID128_MAKE(9f,26,aa,56,2c,f4,40,c2,b1,6c,77,3d,04,79,b5,18)
+#define SD_MESSAGE_CONFIG_ERROR SD_ID128_MAKE(c7,72,d2,4e,9a,88,4c,be,b9,ea,12,62,5c,30,6c,01)
+#define SD_MESSAGE_COREDUMP SD_ID128_MAKE(fc,2e,22,bc,6e,e6,47,b6,b9,07,29,ab,34,a2,50,b1)
+#define SD_MESSAGE_DNSSEC_DOWNGRADE SD_ID128_MAKE(36,db,2d,fa,5a,90,45,e1,bd,4a,f5,f9,3e,1c,f0,57)
+#define SD_MESSAGE_DNSSEC_FAILURE SD_ID128_MAKE(16,75,d7,f1,72,17,40,98,b1,10,8b,f8,c7,dc,8f,5d)
+#define SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED SD_ID128_MAKE(4d,44,08,cf,d0,d1,44,85,91,84,d1,e6,5d,7c,8a,65)
+#define SD_MESSAGE_FORWARD_SYSLOG_MISSED SD_ID128_MAKE(00,27,22,9c,a0,64,41,81,a7,6c,4e,92,45,8a,fa,2e)
+#define SD_MESSAGE_HIBERNATE_KEY SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,73)
+#define SD_MESSAGE_INVALID_CONFIGURATION SD_ID128_MAKE(c7,72,d2,4e,9a,88,4c,be,b9,ea,12,62,5c,30,6c,01)
+#define SD_MESSAGE_JOURNAL_DROPPED SD_ID128_MAKE(a5,96,d6,fe,7b,fa,49,94,82,8e,72,30,9e,95,d6,1e)
+#define SD_MESSAGE_JOURNAL_MISSED SD_ID128_MAKE(e9,bf,28,e6,e8,34,48,1b,b6,f4,8f,54,8a,d1,36,06)
+#define SD_MESSAGE_JOURNAL_START SD_ID128_MAKE(f7,73,79,a8,49,0b,40,8b,be,5f,69,40,50,5a,77,7b)
+#define SD_MESSAGE_JOURNAL_STOP SD_ID128_MAKE(d9,3f,b3,c9,c2,4d,45,1a,97,ce,a6,15,ce,59,c0,0b)
+#define SD_MESSAGE_JOURNAL_USAGE SD_ID128_MAKE(ec,38,7f,57,7b,84,4b,8f,a9,48,f3,3c,ad,9a,75,e6)
+#define SD_MESSAGE_LID_CLOSED SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,70)
+#define SD_MESSAGE_LID_OPENED SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,6f)
+#define SD_MESSAGE_MACHINE_START SD_ID128_MAKE(24,d8,d4,45,25,73,40,24,96,06,83,81,a6,31,2d,f2)
+#define SD_MESSAGE_MACHINE_STOP SD_ID128_MAKE(58,43,2b,d3,ba,ce,47,7c,b5,14,b5,63,81,b8,a7,58)
+#define SD_MESSAGE_OVERMOUNTING SD_ID128_MAKE(1d,ee,03,69,c7,fc,47,36,b7,09,9b,38,ec,b4,6e,e7)
+#define SD_MESSAGE_POWER_KEY SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,71)
+#define SD_MESSAGE_SEAT_START SD_ID128_MAKE(fc,be,fc,5d,a2,3d,42,80,93,f9,7c,82,a9,29,0f,7b)
+#define SD_MESSAGE_SEAT_STOP SD_ID128_MAKE(e7,85,2b,fe,46,78,4e,d0,ac,cd,e0,4b,c8,64,c2,d5)
+#define SD_MESSAGE_SESSION_START SD_ID128_MAKE(8d,45,62,0c,1a,43,48,db,b1,74,10,da,57,c6,0c,66)
+#define SD_MESSAGE_SESSION_STOP SD_ID128_MAKE(33,54,93,94,24,b4,45,6d,98,02,ca,83,33,ed,42,4a)
+#define SD_MESSAGE_SHUTDOWN SD_ID128_MAKE(98,26,88,66,d1,d5,4a,49,9c,4e,98,92,1d,93,bc,40)
+#define SD_MESSAGE_SLEEP_START SD_ID128_MAKE(6b,bd,95,ee,97,79,41,e4,97,c4,8b,e2,7c,25,41,28)
+#define SD_MESSAGE_SLEEP_STOP SD_ID128_MAKE(88,11,e6,df,2a,8e,40,f5,8a,94,ce,a2,6f,8e,bf,14)
+#define SD_MESSAGE_SPAWN_FAILED SD_ID128_MAKE(64,12,57,65,1c,1b,4e,c9,a8,62,4d,7a,40,a9,e1,e7)
+#define SD_MESSAGE_STARTUP_FINISHED SD_ID128_MAKE(b0,7a,24,9c,d0,24,41,4a,82,dd,00,cd,18,13,78,ff)
+#define SD_MESSAGE_SUSPEND_KEY SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,72)
+#define SD_MESSAGE_SYSTEM_DOCKED SD_ID128_MAKE(f5,f4,16,b8,62,07,4b,28,92,7a,48,c3,ba,7d,51,ff)
+#define SD_MESSAGE_SYSTEM_UNDOCKED SD_ID128_MAKE(51,e1,71,bd,58,52,48,56,81,10,14,4c,51,7c,ca,53)
+#define SD_MESSAGE_TIME_CHANGE SD_ID128_MAKE(c7,a7,87,07,9b,35,4e,aa,a9,e7,7b,37,18,93,cd,27)
+#define SD_MESSAGE_TIMEZONE_CHANGE SD_ID128_MAKE(45,f8,2f,4a,ef,7a,4b,bf,94,2c,e8,61,d1,f2,09,90)
+#define SD_MESSAGE_TRUNCATED_CORE SD_ID128_MAKE(5a,ad,d8,e9,54,dc,4b,1a,8c,95,4d,63,fd,9e,11,37)
+#define SD_MESSAGE_UNIT_FAILED SD_ID128_MAKE(be,02,cf,68,55,d2,42,8b,a4,0d,f7,e9,d0,22,f0,3d)
+#define SD_MESSAGE_UNIT_RELOADED SD_ID128_MAKE(7b,05,eb,c6,68,38,42,22,ba,a8,88,11,79,cf,da,54)
+#define SD_MESSAGE_UNIT_RELOADING SD_ID128_MAKE(d3,4d,03,7f,ff,18,47,e6,ae,66,9a,37,0e,69,47,25)
+#define SD_MESSAGE_UNIT_STARTED SD_ID128_MAKE(39,f5,34,79,d3,a0,45,ac,8e,11,78,62,48,23,1f,bf)
+#define SD_MESSAGE_UNIT_STARTING SD_ID128_MAKE(7d,49,58,e8,42,da,4a,75,8f,6c,1c,dc,7b,36,dc,c5)
+#define SD_MESSAGE_UNIT_STOPPED SD_ID128_MAKE(9d,1a,aa,27,d6,01,40,bd,96,36,54,38,aa,d2,02,86)
+#define SD_MESSAGE_UNIT_STOPPING SD_ID128_MAKE(de,5b,42,6a,63,be,47,a7,b6,ac,3e,aa,c8,2e,2f,6f)
+#define SD_MESSAGE_USER_STARTUP_FINISHED SD_ID128_MAKE(ee,d0,0a,68,ff,d8,4e,31,88,21,05,fd,97,3a,bd,d1)
diff --git a/systemd/id128.c b/systemd/id128.c
new file mode 100644
index 0000000..f5d6aa6
--- /dev/null
+++ b/systemd/id128.c
@@ -0,0 +1,169 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+
+ Copyright 2013 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
+
+ python-systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ python-systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <Python.h>
+
+/* Our include is first, so that our defines are replaced by the ones
+ * from the system header. If the system header has the same definitions
+ * (or does not have them at all), this replacement is silent. If the
+ * system header has a different definition, we get a warning. A warning
+ * means that the system headers changed incompatibly, and we should update
+ * our definition.
+ */
+#include "id128-defines.h"
+#include <systemd/sd-messages.h>
+
+
+#include "pyutil.h"
+#include "macro.h"
+
+PyDoc_STRVAR(module__doc__,
+ "Python interface to the libsystemd-id128 library.\n\n"
+ "Provides SD_MESSAGE_* constants and functions to query and generate\n"
+ "128-bit unique identifiers."
+);
+
+PyDoc_STRVAR(randomize__doc__,
+ "randomize() -> UUID\n\n"
+ "Return a new random 128-bit unique identifier.\n"
+ "Wraps sd_id128_randomize(3)."
+);
+
+PyDoc_STRVAR(get_machine__doc__,
+ "get_machine() -> UUID\n\n"
+ "Return a 128-bit unique identifier for this machine.\n"
+ "Wraps sd_id128_get_machine(3)."
+);
+
+PyDoc_STRVAR(get_boot__doc__,
+ "get_boot() -> UUID\n\n"
+ "Return a 128-bit unique identifier for this boot.\n"
+ "Wraps sd_id128_get_boot(3)."
+);
+
+static PyObject* make_uuid(sd_id128_t id) {
+ _cleanup_Py_DECREF_ PyObject
+ *uuid = NULL, *UUID = NULL, *bytes = NULL,
+ *args = NULL, *kwargs = NULL;
+
+ uuid = PyImport_ImportModule("uuid");
+ if (!uuid)
+ return NULL;
+
+ UUID = PyObject_GetAttrString(uuid, "UUID");
+ bytes = PyBytes_FromStringAndSize((const char*) &id.bytes, sizeof(id.bytes));
+ args = Py_BuildValue("()");
+ kwargs = PyDict_New();
+ if (!UUID || !bytes || !args || !kwargs)
+ return NULL;
+
+ if (PyDict_SetItemString(kwargs, "bytes", bytes) < 0)
+ return NULL;
+
+ return PyObject_Call(UUID, args, kwargs);
+}
+
+#define helper(name) \
+ static PyObject *name(PyObject *self, PyObject *args) { \
+ sd_id128_t id; \
+ int r; \
+ \
+ assert(args == NULL); \
+ \
+ r = sd_id128_##name(&id); \
+ if (r < 0) { \
+ errno = -r; \
+ return PyErr_SetFromErrno(PyExc_IOError); \
+ } \
+ \
+ return make_uuid(id); \
+ }
+
+helper(randomize)
+helper(get_machine)
+helper(get_boot)
+
+static PyMethodDef methods[] = {
+ { "randomize", randomize, METH_NOARGS, randomize__doc__},
+ { "get_machine", get_machine, METH_NOARGS, get_machine__doc__},
+ { "get_boot", get_boot, METH_NOARGS, get_boot__doc__},
+ { NULL, NULL, 0, NULL } /* Sentinel */
+};
+
+static int add_id(PyObject *module, const char* name, sd_id128_t id) {
+ PyObject *obj;
+
+ obj = make_uuid(id);
+ if (!obj)
+ return -1;
+
+ return PyModule_AddObject(module, name, obj);
+}
+
+#if PY_MAJOR_VERSION < 3
+
+DISABLE_WARNING_MISSING_PROTOTYPES;
+PyMODINIT_FUNC initid128(void) {
+ PyObject *m;
+
+ m = Py_InitModule3("id128", methods, module__doc__);
+ if (m == NULL)
+ return;
+
+ /* a series of lines like 'add_id() ;' follow */
+#define JOINER ;
+#include "id128-constants.h"
+#undef JOINER
+ PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
+}
+REENABLE_WARNING;
+
+#else
+
+static struct PyModuleDef module = {
+ PyModuleDef_HEAD_INIT,
+ "id128", /* name of module */
+ module__doc__, /* module documentation, may be NULL */
+ -1, /* size of per-interpreter state of the module */
+ methods
+};
+
+DISABLE_WARNING_MISSING_PROTOTYPES;
+PyMODINIT_FUNC PyInit_id128(void) {
+ PyObject *m;
+
+ m = PyModule_Create(&module);
+ if (m == NULL)
+ return NULL;
+
+ if ( /* a series of lines like 'add_id() ||' follow */
+#define JOINER ||
+#include "id128-constants.h"
+#undef JOINER
+ PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
+ Py_DECREF(m);
+ return NULL;
+ }
+
+ return m;
+}
+REENABLE_WARNING;
+
+#endif
diff --git a/systemd/journal.py b/systemd/journal.py
new file mode 100644
index 0000000..cc0b8aa
--- /dev/null
+++ b/systemd/journal.py
@@ -0,0 +1,626 @@
+# -*- Mode: python; coding:utf-8; indent-tabs-mode: nil -*- */
+#
+#
+# Copyright 2012 David Strauss <david@davidstrauss.net>
+# Copyright 2012 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
+# Copyright 2012 Marti Raudsepp <marti@juffo.org>
+#
+# python-systemd is free software; you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation; either version 2.1 of the License, or
+# (at your option) any later version.
+#
+# python-systemd is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import division
+
+import sys as _sys
+import datetime as _datetime
+import uuid as _uuid
+import traceback as _traceback
+import os as _os
+import logging as _logging
+from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
+ LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG)
+if _sys.version_info >= (3,3):
+ from collections import ChainMap as _ChainMap
+
+from ._journal import __version__, sendv, stream_fd
+from ._reader import (_Reader, NOP, APPEND, INVALIDATE,
+ LOCAL_ONLY, RUNTIME_ONLY,
+ SYSTEM, SYSTEM_ONLY, CURRENT_USER,
+ OS_ROOT,
+ _get_catalog)
+from . import id128 as _id128
+
+if _sys.version_info >= (3,):
+ from ._reader import Monotonic
+else:
+ Monotonic = tuple
+
+
+def _convert_monotonic(m):
+ return Monotonic((_datetime.timedelta(microseconds=m[0]),
+ _uuid.UUID(bytes=m[1])))
+
+
+def _convert_source_monotonic(s):
+ return _datetime.timedelta(microseconds=int(s))
+
+
+def _convert_realtime(t):
+ return _datetime.datetime.fromtimestamp(t / 1000000)
+
+
+def _convert_timestamp(s):
+ return _datetime.datetime.fromtimestamp(int(s) / 1000000)
+
+
+def _convert_trivial(x):
+ return x
+
+if _sys.version_info >= (3,):
+ def _convert_uuid(s):
+ return _uuid.UUID(s.decode())
+else:
+ _convert_uuid = _uuid.UUID
+
+DEFAULT_CONVERTERS = {
+ 'MESSAGE_ID': _convert_uuid,
+ '_MACHINE_ID': _convert_uuid,
+ '_BOOT_ID': _convert_uuid,
+ 'PRIORITY': int,
+ 'LEADER': int,
+ 'SESSION_ID': int,
+ 'USERSPACE_USEC': int,
+ 'INITRD_USEC': int,
+ 'KERNEL_USEC': int,
+ '_UID': int,
+ '_GID': int,
+ '_PID': int,
+ 'SYSLOG_FACILITY': int,
+ 'SYSLOG_PID': int,
+ '_AUDIT_SESSION': int,
+ '_AUDIT_LOGINUID': int,
+ '_SYSTEMD_SESSION': int,
+ '_SYSTEMD_OWNER_UID': int,
+ 'CODE_LINE': int,
+ 'ERRNO': int,
+ 'EXIT_STATUS': int,
+ '_SOURCE_REALTIME_TIMESTAMP': _convert_timestamp,
+ '__REALTIME_TIMESTAMP': _convert_realtime,
+ '_SOURCE_MONOTONIC_TIMESTAMP': _convert_source_monotonic,
+ '__MONOTONIC_TIMESTAMP': _convert_monotonic,
+ '__CURSOR': _convert_trivial,
+ 'COREDUMP': bytes,
+ 'COREDUMP_PID': int,
+ 'COREDUMP_UID': int,
+ 'COREDUMP_GID': int,
+ 'COREDUMP_SESSION': int,
+ 'COREDUMP_SIGNAL': int,
+ 'COREDUMP_TIMESTAMP': _convert_timestamp,
+}
+
+_IDENT_CHARACTER = set('ABCDEFGHIJKLMNOPQRTSUVWXYZ_0123456789')
+
+
+def _valid_field_name(s):
+ return not (set(s) - _IDENT_CHARACTER)
+
+
+class Reader(_Reader):
+ """Access systemd journal entries.
+
+ Entries are subject to filtering and limits, see `add_match`, `this_boot`,
+ `this_machine` functions and the `data_treshold` attribute.
+
+ Note that in order to access the system journal, a non-root user must have
+ the necessary privileges, see journalctl(1) for details. Unprivileged users
+ can access only their own journal.
+
+ Example usage to print out all informational or higher level messages for
+ systemd-udevd for this boot:
+
+ >>> from systemd import journal
+ >>> j = journal.Reader()
+ >>> j.this_boot()
+ >>> j.log_level(journal.LOG_INFO)
+ >>> j.add_match(_SYSTEMD_UNIT="systemd-udevd.service")
+ >>> for entry in j: # doctest: +SKIP
+ ... print(entry['MESSAGE'])
+ starting version ...
+
+ See systemd.journal-fields(7) for more info on typical fields found in the
+ journal.
+
+ """
+ def __init__(self, flags=None, path=None, files=None, converters=None):
+ """Create a new Reader.
+
+ Argument `flags` defines the open flags of the journal, which can be one
+ of, or ORed combination of constants: LOCAL_ONLY (default) opens journal
+ on local machine only; RUNTIME_ONLY opens only volatile journal files;
+ and SYSTEM_ONLY opens only journal files of system services and the kernel.
+
+ Argument `path` is the directory of journal files, either a file system
+ path or a file descriptor. Note that `flags`, `path`, and `files` are
+ exclusive.
+
+ Argument `converters` is a dictionary which updates the
+ DEFAULT_CONVERTERS to convert journal field values. Field names are used
+ as keys into this dictionary. The values must be single argument
+ functions, which take a `bytes` object and return a converted
+ value. When there's no entry for a field name, then the default UTF-8
+ decoding will be attempted. If the conversion fails with a ValueError,
+ unconverted bytes object will be returned. (Note that ValueEror is a
+ superclass of UnicodeDecodeError).
+
+ Reader implements the context manager protocol: the journal will be
+ closed when exiting the block.
+ """
+ if flags is None:
+ if path is None and files is None:
+ # This mimics journalctl behaviour of default to local journal only
+ flags = LOCAL_ONLY
+ else:
+ flags = 0
+
+ super(Reader, self).__init__(flags, path, files)
+ if _sys.version_info >= (3, 3):
+ self.converters = _ChainMap()
+ if converters is not None:
+ self.converters.maps.append(converters)
+ self.converters.maps.append(DEFAULT_CONVERTERS)
+ else:
+ self.converters = DEFAULT_CONVERTERS.copy()
+ if converters is not None:
+ self.converters.update(converters)
+
+ def _convert_field(self, key, value):
+ """Convert value using self.converters[key].
+
+ If `key` is not present in self.converters, a standard unicode decoding
+ will be attempted. If the conversion (either key-specific or the
+ default one) fails with a ValueError, the original bytes object will be
+ returned.
+ """
+ convert = self.converters.get(key, bytes.decode)
+ try:
+ return convert(value)
+ except ValueError:
+ # Leave in default bytes
+ return value
+
+ def _convert_entry(self, entry):
+ """Convert entire journal entry utilising _convert_field."""
+ result = {}
+ for key, value in entry.items():
+ if isinstance(value, list):
+ result[key] = [self._convert_field(key, val) for val in value]
+ else:
+ result[key] = self._convert_field(key, value)
+ return result
+
+ def __iter__(self):
+ """Return self.
+
+ Part of the iterator protocol.
+ """
+ return self
+
+ def __next__(self):
+ """Return the next entry in the journal.
+
+ Returns self.get_next() or raises StopIteration.
+
+ Part of the iterator protocol.
+ """
+ ans = self.get_next()
+ if ans:
+ return ans
+ else:
+ raise StopIteration()
+
+ if _sys.version_info < (3,):
+ next = __next__
+
+ def add_match(self, *args, **kwargs):
+ """Add one or more matches to the filter journal log entries.
+
+ All matches of different field are combined with logical AND, and
+ matches of the same field are automatically combined with logical OR.
+ Matches can be passed as strings of form "FIELD=value", or keyword
+ arguments FIELD="value".
+ """
+ args = list(args)
+ args.extend(_make_line(key, val) for key, val in kwargs.items())
+ for arg in args:
+ super(Reader, self).add_match(arg)
+
+ def get_next(self, skip=1):
+ r"""Return the next log entry as a dictionary.
+
+ Entries will be processed with converters specified during Reader
+ creation.
+
+ Optional `skip` value will return the `skip`-th log entry.
+
+ Currently a standard dictionary of fields is returned, but in the
+ future this might be changed to a different mapping type, so the
+ calling code should not make assumptions about a specific type.
+ """
+ if super(Reader, self)._next(skip):
+ entry = super(Reader, self)._get_all()
+ if entry:
+ entry['__REALTIME_TIMESTAMP'] = self._get_realtime()
+ entry['__MONOTONIC_TIMESTAMP'] = self._get_monotonic()
+ entry['__CURSOR'] = self._get_cursor()
+ return self._convert_entry(entry)
+ return dict()
+
+ def get_previous(self, skip=1):
+ r"""Return the previous log entry.
+
+ Equivalent to get_next(-skip).
+
+ Optional `skip` value will return the -`skip`-th log entry.
+
+ Entries will be processed with converters specified during Reader
+ creation.
+
+ Currently a standard dictionary of fields is returned, but in the
+ future this might be changed to a different mapping type, so the
+ calling code should not make assumptions about a specific type.
+ """
+ return self.get_next(-skip)
+
+ def query_unique(self, field):
+ """Return a list of unique values appearing in the journal for the given
+ `field`.
+
+ Note this does not respect any journal matches.
+
+ Entries will be processed with converters specified during
+ Reader creation.
+ """
+ return set(self._convert_field(field, value)
+ for value in super(Reader, self).query_unique(field))
+
+ def wait(self, timeout=None):
+ """Wait for a change in the journal.
+
+ `timeout` is the maximum time in seconds to wait, or None which
+ means to wait forever.
+
+ Returns one of NOP (no change), APPEND (new entries have been added to
+ the end of the journal), or INVALIDATE (journal files have been added or
+ removed).
+ """
+ us = -1 if timeout is None else int(timeout * 1000000)
+ return super(Reader, self).wait(us)
+
+ def seek_realtime(self, realtime):
+ """Seek to a matching journal entry nearest to `timestamp` time.
+
+ Argument `realtime` must be either an integer UNIX timestamp (in
+ microseconds since the beginning of the UNIX epoch), or an float UNIX
+ timestamp (in seconds since the beginning of the UNIX epoch), or a
+ datetime.datetime instance. The integer form is deprecated.
+
+ >>> import time
+ >>> from systemd import journal
+
+ >>> yesterday = time.time() - 24 * 60**2
+ >>> j = journal.Reader()
+ >>> j.seek_realtime(yesterday)
+ """
+ if isinstance(realtime, _datetime.datetime):
+ realtime = int(float(realtime.strftime("%s.%f")) * 1000000)
+ elif not isinstance(realtime, int):
+ realtime = int(realtime * 1000000)
+ return super(Reader, self).seek_realtime(realtime)
+
+ def seek_monotonic(self, monotonic, bootid=None):
+ """Seek to a matching journal entry nearest to `monotonic` time.
+
+ Argument `monotonic` is a timestamp from boot in either seconds or a
+ datetime.timedelta instance. Argument `bootid` is a string or UUID
+ representing which boot the monotonic time is reference to. Defaults to
+ current bootid.
+ """
+ if isinstance(monotonic, _datetime.timedelta):
+ monotonic = monotonic.total_seconds()
+ monotonic = int(monotonic * 1000000)
+ if isinstance(bootid, _uuid.UUID):
+ bootid = bootid.hex
+ return super(Reader, self).seek_monotonic(monotonic, bootid)
+
+ def log_level(self, level):
+ """Set maximum log `level` by setting matches for PRIORITY.
+ """
+ if 0 <= level <= 7:
+ for i in range(level+1):
+ self.add_match(PRIORITY="%d" % i)
+ else:
+ raise ValueError("Log level must be 0 <= level <= 7")
+
+ def messageid_match(self, messageid):
+ """Add match for log entries with specified `messageid`.
+
+ `messageid` can be string of hexadicimal digits or a UUID
+ instance. Standard message IDs can be found in systemd.id128.
+
+ Equivalent to add_match(MESSAGE_ID=`messageid`).
+ """
+ if isinstance(messageid, _uuid.UUID):
+ messageid = messageid.hex
+ self.add_match(MESSAGE_ID=messageid)
+
+ def this_boot(self, bootid=None):
+ """Add match for _BOOT_ID for current boot or the specified boot ID.
+
+ If specified, bootid should be either a UUID or a 32 digit hex number.
+
+ Equivalent to add_match(_BOOT_ID='bootid').
+ """
+ if bootid is None:
+ bootid = _id128.get_boot().hex
+ else:
+ bootid = getattr(bootid, 'hex', bootid)
+ self.add_match(_BOOT_ID=bootid)
+
+ def this_machine(self, machineid=None):
+ """Add match for _MACHINE_ID equal to the ID of this machine.
+
+ If specified, machineid should be either a UUID or a 32 digit hex
+ number.
+
+ Equivalent to add_match(_MACHINE_ID='machineid').
+ """
+ if machineid is None:
+ machineid = _id128.get_machine().hex
+ else:
+ machineid = getattr(machineid, 'hex', machineid)
+ self.add_match(_MACHINE_ID=machineid)
+
+
+def get_catalog(mid):
+ """Return catalog entry for the specified ID.
+
+ `mid` should be either a UUID or a 32 digit hex number.
+ """
+ if isinstance(mid, _uuid.UUID):
+ mid = mid.hex
+ return _get_catalog(mid)
+
+
+def _make_line(field, value):
+ if isinstance(value, bytes):
+ return field.encode('utf-8') + b'=' + value
+ elif isinstance(value, str):
+ return field + '=' + value
+ else:
+ return field + '=' + str(value)
+
+
+def send(MESSAGE, MESSAGE_ID=None,
+ CODE_FILE=None, CODE_LINE=None, CODE_FUNC=None,
+ **kwargs):
+ r"""Send a message to the journal.
+
+ >>> from systemd import journal
+ >>> journal.send('Hello world')
+ >>> journal.send('Hello, again, world', FIELD2='Greetings!')
+ >>> journal.send('Binary message', BINARY=b'\xde\xad\xbe\xef')
+
+ Value of the MESSAGE argument will be used for the MESSAGE= field. MESSAGE
+ must be a string and will be sent as UTF-8 to the journal.
+
+ MESSAGE_ID can be given to uniquely identify the type of message. It must be
+ a string or a uuid.UUID object.
+
+ CODE_LINE, CODE_FILE, and CODE_FUNC can be specified to identify the caller.
+ Unless at least on of the three is given, values are extracted from the
+ stack frame of the caller of send(). CODE_FILE and CODE_FUNC must be
+ strings, CODE_LINE must be an integer.
+
+ Additional fields for the journal entry can only be specified as keyword
+ arguments. The payload can be either a string or bytes. A string will be
+ sent as UTF-8, and bytes will be sent as-is to the journal.
+
+ Other useful fields include PRIORITY, SYSLOG_FACILITY, SYSLOG_IDENTIFIER,
+ SYSLOG_PID.
+ """
+
+ args = ['MESSAGE=' + MESSAGE]
+
+ if MESSAGE_ID is not None:
+ id = getattr(MESSAGE_ID, 'hex', MESSAGE_ID)
+ args.append('MESSAGE_ID=' + id)
+
+ if CODE_LINE is CODE_FILE is CODE_FUNC is None:
+ CODE_FILE, CODE_LINE, CODE_FUNC = _traceback.extract_stack(limit=2)[0][:3]
+ if CODE_FILE is not None:
+ args.append('CODE_FILE=' + CODE_FILE)
+ if CODE_LINE is not None:
+ args.append('CODE_LINE={:d}'.format(CODE_LINE))
+ if CODE_FUNC is not None:
+ args.append('CODE_FUNC=' + CODE_FUNC)
+
+ args.extend(_make_line(key, val) for key, val in kwargs.items())
+ return sendv(*args)
+
+
+def stream(identifier=None, priority=LOG_INFO, level_prefix=False):
+ r"""Return a file object wrapping a stream to journal.
+
+ Log messages written to this file as simple newline sepearted text strings
+ are written to the journal.
+
+ The file will be line buffered, so messages are actually sent after a
+ newline character is written.
+
+ >>> from systemd import journal
+ >>> stream = journal.stream('myapp') # doctest: +SKIP
+ >>> res = stream.write('message...\n') # doctest: +SKIP
+
+ will produce the following message in the journal::
+
+ PRIORITY=7
+ SYSLOG_IDENTIFIER=myapp
+ MESSAGE=message...
+
+ If identifier is None, a suitable default based on sys.argv[0] will be used.
+
+ This interface can be used conveniently with the print function:
+
+ >>> from __future__ import print_function
+ >>> stream = journal.stream() # doctest: +SKIP
+ >>> print('message...', file=stream) # doctest: +SKIP
+
+ priority is the syslog priority, one of `LOG_EMERG`, `LOG_ALERT`,
+ `LOG_CRIT`, `LOG_ERR`, `LOG_WARNING`, `LOG_NOTICE`, `LOG_INFO`, `LOG_DEBUG`.
+
+ level_prefix is a boolean. If true, kernel-style log priority level prefixes
+ (such as '<1>') are interpreted. See sd-daemon(3) for more information.
+ """
+
+ if identifier is None:
+ if not _sys.argv or not _sys.argv[0] or _sys.argv[0] == '-c':
+ identifier = 'python'
+ else:
+ identifier = _sys.argv[0]
+
+ fd = stream_fd(identifier, priority, level_prefix)
+ return _os.fdopen(fd, 'w', 1)
+
+
+class JournalHandler(_logging.Handler):
+ """Journal handler class for the Python logging framework.
+
+ Please see the Python logging module documentation for an overview:
+ http://docs.python.org/library/logging.html.
+
+ To create a custom logger whose messages go only to journal:
+
+ >>> import logging
+ >>> log = logging.getLogger('custom_logger_name')
+ >>> log.propagate = False
+ >>> log.addHandler(JournalHandler())
+ >>> log.warning("Some message: %s", 'detail')
+
+ Note that by default, message levels `INFO` and `DEBUG` are ignored by the
+ logging framework. To enable those log levels:
+
+ >>> log.setLevel(logging.DEBUG)
+
+ To redirect all logging messages to journal regardless of where they come
+ from, attach it to the root logger:
+
+ >>> logging.root.addHandler(JournalHandler())
+
+ For more complex configurations when using `dictConfig` or `fileConfig`,
+ specify `systemd.journal.JournalHandler` as the handler class. Only
+ standard handler configuration options are supported: `level`, `formatter`,
+ `filters`.
+
+ To attach journal MESSAGE_ID, an extra field is supported:
+
+ >>> import uuid
+ >>> mid = uuid.UUID('0123456789ABCDEF0123456789ABCDEF')
+ >>> log.warning("Message with ID", extra={'MESSAGE_ID': mid})
+
+ Fields to be attached to all messages sent through this handler can be
+ specified as keyword arguments. This probably makes sense only for
+ SYSLOG_IDENTIFIER and similar fields which are constant for the whole
+ program:
+
+ >>> JournalHandler(SYSLOG_IDENTIFIER='my-cool-app')
+ <...JournalHandler ...>
+
+ The following journal fields will be sent: `MESSAGE`, `PRIORITY`,
+ `THREAD_NAME`, `CODE_FILE`, `CODE_LINE`, `CODE_FUNC`, `LOGGER` (name as
+ supplied to getLogger call), `MESSAGE_ID` (optional, see above),
+ `SYSLOG_IDENTIFIER` (defaults to sys.argv[0]).
+
+ The function used to actually send messages can be overridden using
+ the `sender_function` parameter.
+ """
+
+ def __init__(self, level=_logging.NOTSET, sender_function=send, **kwargs):
+ super(JournalHandler, self).__init__(level)
+
+ for name in kwargs:
+ if not _valid_field_name(name):
+ raise ValueError('Invalid field name: ' + name)
+ if 'SYSLOG_IDENTIFIER' not in kwargs:
+ kwargs['SYSLOG_IDENTIFIER'] = _sys.argv[0]
+
+ self.send = sender_function
+ self._extra = kwargs
+
+ def emit(self, record):
+ """Write `record` as a journal event.
+
+ MESSAGE is taken from the message provided by the user, and PRIORITY,
+ LOGGER, THREAD_NAME, CODE_{FILE,LINE,FUNC} fields are appended
+ automatically. In addition, record.MESSAGE_ID will be used if present.
+ """
+ try:
+ msg = self.format(record)
+ pri = self.map_priority(record.levelno)
+ # defaults
+ extras = self._extra.copy()
+
+ # higher priority
+ if record.exc_text:
+ extras['EXCEPTION_TEXT'] = record.exc_text
+
+ if record.exc_info:
+ extras['EXCEPTION_INFO'] = record.exc_info
+
+ if record.args:
+ extras['CODE_ARGS'] = str(record.args)
+
+ # explicit arguments — highest priority
+ extras.update(record.__dict__)
+
+ self.send(msg,
+ PRIORITY=format(pri),
+ LOGGER=record.name,
+ THREAD_NAME=record.threadName,
+ PROCESS_NAME=record.processName,
+ CODE_FILE=record.pathname,
+ CODE_LINE=record.lineno,
+ CODE_FUNC=record.funcName,
+ **extras)
+ except Exception:
+ self.handleError(record)
+
+ @staticmethod
+ def map_priority(levelno):
+ """Map logging levels to journald priorities.
+
+ Since Python log level numbers are "sparse", we have to map numbers in
+ between the standard levels too.
+ """
+ if levelno <= _logging.DEBUG:
+ return LOG_DEBUG
+ elif levelno <= _logging.INFO:
+ return LOG_INFO
+ elif levelno <= _logging.WARNING:
+ return LOG_WARNING
+ elif levelno <= _logging.ERROR:
+ return LOG_ERR
+ elif levelno <= _logging.CRITICAL:
+ return LOG_CRIT
+ else:
+ return LOG_ALERT
+
+ mapPriority = map_priority
diff --git a/systemd/login.c b/systemd/login.c
new file mode 100644
index 0000000..ff74dc7
--- /dev/null
+++ b/systemd/login.c
@@ -0,0 +1,373 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+
+ Copyright 2013 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
+
+ python-systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ python-systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#define PY_SSIZE_T_CLEAN
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wredundant-decls"
+#include <Python.h>
+#pragma GCC diagnostic pop
+
+#include "systemd/sd-login.h"
+#include "pyutil.h"
+#include "strv.h"
+
+PyDoc_STRVAR(module__doc__,
+ "Python interface to the libsystemd-login library."
+);
+
+#define helper(name) \
+static PyObject* name(PyObject *self, PyObject *args) { \
+ _cleanup_strv_free_ char **list = NULL; \
+ int r; \
+ PyObject *ans; \
+ \
+ assert(args == NULL); \
+ \
+ r = sd_get_##name(&list); \
+ if (r < 0) { \
+ errno = -r; \
+ return PyErr_SetFromErrno(PyExc_IOError); \
+ } \
+ \
+ ans = PyList_New(r); \
+ if (!ans) \
+ return NULL; \
+ \
+ for (r--; r >= 0; r--) { \
+ PyObject *s = unicode_FromString(list[r]); \
+ if (!s) { \
+ Py_DECREF(ans); \
+ return NULL; \
+ } \
+ \
+ PyList_SetItem(ans, r, s); \
+ } \
+ \
+ return ans; \
+}
+
+helper(seats)
+helper(sessions)
+helper(machine_names)
+#undef helper
+
+static PyObject* uids(PyObject *self, PyObject *args) {
+ _cleanup_free_ uid_t *list = NULL;
+ int r;
+ PyObject *ans;
+
+ assert(args == NULL);
+
+ r = sd_get_uids(&list);
+ if (r < 0) {
+ errno = -r;
+ return PyErr_SetFromErrno(PyExc_IOError);
+ }
+
+ ans = PyList_New(r);
+ if (!ans)
+ return NULL;
+
+ for (r--; r >= 0; r--) {
+ PyObject *s = long_FromLong(list[r]);
+ if (!s) {
+ Py_DECREF(ans);
+ return NULL;
+ }
+
+ PyList_SetItem(ans, r, s);
+ }
+
+ return ans;
+}
+
+PyDoc_STRVAR(seats__doc__,
+ "seats() -> list\n\n"
+ "Returns a list of currently available local seats.\n"
+ "Wraps sd_get_seats(3)."
+);
+
+PyDoc_STRVAR(sessions__doc__,
+ "sessions() -> list\n\n"
+ "Returns a list of current login sessions.\n"
+ "Wraps sd_get_sessions(3)."
+);
+
+PyDoc_STRVAR(machine_names__doc__,
+ "machine_names() -> list\n\n"
+ "Returns a list of currently running virtual machines\n"
+ "and containers on the system.\n"
+ "Wraps sd_get_machine_names(3)."
+);
+
+PyDoc_STRVAR(uids__doc__,
+ "uids() -> list\n\n"
+ "Returns a list of uids of users who currently have login sessions.\n"
+ "Wraps sd_get_uids(3)."
+);
+
+static PyMethodDef methods[] = {
+ { "seats", seats, METH_NOARGS, seats__doc__},
+ { "sessions", sessions, METH_NOARGS, sessions__doc__},
+ { "machine_names", machine_names, METH_NOARGS, machine_names__doc__},
+ { "uids", uids, METH_NOARGS, uids__doc__},
+ {} /* Sentinel */
+};
+
+
+typedef struct {
+ PyObject_HEAD
+ sd_login_monitor *monitor;
+} Monitor;
+static PyTypeObject MonitorType;
+
+static void Monitor_dealloc(Monitor* self) {
+ sd_login_monitor_unref(self->monitor);
+ Py_TYPE(self)->tp_free((PyObject*)self);
+}
+
+PyDoc_STRVAR(Monitor__doc__,
+ "Monitor([category]) -> ...\n\n"
+ "Monitor may be used to monitor login sessions, users, seats, and virtual\n"
+ "machines/containers. Monitor provides a file descriptor which can be\n"
+ "integrated in an external event loop.\n\n"
+ "See man:sd_login_monitor_new(3) for the details about what can be monitored.");
+static int Monitor_init(Monitor *self, PyObject *args, PyObject *keywds) {
+ const char *category = NULL;
+ int r;
+
+ static const char* const kwlist[] = {"category", NULL};
+ if (!PyArg_ParseTupleAndKeywords(args, keywds, "|z:__init__", (char**) kwlist,
+ &category))
+ return -1;
+
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_login_monitor_new(category, &self->monitor);
+ Py_END_ALLOW_THREADS
+
+ return set_error(r, NULL, "Invalid category");
+}
+
+
+PyDoc_STRVAR(Monitor_fileno__doc__,
+ "fileno() -> int\n\n"
+ "Get a file descriptor to poll for events.\n"
+ "This method wraps sd_login_monitor_get_fd(3).");
+static PyObject* Monitor_fileno(Monitor *self, PyObject *args) {
+ int fd = sd_login_monitor_get_fd(self->monitor);
+ set_error(fd, NULL, NULL);
+ if (fd < 0)
+ return NULL;
+ return long_FromLong(fd);
+}
+
+
+PyDoc_STRVAR(Monitor_get_events__doc__,
+ "get_events() -> int\n\n"
+ "Returns a mask of poll() events to wait for on the file descriptor returned\n"
+ "by .fileno().\n\n"
+ "See man:sd_login_monitor_get_events(3) for further discussion.");
+static PyObject* Monitor_get_events(Monitor *self, PyObject *args) {
+ int r = sd_login_monitor_get_events(self->monitor);
+ set_error(r, NULL, NULL);
+ if (r < 0)
+ return NULL;
+ return long_FromLong(r);
+}
+
+
+PyDoc_STRVAR(Monitor_get_timeout__doc__,
+ "get_timeout() -> int or None\n\n"
+ "Returns a timeout value for usage in poll(), the time since the\n"
+ "epoch of clock_gettime(2) in microseconds, or None if no timeout\n"
+ "is necessary.\n\n"
+ "The return value must be converted to a relative timeout in\n"
+ "milliseconds if it is to be used as an argument for poll().\n"
+ "See man:sd_login_monitor_get_timeout(3) for further discussion.");
+static PyObject* Monitor_get_timeout(Monitor *self, PyObject *args) {
+ int r;
+ uint64_t t;
+
+ r = sd_login_monitor_get_timeout(self->monitor, &t);
+ set_error(r, NULL, NULL);
+ if (r < 0)
+ return NULL;
+
+ if (t == (uint64_t) -1)
+ Py_RETURN_NONE;
+
+ assert_cc(sizeof(unsigned long long) == sizeof(t));
+ return PyLong_FromUnsignedLongLong(t);
+}
+
+
+PyDoc_STRVAR(Monitor_get_timeout_ms__doc__,
+ "get_timeout_ms() -> int\n\n"
+ "Returns a timeout value suitable for usage in poll(), the value\n"
+ "returned by .get_timeout() converted to relative ms, or -1 if\n"
+ "no timeout is necessary.");
+static PyObject* Monitor_get_timeout_ms(Monitor *self, PyObject *args) {
+ int r;
+ uint64_t t;
+
+ r = sd_login_monitor_get_timeout(self->monitor, &t);
+ set_error(r, NULL, NULL);
+ if (r < 0)
+ return NULL;
+
+ return absolute_timeout(t);
+}
+
+
+PyDoc_STRVAR(Monitor_close__doc__,
+ "close() -> None\n\n"
+ "Free resources allocated by this Monitor object.\n"
+ "This method invokes sd_login_monitor_unref().\n"
+ "See man:sd_login_monitor_unref(3).");
+static PyObject* Monitor_close(Monitor *self, PyObject *args) {
+ assert(self);
+ assert(!args);
+
+ sd_login_monitor_unref(self->monitor);
+ self->monitor = NULL;
+ Py_RETURN_NONE;
+}
+
+
+PyDoc_STRVAR(Monitor_flush__doc__,
+ "flush() -> None\n\n"
+ "Reset the wakeup state of the monitor object.\n"
+ "This method invokes sd_login_monitor_flush().\n"
+ "See man:sd_login_monitor_flush(3).");
+static PyObject* Monitor_flush(Monitor *self, PyObject *args) {
+ assert(self);
+ assert(!args);
+
+ Py_BEGIN_ALLOW_THREADS
+ sd_login_monitor_flush(self->monitor);
+ Py_END_ALLOW_THREADS
+ Py_RETURN_NONE;
+}
+
+
+PyDoc_STRVAR(Monitor___enter____doc__,
+ "__enter__() -> self\n\n"
+ "Part of the context manager protocol.\n"
+ "Returns self.\n");
+static PyObject* Monitor___enter__(PyObject *self, PyObject *args) {
+ assert(self);
+ assert(!args);
+
+ Py_INCREF(self);
+ return self;
+}
+
+
+PyDoc_STRVAR(Monitor___exit____doc__,
+ "__exit__(type, value, traceback) -> None\n\n"
+ "Part of the context manager protocol.\n"
+ "Closes the monitor..\n");
+static PyObject* Monitor___exit__(Monitor *self, PyObject *args) {
+ return Monitor_close(self, args);
+}
+
+
+static PyMethodDef Monitor_methods[] = {
+ {"fileno", (PyCFunction) Monitor_fileno, METH_NOARGS, Monitor_fileno__doc__},
+ {"get_events", (PyCFunction) Monitor_get_events, METH_NOARGS, Monitor_get_events__doc__},
+ {"get_timeout", (PyCFunction) Monitor_get_timeout, METH_NOARGS, Monitor_get_timeout__doc__},
+ {"get_timeout_ms", (PyCFunction) Monitor_get_timeout_ms, METH_NOARGS, Monitor_get_timeout_ms__doc__},
+ {"close", (PyCFunction) Monitor_close, METH_NOARGS, Monitor_close__doc__},
+ {"flush", (PyCFunction) Monitor_flush, METH_NOARGS, Monitor_flush__doc__},
+ {"__enter__", (PyCFunction) Monitor___enter__, METH_NOARGS, Monitor___enter____doc__},
+ {"__exit__", (PyCFunction) Monitor___exit__, METH_VARARGS, Monitor___exit____doc__},
+ {} /* Sentinel */
+};
+
+static PyTypeObject MonitorType = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ .tp_name = "login.Monitor",
+ .tp_basicsize = sizeof(Monitor),
+ .tp_dealloc = (destructor) Monitor_dealloc,
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ .tp_doc = Monitor__doc__,
+ .tp_methods = Monitor_methods,
+ .tp_init = (initproc) Monitor_init,
+ .tp_new = PyType_GenericNew,
+};
+
+#if PY_MAJOR_VERSION < 3
+
+DISABLE_WARNING_MISSING_PROTOTYPES;
+PyMODINIT_FUNC initlogin(void) {
+ PyObject *m;
+
+ if (PyType_Ready(&MonitorType) < 0)
+ return;
+
+ m = Py_InitModule3("login", methods, module__doc__);
+ if (m == NULL)
+ return;
+
+ PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
+
+ Py_INCREF(&MonitorType);
+ PyModule_AddObject(m, "Monitor", (PyObject *) &MonitorType);
+}
+REENABLE_WARNING;
+
+#else
+
+static struct PyModuleDef module = {
+ PyModuleDef_HEAD_INIT,
+ "login", /* name of module */
+ module__doc__, /* module documentation, may be NULL */
+ -1, /* size of per-interpreter state of the module */
+ methods
+};
+
+DISABLE_WARNING_MISSING_PROTOTYPES;
+PyMODINIT_FUNC PyInit_login(void) {
+ PyObject *m;
+
+ if (PyType_Ready(&MonitorType) < 0)
+ return NULL;
+
+ m = PyModule_Create(&module);
+ if (m == NULL)
+ return NULL;
+
+ if (PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
+ Py_DECREF(m);
+ return NULL;
+ }
+
+ Py_INCREF(&MonitorType);
+ if (PyModule_AddObject(m, "Monitor", (PyObject *) &MonitorType)) {
+ Py_DECREF(&MonitorType);
+ Py_DECREF(m);
+ return NULL;
+ }
+
+ return m;
+}
+REENABLE_WARNING;
+
+#endif
diff --git a/systemd/macro.h b/systemd/macro.h
new file mode 100644
index 0000000..1b0db1d
--- /dev/null
+++ b/systemd/macro.h
@@ -0,0 +1,59 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+
+ Copyright 2010 Lennart Poettering
+
+ python-systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ python-systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#define DISABLE_WARNING_MISSING_PROTOTYPES \
+ _Pragma("GCC diagnostic push"); \
+ _Pragma("GCC diagnostic ignored \"-Wmissing-prototypes\"")
+
+#define REENABLE_WARNING \
+ _Pragma("GCC diagnostic pop")
+
+#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
+ static inline void func##p(type *p) { \
+ if (*p) \
+ func(*p); \
+ } \
+ struct __useless_struct_to_allow_trailing_semicolon__
+
+#define new0(t, n) ((t*) calloc((n), sizeof(t)))
+#define alloca0(n) \
+ ({ \
+ char *_new_; \
+ size_t _len_ = n; \
+ _new_ = alloca(_len_); \
+ (void *) memset(_new_, 0, _len_); \
+ })
+
+#define _cleanup_(x) __attribute__((cleanup(x)))
+
+static inline void freep(void *p) {
+ free(*(void**) p);
+}
+
+#define _cleanup_free_ _cleanup_(freep)
+
+#if defined(static_assert)
+# define assert_cc(expr) \
+ static_assert(expr, #expr)
+#else
+# define assert_cc(expr)
+#endif
diff --git a/systemd/pyutil.c b/systemd/pyutil.c
new file mode 100644
index 0000000..cd91ec3
--- /dev/null
+++ b/systemd/pyutil.c
@@ -0,0 +1,79 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+
+ Copyright 2013 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
+
+ python-systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ python-systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <Python.h>
+#include "pyutil.h"
+
+void cleanup_Py_DECREFp(PyObject **p) {
+ if (!*p)
+ return;
+
+ Py_DECREF(*p);
+}
+
+PyObject* absolute_timeout(uint64_t t) {
+ if (t == (uint64_t) -1)
+ return PyLong_FromLong(-1);
+ else {
+ struct timespec ts;
+ uint64_t n;
+ int msec;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
+ msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
+
+ return PyLong_FromLong(msec);
+ }
+}
+
+int set_error(int r, const char* path, const char* invalid_message) {
+ if (r >= 0)
+ return r;
+ if (r == -EINVAL && invalid_message)
+ PyErr_SetString(PyExc_ValueError, invalid_message);
+ else if (r == -ENOMEM)
+ PyErr_SetString(PyExc_MemoryError, "Not enough memory");
+ else {
+ errno = -r;
+ PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
+ }
+ return -1;
+}
+
+#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
+int Unicode_FSConverter(PyObject* obj, void *_result) {
+ PyObject **result = _result;
+
+ assert(result);
+
+ if (!obj)
+ /* cleanup: we don't return Py_CLEANUP_SUPPORTED, so
+ * we can assume that it was PyUnicode_FSConverter. */
+ return PyUnicode_FSConverter(obj, result);
+
+ if (obj == Py_None) {
+ *result = NULL;
+ return 1;
+ }
+
+ return PyUnicode_FSConverter(obj, result);
+}
+#endif
diff --git a/systemd/pyutil.h b/systemd/pyutil.h
new file mode 100644
index 0000000..f840e87
--- /dev/null
+++ b/systemd/pyutil.h
@@ -0,0 +1,53 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+
+ Copyright 2013 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
+
+ python-systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ python-systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#ifndef Py_TYPE
+/* avoid duplication warnings from errors in Python 2.7 headers */
+# include <Python.h>
+#endif
+
+void cleanup_Py_DECREFp(PyObject **p);
+PyObject* absolute_timeout(uint64_t t);
+int set_error(int r, const char* path, const char* invalid_message);
+
+#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
+int Unicode_FSConverter(PyObject* obj, void *_result);
+#endif
+
+#define _cleanup_Py_DECREF_ _cleanup_(cleanup_Py_DECREFp)
+
+#if PY_MAJOR_VERSION >=3
+# define unicode_FromStringAndSize PyUnicode_FromStringAndSize
+# define unicode_FromString PyUnicode_FromString
+# define long_FromLong PyLong_FromLong
+# define long_FromSize_t PyLong_FromSize_t
+# define long_Check PyLong_Check
+# define long_AsLong PyLong_AsLong
+#else
+/* Python 3 type naming convention is used */
+# define unicode_FromStringAndSize PyString_FromStringAndSize
+# define unicode_FromString PyString_FromString
+# define long_FromLong PyInt_FromLong
+# define long_FromSize_t PyInt_FromSize_t
+# define long_Check PyInt_Check
+# define long_AsLong PyInt_AsLong
+#endif
diff --git a/systemd/strv.c b/systemd/strv.c
new file mode 100644
index 0000000..0ba62a3
--- /dev/null
+++ b/systemd/strv.c
@@ -0,0 +1,39 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+
+ Copyright 2010 Lennart Poettering
+
+ python-systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ python-systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdlib.h>
+
+void strv_clear(char **l) {
+ char **k;
+
+ if (!l)
+ return;
+
+ for (k = l; *k; k++)
+ free(*k);
+
+ *l = NULL;
+}
+
+char **strv_free(char **l) {
+ strv_clear(l);
+ free(l);
+ return NULL;
+}
diff --git a/systemd/strv.h b/systemd/strv.h
new file mode 100644
index 0000000..3ce5505
--- /dev/null
+++ b/systemd/strv.h
@@ -0,0 +1,25 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+
+ Copyright 2010 Lennart Poettering
+
+ python-systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ python-systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with python-systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "macro.h"
+
+char **strv_free(char **l);
+DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
+#define _cleanup_strv_free_ _cleanup_(strv_freep)
diff --git a/systemd/test/test_daemon.py b/systemd/test/test_daemon.py
new file mode 100644
index 0000000..1ddb55e
--- /dev/null
+++ b/systemd/test/test_daemon.py
@@ -0,0 +1,304 @@
+import sys
+import os
+import posix
+import socket
+import contextlib
+import errno
+from systemd.daemon import (booted,
+ is_fifo, _is_fifo,
+ is_socket, _is_socket,
+ is_socket_inet, _is_socket_inet,
+ is_socket_unix, _is_socket_unix,
+ is_socket_sockaddr, _is_socket_sockaddr,
+ is_mq, _is_mq,
+ listen_fds,
+ notify)
+
+import pytest
+
+@contextlib.contextmanager
+def skip_enosys():
+ try:
+ yield
+ except OSError as e:
+ if e.errno == errno.ENOSYS:
+ pytest.skip()
+ raise
+
+@contextlib.contextmanager
+def closing_socketpair(family):
+ pair = socket.socketpair(family)
+ try:
+ yield pair
+ finally:
+ pair[0].close()
+ pair[1].close()
+
+
+def test_booted():
+ if os.path.exists('/run/systemd/system'):
+ # assume we are running under systemd
+ assert booted()
+ else:
+ # don't assume anything
+ assert booted() in {False, True}
+
+def test__is_fifo(tmpdir):
+ path = tmpdir.join('test.fifo').strpath
+ posix.mkfifo(path)
+ fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK)
+
+ assert _is_fifo(fd, None)
+ assert _is_fifo(fd, path)
+
+def test__is_fifo_file(tmpdir):
+ file = tmpdir.join('test.fifo')
+ file.write('boo')
+ path = file.strpath
+ fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK)
+
+ assert not _is_fifo(fd, None)
+ assert not _is_fifo(fd, path)
+
+def test__is_fifo_bad_fd(tmpdir):
+ path = tmpdir.join('test.fifo').strpath
+
+ with pytest.raises(OSError):
+ assert not _is_fifo(-1, None)
+
+ with pytest.raises(OSError):
+ assert not _is_fifo(-1, path)
+
+def test_is_fifo(tmpdir):
+ path = tmpdir.join('test.fifo').strpath
+ posix.mkfifo(path)
+ fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK)
+ file = os.fdopen(fd, 'r')
+
+ assert is_fifo(file, None)
+ assert is_fifo(file, path)
+ assert is_fifo(fd, None)
+ assert is_fifo(fd, path)
+
+def test_is_fifo_file(tmpdir):
+ file = tmpdir.join('test.fifo')
+ file.write('boo')
+ path = file.strpath
+ fd = os.open(path, os.O_RDONLY|os.O_NONBLOCK)
+ file = os.fdopen(fd, 'r')
+
+ assert not is_fifo(file, None)
+ assert not is_fifo(file, path)
+ assert not is_fifo(fd, None)
+ assert not is_fifo(fd, path)
+
+def test_is_fifo_bad_fd(tmpdir):
+ path = tmpdir.join('test.fifo').strpath
+
+ with pytest.raises(OSError):
+ assert not is_fifo(-1, None)
+
+ with pytest.raises(OSError):
+ assert not is_fifo(-1, path)
+
+def is_mq_wrapper(arg):
+ try:
+ return is_mq(arg)
+ except OSError as error:
+ # systemd < 227 compatibility
+ assert error.errno == errno.EBADF
+ return False
+
+def _is_mq_wrapper(arg):
+ try:
+ return _is_mq(arg)
+ except OSError as error:
+ # systemd < 227 compatibility
+ assert error.errno == errno.EBADF
+ return False
+
+def test_no_mismatch():
+ with closing_socketpair(socket.AF_UNIX) as pair:
+ for sock in pair:
+ assert not is_fifo(sock)
+ assert not is_mq_wrapper(sock)
+ assert not is_socket_inet(sock)
+ with skip_enosys():
+ assert not is_socket_sockaddr(sock, '127.0.0.1:2000')
+
+ fd = sock.fileno()
+ assert not is_fifo(fd)
+ assert not is_mq_wrapper(fd)
+ assert not is_socket_inet(fd)
+ with skip_enosys():
+ assert not is_socket_sockaddr(fd, '127.0.0.1:2000')
+
+ assert not _is_fifo(fd)
+ assert not _is_mq_wrapper(fd)
+ assert not _is_socket_inet(fd)
+ with skip_enosys():
+ assert not _is_socket_sockaddr(fd, '127.0.0.1:2000')
+
+def test_is_socket():
+ with closing_socketpair(socket.AF_UNIX) as pair:
+ for sock in pair:
+ for arg in (sock, sock.fileno()):
+ assert is_socket(arg)
+ assert is_socket(arg, socket.AF_UNIX)
+ assert not is_socket(arg, socket.AF_INET)
+ assert is_socket(arg, socket.AF_UNIX, socket.SOCK_STREAM)
+ assert not is_socket(arg, socket.AF_INET, socket.SOCK_DGRAM)
+ with skip_enosys():
+ assert not is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0)
+
+ assert _is_socket(arg)
+ assert _is_socket(arg, socket.AF_UNIX)
+ assert not _is_socket(arg, socket.AF_INET)
+ assert _is_socket(arg, socket.AF_UNIX, socket.SOCK_STREAM)
+ assert not _is_socket(arg, socket.AF_INET, socket.SOCK_DGRAM)
+ with skip_enosys():
+ assert not _is_socket_sockaddr(arg, '8.8.8.8:2000', socket.SOCK_DGRAM, 0, 0)
+
+def test_is_socket_sockaddr():
+ with contextlib.closing(socket.socket(socket.AF_INET)) as sock:
+ sock.bind(('127.0.0.1', 0))
+ addr, port = sock.getsockname()
+ port = ':{}'.format(port)
+
+ for listening in (0, 1):
+ for arg in (sock, sock.fileno()):
+ with skip_enosys():
+ assert is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_STREAM)
+ with skip_enosys():
+ assert is_socket_sockaddr(arg, '127.0.0.1' + port, socket.SOCK_STREAM)
+
+ with skip_enosys():
+ assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=listening)
+ with skip_enosys():
+ assert is_socket_sockaddr(arg, '127.0.0.1' + port, listening=-1)
+ with skip_enosys():
+ assert not is_socket_sockaddr(arg, '127.0.0.1' + port, listening=not listening)
+
+ with pytest.raises(ValueError):
+ is_socket_sockaddr(arg, '127.0.0.1', flowinfo=123456)
+
+ with skip_enosys():
+ assert not is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM)
+ with skip_enosys():
+ assert not is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM)
+
+ with pytest.raises(ValueError):
+ _is_socket_sockaddr(arg, '127.0.0.1', 0, 123456)
+
+ with skip_enosys():
+ assert not _is_socket_sockaddr(arg, '129.168.11.11:23', socket.SOCK_STREAM)
+ with skip_enosys():
+ assert not _is_socket_sockaddr(arg, '127.0.0.1', socket.SOCK_DGRAM)
+
+ sock.listen(11)
+
+def test__is_socket():
+ with closing_socketpair(socket.AF_UNIX) as pair:
+ for sock in pair:
+ fd = sock.fileno()
+ assert _is_socket(fd)
+ assert _is_socket(fd, socket.AF_UNIX)
+ assert not _is_socket(fd, socket.AF_INET)
+ assert _is_socket(fd, socket.AF_UNIX, socket.SOCK_STREAM)
+ assert not _is_socket(fd, socket.AF_INET, socket.SOCK_DGRAM)
+
+ assert _is_socket(fd)
+ assert _is_socket(fd, socket.AF_UNIX)
+ assert not _is_socket(fd, socket.AF_INET)
+ assert _is_socket(fd, socket.AF_UNIX, socket.SOCK_STREAM)
+ assert not _is_socket(fd, socket.AF_INET, socket.SOCK_DGRAM)
+
+def test_is_socket_unix():
+ with closing_socketpair(socket.AF_UNIX) as pair:
+ for sock in pair:
+ for arg in (sock, sock.fileno()):
+ assert is_socket_unix(arg)
+ assert not is_socket_unix(arg, path="/no/such/path")
+ assert is_socket_unix(arg, socket.SOCK_STREAM)
+ assert not is_socket_unix(arg, socket.SOCK_DGRAM)
+
+def test__is_socket_unix():
+ with closing_socketpair(socket.AF_UNIX) as pair:
+ for sock in pair:
+ fd = sock.fileno()
+ assert _is_socket_unix(fd)
+ assert not _is_socket_unix(fd, 0, -1, "/no/such/path")
+ assert _is_socket_unix(fd, socket.SOCK_STREAM)
+ assert not _is_socket_unix(fd, socket.SOCK_DGRAM)
+
+def test_listen_fds_no_fds():
+ # make sure we have no fds to listen to
+ os.unsetenv('LISTEN_FDS')
+ os.unsetenv('LISTEN_PID')
+
+ assert listen_fds() == []
+ assert listen_fds(True) == []
+ assert listen_fds(False) == []
+
+def test_listen_fds():
+ os.environ['LISTEN_FDS'] = '3'
+ os.environ['LISTEN_PID'] = str(os.getpid())
+
+ assert listen_fds(False) == [3, 4, 5]
+ assert listen_fds(True) == [3, 4, 5]
+ assert listen_fds() == []
+
+def test_listen_fds_default_unset():
+ os.environ['LISTEN_FDS'] = '1'
+ os.environ['LISTEN_PID'] = str(os.getpid())
+
+ assert listen_fds(False) == [3]
+ assert listen_fds() == [3]
+ assert listen_fds() == []
+
+def test_notify_no_socket():
+ assert notify('READY=1') is False
+ with skip_enosys():
+ assert notify('FDSTORE=1', fds=[]) is False
+ assert notify('FDSTORE=1', fds=[1, 2]) is False
+ assert notify('FDSTORE=1', pid=os.getpid()) is False
+ assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,)) is False
+
+if sys.version_info >= (3,):
+ connection_error = ConnectionRefusedError
+else:
+ connection_error = OSError
+
+def test_notify_bad_socket():
+ os.environ['NOTIFY_SOCKET'] = '/dev/null'
+
+ with pytest.raises(connection_error):
+ notify('READY=1')
+ with pytest.raises(connection_error):
+ with skip_enosys():
+ notify('FDSTORE=1', fds=[])
+ with pytest.raises(connection_error):
+ notify('FDSTORE=1', fds=[1, 2])
+ with pytest.raises(connection_error):
+ notify('FDSTORE=1', pid=os.getpid())
+ with pytest.raises(connection_error):
+ notify('FDSTORE=1', pid=os.getpid(), fds=(1,))
+
+def test_notify_with_socket(tmpdir):
+ path = tmpdir.join('socket').strpath
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
+ try:
+ sock.bind(path)
+ except socket.error as e:
+ pytest.xfail('failed to bind socket (%s)' % e)
+ # SO_PASSCRED is not defined in python2.7
+ SO_PASSCRED = getattr(socket, 'SO_PASSCRED', 16)
+ sock.setsockopt(socket.SOL_SOCKET, SO_PASSCRED, 1)
+ os.environ['NOTIFY_SOCKET'] = path
+
+ assert notify('READY=1')
+ with skip_enosys():
+ assert notify('FDSTORE=1', fds=[])
+ assert notify('FDSTORE=1', fds=[1, 2])
+ assert notify('FDSTORE=1', pid=os.getpid())
+ assert notify('FDSTORE=1', pid=os.getpid(), fds=(1,))
diff --git a/systemd/test/test_journal.py b/systemd/test/test_journal.py
new file mode 100644
index 0000000..49e4279
--- /dev/null
+++ b/systemd/test/test_journal.py
@@ -0,0 +1,298 @@
+from __future__ import print_function
+import contextlib
+import datetime
+import errno
+import logging
+import os
+import time
+import uuid
+import traceback as _traceback
+
+from systemd import journal, id128
+from systemd.journal import _make_line
+
+import pytest
+
+TEST_MID = uuid.UUID('8441372f8dca4ca98694a6091fd8519f')
+TEST_MID2 = uuid.UUID('8441370000000000000000001fd85000')
+
+class MockSender:
+ def __init__(self):
+ self.buf = []
+
+ def send(self, MESSAGE, MESSAGE_ID=None,
+ CODE_FILE=None, CODE_LINE=None, CODE_FUNC=None,
+ **kwargs):
+ args = ['MESSAGE=' + MESSAGE]
+
+ if MESSAGE_ID is not None:
+ id = getattr(MESSAGE_ID, 'hex', MESSAGE_ID)
+ args.append('MESSAGE_ID=' + id)
+
+ if CODE_LINE is CODE_FILE is CODE_FUNC is None:
+ CODE_FILE, CODE_LINE, CODE_FUNC = _traceback.extract_stack(limit=2)[0][:3]
+ if CODE_FILE is not None:
+ args.append('CODE_FILE=' + CODE_FILE)
+ if CODE_LINE is not None:
+ args.append('CODE_LINE={:d}'.format(CODE_LINE))
+ if CODE_FUNC is not None:
+ args.append('CODE_FUNC=' + CODE_FUNC)
+
+ args.extend(_make_line(key, val) for key, val in kwargs.items())
+ self.buf.append(args)
+
+@contextlib.contextmanager
+def skip_oserror(code):
+ try:
+ yield
+ except (OSError, IOError) as e:
+ if e.errno == code:
+ pytest.skip()
+ raise
+
+@contextlib.contextmanager
+def skip_valueerror():
+ try:
+ yield
+ except ValueError:
+ pytest.skip()
+
+def test_priorities():
+ p = journal.JournalHandler.map_priority
+
+ assert p(logging.NOTSET) == journal.LOG_DEBUG
+ assert p(logging.DEBUG) == journal.LOG_DEBUG
+ assert p(logging.DEBUG - 1) == journal.LOG_DEBUG
+ assert p(logging.DEBUG + 1) == journal.LOG_INFO
+ assert p(logging.INFO - 1) == journal.LOG_INFO
+ assert p(logging.INFO) == journal.LOG_INFO
+ assert p(logging.INFO + 1) == journal.LOG_WARNING
+ assert p(logging.WARN - 1) == journal.LOG_WARNING
+ assert p(logging.WARN) == journal.LOG_WARNING
+ assert p(logging.WARN + 1) == journal.LOG_ERR
+ assert p(logging.ERROR - 1) == journal.LOG_ERR
+ assert p(logging.ERROR) == journal.LOG_ERR
+ assert p(logging.ERROR + 1) == journal.LOG_CRIT
+ assert p(logging.FATAL) == journal.LOG_CRIT
+ assert p(logging.CRITICAL) == journal.LOG_CRIT
+ assert p(logging.CRITICAL + 1) == journal.LOG_ALERT
+
+
+def test_journalhandler_init_exception():
+ kw = {' X ':3}
+ with pytest.raises(ValueError):
+ journal.JournalHandler(**kw)
+
+def test_journalhandler_init():
+ kw = {'X':3, 'X3':4}
+ journal.JournalHandler(logging.INFO, **kw)
+
+def test_journalhandler_info():
+ record = logging.LogRecord('test-logger', logging.INFO, 'testpath', 1, 'test', None, None)
+
+ sender = MockSender()
+ kw = {'X':3, 'X3':4, 'sender_function': sender.send}
+ handler = journal.JournalHandler(logging.INFO, **kw)
+ handler.emit(record)
+ assert len(sender.buf) == 1
+ assert 'X=3' in sender.buf[0]
+ assert 'X3=4' in sender.buf[0]
+
+def test_journalhandler_no_message_id():
+ record = logging.LogRecord('test-logger', logging.INFO, 'testpath', 1, 'test', None, None)
+ sender = MockSender()
+ handler = journal.JournalHandler(logging.INFO, sender_function=sender.send)
+ handler.emit(record)
+ assert len(sender.buf) == 1
+ assert all(not m.startswith('MESSAGE_ID=') for m in sender.buf[0])
+
+def test_journalhandler_message_id_on_handler():
+ record = logging.LogRecord('test-logger', logging.INFO, 'testpath', 1, 'test', None, None)
+ sender = MockSender()
+ handler = journal.JournalHandler(logging.INFO, sender_function=sender.send,
+ MESSAGE_ID=TEST_MID)
+ handler.emit(record)
+ assert len(sender.buf) == 1
+ assert 'MESSAGE_ID=' + TEST_MID.hex in sender.buf[0]
+
+def test_journalhandler_message_id_on_handler_hex():
+ record = logging.LogRecord('test-logger', logging.INFO, 'testpath', 1, 'test', None, None)
+ sender = MockSender()
+ handler = journal.JournalHandler(logging.INFO, sender_function=sender.send,
+ MESSAGE_ID=TEST_MID.hex)
+ handler.emit(record)
+ assert len(sender.buf) == 1
+ assert 'MESSAGE_ID=' + TEST_MID.hex in sender.buf[0]
+
+def test_journalhandler_message_id_on_message():
+ record = logging.LogRecord('test-logger', logging.INFO, 'testpath', 1, 'test', None, None)
+ record.__dict__['MESSAGE_ID'] = TEST_MID2
+ sender = MockSender()
+ handler = journal.JournalHandler(logging.INFO, sender_function=sender.send,
+ MESSAGE_ID=TEST_MID)
+ handler.emit(record)
+ assert len(sender.buf) == 1
+ assert 'MESSAGE_ID=' + TEST_MID2.hex in sender.buf[0]
+
+def test_journalhandler_message_id_on_message_hex():
+ record = logging.LogRecord('test-logger', logging.INFO, 'testpath', 1, 'test', None, None)
+ record.__dict__['MESSAGE_ID'] = TEST_MID2.hex
+ sender = MockSender()
+ handler = journal.JournalHandler(logging.INFO, sender_function=sender.send,
+ MESSAGE_ID=TEST_MID)
+ handler.emit(record)
+ assert len(sender.buf) == 1
+ assert 'MESSAGE_ID=' + TEST_MID2.hex in sender.buf[0]
+
+def test_reader_init_flags():
+ j1 = journal.Reader()
+ j2 = journal.Reader(journal.LOCAL_ONLY)
+ j3 = journal.Reader(journal.RUNTIME_ONLY)
+ j4 = journal.Reader(journal.SYSTEM_ONLY)
+ j5 = journal.Reader(journal.LOCAL_ONLY | journal.RUNTIME_ONLY | journal.SYSTEM_ONLY)
+ j6 = journal.Reader(0)
+
+def test_reader_os_root(tmpdir):
+ with pytest.raises(ValueError):
+ journal.Reader(journal.OS_ROOT)
+ with skip_valueerror():
+ j1 = journal.Reader(path=tmpdir.strpath,
+ flags=journal.OS_ROOT)
+ with skip_valueerror():
+ j2 = journal.Reader(path=tmpdir.strpath,
+ flags=journal.OS_ROOT | journal.CURRENT_USER)
+ j3 = journal.Reader(path=tmpdir.strpath,
+ flags=journal.OS_ROOT | journal.SYSTEM_ONLY)
+
+def test_reader_init_path(tmpdir):
+ j1 = journal.Reader(path=tmpdir.strpath)
+ journal.Reader(0, path=tmpdir.strpath)
+
+ j2 = journal.Reader(path=tmpdir.strpath)
+ journal.Reader(path=tmpdir.strpath)
+
+def test_reader_init_path_invalid_fd():
+ with pytest.raises(OSError):
+ journal.Reader(0, path=-1)
+
+def test_reader_init_path_nondirectory_fd():
+ with pytest.raises(OSError):
+ journal.Reader(0, path=0)
+
+def test_reader_init_path_fd(tmpdir):
+ fd = os.open(tmpdir.strpath, os.O_RDONLY)
+
+ with skip_oserror(errno.ENOSYS):
+ j1 = journal.Reader(path=fd)
+ assert list(j1) == []
+
+ with skip_valueerror():
+ j2 = journal.Reader(journal.SYSTEM, path=fd)
+ assert list(j2) == []
+
+ j3 = journal.Reader(journal.CURRENT_USER, path=fd)
+ assert list(j3) == []
+
+def test_reader_as_cm(tmpdir):
+ j = journal.Reader(path=tmpdir.strpath)
+ with j:
+ assert not j.closed
+ assert j.closed
+ # make sure that operations on the Reader fail
+ with pytest.raises(OSError):
+ next(j)
+
+def test_reader_messageid_match(tmpdir):
+ j = journal.Reader(path=tmpdir.strpath)
+ with j:
+ j.messageid_match(id128.SD_MESSAGE_JOURNAL_START)
+ j.messageid_match(id128.SD_MESSAGE_JOURNAL_STOP.hex)
+
+def test_reader_this_boot(tmpdir):
+ j = journal.Reader(path=tmpdir.strpath)
+ with j:
+ j.this_boot()
+ j.this_boot(TEST_MID)
+ j.this_boot(TEST_MID.hex)
+
+def test_reader_this_machine(tmpdir):
+ j = journal.Reader(path=tmpdir.strpath)
+ with j:
+ j.this_machine()
+ j.this_machine(TEST_MID)
+ j.this_machine(TEST_MID.hex)
+
+def test_reader_query_unique(tmpdir):
+ j = journal.Reader(path=tmpdir.strpath)
+ with j:
+ with skip_oserror(errno.ENOSYS):
+ ans = j.query_unique('FOOBAR')
+ assert isinstance(ans, set)
+ assert ans == set()
+
+def test_reader_enumerate_fields(tmpdir):
+ j = journal.Reader(path=tmpdir.strpath)
+ with j:
+ with skip_oserror(errno.ENOSYS):
+ ans = j.enumerate_fields()
+ assert isinstance(ans, set)
+ assert ans == set()
+
+def test_reader_has_runtime_files(tmpdir):
+ j = journal.Reader(path=tmpdir.strpath)
+ with j:
+ with skip_oserror(errno.ENOSYS):
+ ans = j.has_runtime_files()
+ assert ans is False
+
+def test_reader_has_persistent_files(tmpdir):
+ j = journal.Reader(path=tmpdir.strpath)
+ with j:
+ with skip_oserror(errno.ENOSYS):
+ ans = j.has_runtime_files()
+ assert ans is False
+
+def test_reader_converters(tmpdir):
+ converters = {'xxx' : lambda arg: 'yyy'}
+ j = journal.Reader(path=tmpdir.strpath, converters=converters)
+
+ val = j._convert_field('xxx', b'abc')
+ assert val == 'yyy'
+
+ val = j._convert_field('zzz', b'\200\200')
+ assert val == b'\200\200'
+
+def test_reader_convert_entry(tmpdir):
+ converters = {'x1' : lambda arg: 'yyy',
+ 'x2' : lambda arg: 'YYY'}
+ j = journal.Reader(path=tmpdir.strpath, converters=converters)
+
+ val = j._convert_entry({'x1' : b'abc',
+ 'y1' : b'\200\200',
+ 'x2' : [b'abc', b'def'],
+ 'y2' : [b'\200\200', b'\200\201']})
+ assert val == {'x1' : 'yyy',
+ 'y1' : b'\200\200',
+ 'x2' : ['YYY', 'YYY'],
+ 'y2' : [b'\200\200', b'\200\201']}
+
+def test_seek_realtime(tmpdir):
+ j = journal.Reader(path=tmpdir.strpath)
+
+ now = time.time()
+ j.seek_realtime(now)
+
+ j.seek_realtime(12345)
+
+ long_ago = datetime.datetime(1970, 5, 4)
+ j.seek_realtime(long_ago)
+
+def test_journal_stream():
+ # This will fail when running in a bare chroot without /run/systemd/journal/stdout
+ with skip_oserror(errno.ENOENT):
+ stream = journal.stream('test_journal.py')
+
+ res = stream.write('message...\n')
+ assert res in (11, None) # Python2 returns None
+
+ print('printed message...', file=stream)
diff --git a/systemd/test/test_login.py b/systemd/test/test_login.py
new file mode 100644
index 0000000..afb5f45
--- /dev/null
+++ b/systemd/test/test_login.py
@@ -0,0 +1,48 @@
+from __future__ import print_function
+import select
+import contextlib
+import errno
+
+from systemd import login
+
+import pytest
+
+@contextlib.contextmanager
+def skip_oserror(code):
+ try:
+ yield
+ except (OSError, IOError) as e:
+ if e.errno == code:
+ pytest.skip()
+ raise
+
+def test_seats():
+ # just check that we get some sequence back
+ with skip_oserror(errno.ENOENT):
+ seats = login.seats()
+ assert len(seats) >= 0
+
+def test_sessions():
+ with skip_oserror(errno.ENOENT):
+ sessions = login.sessions()
+ assert len(sessions) >= 0
+
+def test_machine_names():
+ with skip_oserror(errno.ENOENT):
+ machine_names = login.machine_names()
+ assert len(machine_names) >= 0
+
+def test_uids():
+ with skip_oserror(errno.ENOENT):
+ uids = login.uids()
+ assert len(uids) >= 0
+
+def test_monitor():
+ p = select.poll()
+
+ with skip_oserror(errno.ENOENT):
+ m = login.Monitor("machine")
+ p.register(m, m.get_events())
+ login.machine_names()
+ p.poll(1)
+ login.machine_names()
diff --git a/systemd/util.c b/systemd/util.c
new file mode 100644
index 0000000..e02c825
--- /dev/null
+++ b/systemd/util.c
@@ -0,0 +1,187 @@
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+/* stuff imported from systemd without any changes */
+
+#ifndef _GNU_SOURCE
+# define _GNU_SOURCE
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <net/if.h>
+
+#include "util.h"
+
+int safe_atou(const char *s, unsigned *ret_u) {
+ char *x = NULL;
+ unsigned long l;
+
+ assert(s);
+ assert(ret_u);
+
+ /* strtoul() is happy to parse negative values, and silently
+ * converts them to unsigned values without generating an
+ * error. We want a clean error, hence let's look for the "-"
+ * prefix on our own, and generate an error. But let's do so
+ * only after strtoul() validated that the string is clean
+ * otherwise, so that we return EINVAL preferably over
+ * ERANGE. */
+
+ errno = 0;
+ l = strtoul(s, &x, 0);
+ if (errno > 0)
+ return -errno;
+ if (!x || x == s || *x)
+ return -EINVAL;
+ if (s[0] == '-')
+ return -ERANGE;
+ if ((unsigned long) (unsigned) l != l)
+ return -ERANGE;
+
+ *ret_u = (unsigned) l;
+ return 0;
+}
+
+static bool socket_ipv6_is_supported(void) {
+ if (access("/proc/net/if_inet6", F_OK) != 0)
+ return false;
+
+ return true;
+}
+
+static int assign_address(const char *s,
+ uint16_t port,
+ union sockaddr_union *addr, unsigned *addr_len) {
+ int r;
+
+ /* IPv4 in w.x.y.z:p notation? */
+ r = inet_pton(AF_INET, s, &addr->in.sin_addr);
+ if (r < 0)
+ return -errno;
+
+ if (r > 0) {
+ /* Gotcha, it's a traditional IPv4 address */
+ addr->in.sin_family = AF_INET;
+ addr->in.sin_port = htobe16(port);
+ *addr_len = sizeof(struct sockaddr_in);
+ } else {
+ unsigned idx;
+
+ if (strlen(s) > IF_NAMESIZE-1)
+ return -EINVAL;
+
+ /* Uh, our last resort, an interface name */
+ idx = if_nametoindex(s);
+ if (idx == 0)
+ return -EINVAL;
+
+ addr->in6.sin6_family = AF_INET6;
+ addr->in6.sin6_port = htobe16(port);
+ addr->in6.sin6_scope_id = idx;
+ addr->in6.sin6_addr = in6addr_any;
+ *addr_len = sizeof(struct sockaddr_in6);
+ }
+
+ return 0;
+}
+
+
+int parse_sockaddr(const char *s,
+ union sockaddr_union *addr, unsigned *addr_len) {
+
+ char *e, *n;
+ unsigned u;
+ int r;
+
+ if (*s == '[') {
+ /* IPv6 in [x:.....:z]:p notation */
+
+ e = strchr(s+1, ']');
+ if (!e)
+ return -EINVAL;
+
+ n = strndupa(s+1, e-s-1);
+
+ errno = 0;
+ if (inet_pton(AF_INET6, n, &addr->in6.sin6_addr) <= 0)
+ return errno > 0 ? -errno : -EINVAL;
+
+ e++;
+ if (*e) {
+ if (*e != ':')
+ return -EINVAL;
+
+ e++;
+ r = safe_atou(e, &u);
+ if (r < 0)
+ return r;
+
+ if (u <= 0 || u > 0xFFFF)
+ return -EINVAL;
+
+ addr->in6.sin6_port = htobe16((uint16_t)u);
+ }
+
+ addr->in6.sin6_family = AF_INET6;
+ *addr_len = sizeof(struct sockaddr_in6);
+
+ } else {
+ e = strchr(s, ':');
+ if (e) {
+ r = safe_atou(e+1, &u);
+ if (r < 0)
+ return r;
+
+ if (u <= 0 || u > 0xFFFF)
+ return -EINVAL;
+
+ n = strndupa(s, e-s);
+ return assign_address(n, u, addr, addr_len);
+
+ } else {
+ r = safe_atou(s, &u);
+ if (r < 0)
+ return assign_address(s, 0, addr, addr_len);
+
+ /* Just a port */
+ if (u <= 0 || u > 0xFFFF)
+ return -EINVAL;
+
+ if (socket_ipv6_is_supported()) {
+ addr->in6.sin6_family = AF_INET6;
+ addr->in6.sin6_port = htobe16((uint16_t)u);
+ addr->in6.sin6_addr = in6addr_any;
+ *addr_len = sizeof(struct sockaddr_in6);
+ } else {
+ addr->in.sin_family = AF_INET;
+ addr->in.sin_port = htobe16((uint16_t)u);
+ addr->in.sin_addr.s_addr = INADDR_ANY;
+ *addr_len = sizeof(struct sockaddr_in);
+ }
+ }
+ }
+
+ return 0;
+}
diff --git a/systemd/util.h b/systemd/util.h
new file mode 100644
index 0000000..337920c
--- /dev/null
+++ b/systemd/util.h
@@ -0,0 +1,33 @@
+#pragma once
+
+/***
+ This file is part of systemd.
+
+ Copyright 2010 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <netinet/ip.h>
+#include <arpa/inet.h>
+
+union sockaddr_union {
+ struct sockaddr sa;
+ struct sockaddr_in in;
+ struct sockaddr_in6 in6;
+};
+
+int safe_atou(const char *s, unsigned *ret_u);
+int parse_sockaddr(const char *s,
+ union sockaddr_union *addr, unsigned *addr_len);
diff --git a/update-constants.py b/update-constants.py
new file mode 100644
index 0000000..2dcacda
--- /dev/null
+++ b/update-constants.py
@@ -0,0 +1,9 @@
+import sys
+
+for file in sys.argv[1:]:
+ lines = iter(open(file).read().splitlines())
+ for line in lines:
+ if line.startswith('#define SD_MESSAGE') and '_STR ' not in line:
+ if line.endswith('\\'):
+ line = line[:-1] + next(lines)
+ print(' '.join(line.split()))