mirror of
https://github.com/pygos/init.git
synced 2024-05-18 19:56:14 +02:00
Compare commits
97 commits
Author | SHA1 | Date | |
---|---|---|---|
David Oberhollenzer | 5307b95b93 | ||
David Oberhollenzer | 70ea16b0b4 | ||
David Oberhollenzer | 5f28289731 | ||
David Oberhollenzer | 0975ed0fb7 | ||
David Oberhollenzer | 9b43890591 | ||
David Oberhollenzer | 87a524d931 | ||
David Oberhollenzer | 9f9807d4d3 | ||
David Oberhollenzer | 0d985a7430 | ||
David Oberhollenzer | 60efd9dc33 | ||
David Oberhollenzer | 1c72dd2c2f | ||
David Oberhollenzer | 2e89d32a75 | ||
David Oberhollenzer | c1cb8491f9 | ||
David Oberhollenzer | c8c0f10ce1 | ||
David Oberhollenzer | be06641904 | ||
David Oberhollenzer | c3d14cbfa8 | ||
David Oberhollenzer | 7cfe6e8458 | ||
David Oberhollenzer | f844c4e2c2 | ||
David Oberhollenzer | 028394b8a5 | ||
David Oberhollenzer | 6fa0393be4 | ||
David Oberhollenzer | ba12700080 | ||
David Oberhollenzer | 9e7478397a | ||
David Oberhollenzer | c16735414b | ||
David Oberhollenzer | d16d260181 | ||
David Oberhollenzer | affe9e4b88 | ||
David Oberhollenzer | 72c02308cd | ||
David Oberhollenzer | 9fece2eb88 | ||
David Oberhollenzer | ec04681c4d | ||
David Oberhollenzer | a6c059203b | ||
David Oberhollenzer | e21840cfce | ||
David Oberhollenzer | 390175c406 | ||
David Oberhollenzer | 4f1b393cee | ||
David Oberhollenzer | 1850f31d6d | ||
David Oberhollenzer | 065d3b678d | ||
David Oberhollenzer | 11053ebe6a | ||
David Oberhollenzer | a9602ad6e0 | ||
David Oberhollenzer | 3f40c4d3ed | ||
David Oberhollenzer | 40ad83dc6a | ||
David Oberhollenzer | 23b713c3b5 | ||
David Oberhollenzer | 08f72865b2 | ||
David Oberhollenzer | c78bbd2f73 | ||
David Oberhollenzer | c544fcc7a3 | ||
David Oberhollenzer | 5b106abaed | ||
4ee949c1ca | |||
d9a5736bdf | |||
c14c3c0173 | |||
d983a282ed | |||
bf878d08dd | |||
dc6358b0e1 | |||
7ee95668ca | |||
4bcfd98764 | |||
a8f3360e93 | |||
5923ad488a | |||
b864c727c8 | |||
c0e8c7e245 | |||
074fe20a47 | |||
34f542b7cc | |||
1ffc240b3f | |||
acd09007a1 | |||
d4ce928fc0 | |||
b3773d09ea | |||
24e98f1e5b | |||
ef51a8e06f | |||
53bc182e09 | |||
24c90b7700 | |||
7b647eefef | |||
209121cdeb | |||
e171f88865 | |||
a191a7cc18 | |||
66dde441bc | |||
33aa4cedff | |||
a4dddb7777 | |||
b9ba11b4db | |||
f38163772c | |||
5cd5f48f76 | |||
481744a2ba | |||
5b78d4d695 | |||
ce7c79a639 | |||
f67d32cba1 | |||
42fba964c9 | |||
3b8764b9d2 | |||
dc30dd7530 | |||
b0b6c68e38 | |||
bb16d1f1bf | |||
f39aba8aa3 | |||
34aff498ed | |||
b5c6e9d392 | |||
0ed964c8a5 | |||
066efaa33e | |||
2d54b32d24 | |||
ec6264bad5 | |||
25afc0b3d0 | |||
ec74e5bbee | |||
532f5e6819 | |||
73404a09d4 | |||
325f919847 | |||
61bc850984 | |||
f51dca0878 |
23
.gitignore
vendored
23
.gitignore
vendored
|
@ -20,26 +20,7 @@ reboot
|
|||
shutdown
|
||||
killall5
|
||||
runsvc
|
||||
syslog
|
||||
usyslogd
|
||||
|
||||
services/sigkill
|
||||
services/sigterm
|
||||
services/devfs
|
||||
services/procfs
|
||||
services/sysfs
|
||||
services/ifrename
|
||||
services/ifcfg
|
||||
services/ifdown
|
||||
services/modules
|
||||
services/hostapd
|
||||
services/swclock
|
||||
services/swclocksave
|
||||
services/nft
|
||||
|
||||
scripts/devfs.sh
|
||||
scripts/ifrename.sh
|
||||
scripts/ifcfg.sh
|
||||
scripts/modules_load.sh
|
||||
gcrond
|
||||
waitfile
|
||||
|
||||
etc/initd.env
|
||||
|
|
687
LICENSE
687
LICENSE
|
@ -1,674 +1,13 @@
|
|||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, 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
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If 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 convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU 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
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "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 PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM 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 PROGRAM (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 PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state 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 program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
Copyright (c) 2018 David Oberhollenzer <goliath@infraroot.at>
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
|
43
Makefile.am
43
Makefile.am
|
@ -6,48 +6,27 @@ AM_CFLAGS = $(WARN_CFLAGS)
|
|||
bin_PROGRAMS =
|
||||
sbin_PROGRAMS =
|
||||
noinst_LIBRARIES =
|
||||
nobase_sysconf_DATA = netcfg/ifrename
|
||||
nobase_sysconf_DATA =
|
||||
sysconf_DATA = etc/initd.env
|
||||
EXTRA_DIST = README.md LICENSE docs netcfg
|
||||
|
||||
helperdir = @SCRIPTDIR@
|
||||
helper_PROGRAMS =
|
||||
helper_SCRIPTS =
|
||||
|
||||
EXTRA_DIST = README.md LICENSE docs
|
||||
|
||||
dist_man1_MANS =
|
||||
dist_man8_MANS =
|
||||
|
||||
include lib/Makemodule.am
|
||||
include cmd/Makemodule.am
|
||||
include initd/Makemodule.am
|
||||
include scripts/Makemodule.am
|
||||
include services/Makemodule.am
|
||||
|
||||
if USYSLOGD
|
||||
include syslogd/Makemodule.am
|
||||
endif
|
||||
install-exec-hook:
|
||||
(cd $(DESTDIR)$(sbindir); $(LN_S) shutdown reboot)
|
||||
|
||||
install-data-local:
|
||||
$(MKDIR_P) $(DESTDIR)$(man8dir)
|
||||
(cd $(DESTDIR)$(man8dir); $(LN_S) shutdown.8 reboot.8)
|
||||
$(MKDIR_P) $(DESTDIR)$(SVCDIR)
|
||||
$(LN_S) $(TEMPLATEDIR)/loopback $(DESTDIR)$(SVCDIR)/loopback
|
||||
$(LN_S) $(TEMPLATEDIR)/hostname $(DESTDIR)$(SVCDIR)/hostname
|
||||
$(LN_S) $(TEMPLATEDIR)/sysctl $(DESTDIR)$(SVCDIR)/sysctl
|
||||
$(LN_S) $(TEMPLATEDIR)/sysinit $(DESTDIR)$(SVCDIR)/sysinit
|
||||
$(LN_S) $(TEMPLATEDIR)/procfs $(DESTDIR)$(SVCDIR)/procfs
|
||||
$(LN_S) $(TEMPLATEDIR)/sysfs $(DESTDIR)$(SVCDIR)/sysfs
|
||||
$(LN_S) $(TEMPLATEDIR)/devfs $(DESTDIR)$(SVCDIR)/devfs
|
||||
$(LN_S) $(TEMPLATEDIR)/tmpfs $(DESTDIR)$(SVCDIR)/tmpfs
|
||||
$(LN_S) $(TEMPLATEDIR)/vfs $(DESTDIR)$(SVCDIR)/vfs
|
||||
$(LN_S) $(TEMPLATEDIR)/shutdown $(DESTDIR)$(SVCDIR)/shutdown
|
||||
$(LN_S) $(TEMPLATEDIR)/reboot $(DESTDIR)$(SVCDIR)/reboot
|
||||
$(LN_S) $(TEMPLATEDIR)/ifdown $(DESTDIR)$(SVCDIR)/ifdown@shutdown
|
||||
$(LN_S) $(TEMPLATEDIR)/sync $(DESTDIR)$(SVCDIR)/sync@shutdown
|
||||
$(LN_S) $(TEMPLATEDIR)/sigkill $(DESTDIR)$(SVCDIR)/sigkill@shutdown
|
||||
$(LN_S) $(TEMPLATEDIR)/sigterm $(DESTDIR)$(SVCDIR)/sigterm@shutdown
|
||||
$(LN_S) $(TEMPLATEDIR)/sync $(DESTDIR)$(SVCDIR)/sync@reboot
|
||||
$(LN_S) $(TEMPLATEDIR)/sigkill $(DESTDIR)$(SVCDIR)/sigkill@reboot
|
||||
$(LN_S) $(TEMPLATEDIR)/sigterm $(DESTDIR)$(SVCDIR)/sigterm@reboot
|
||||
$(LN_S) $(TEMPLATEDIR)/ifdown $(DESTDIR)$(SVCDIR)/ifdown@reboot
|
||||
$(LN_S) $(TEMPLATEDIR)/ifcfg $(DESTDIR)$(SVCDIR)/ifcfg
|
||||
$(LN_S) $(TEMPLATEDIR)/modules $(DESTDIR)$(SVCDIR)/modules
|
||||
$(LN_S) $(TEMPLATEDIR)/network $(DESTDIR)$(SVCDIR)/network
|
||||
if USYSLOGD
|
||||
$(LN_S) $(TEMPLATEDIR)/usyslogd $(DESTDIR)$(SVCDIR)/usyslogd
|
||||
endif
|
||||
$(MKDIR_P) $(DESTDIR)$(TEMPLATEDIR)
|
||||
|
|
57
README.md
57
README.md
|
@ -1,43 +1,49 @@
|
|||
# About
|
||||
|
||||
This directory contains the source code for a tiny init devised for
|
||||
the Pygos system.
|
||||
|
||||
The main goal of this project is to create a simple framework for:
|
||||
|
||||
- system boot up and initialization
|
||||
- service supervision
|
||||
|
||||
With the additional aims of having something that:
|
||||
|
||||
- simply works
|
||||
- is easy to understand
|
||||
- is easy to configure and maintain
|
||||
This directory contains the source code for a tiny service supervision
|
||||
framework devised for the Pygos system, consisting of an init daemon and
|
||||
accompanying command line utilities.
|
||||
|
||||
|
||||
The init process is intended to run on top of Linux and makes use of some
|
||||
Linux specific features (e.g. signalfd), but if sufficient interest exists,
|
||||
it should still be possible to make it run on some BSDs or whatever else.
|
||||
The programs of this package are developed first and foremost for GNU/Linux
|
||||
systems, so there are some GNU and some Linux extensions used and some of the
|
||||
code may unintentionally rely on Linux specific behavior.
|
||||
|
||||
Nevertheless, if sufficient interest exists, it should be possible to make it
|
||||
run on BSDs or other Unix-like systems, but some effort may be required.
|
||||
|
||||
|
||||
The init system tries to mimic the concept of unit files from systemd as those
|
||||
were considered to be a good design choice.
|
||||
|
||||
Those parameterizeable service description files are stored in `/usr/share/init`
|
||||
by default. Services are enabled by creating a symlink in `/etc/init.d`. This
|
||||
can be done using the `service` command line tool.
|
||||
In a typical setup, the parameterizeable service description files are stored
|
||||
in `/usr/share/init` by default. Services are enabled by creating a symlink
|
||||
in `/etc/init.d`. This can be done more conveniently using the `service`
|
||||
command line tool.
|
||||
|
||||
See [docs/services.md](docs/services.md) for more information on service
|
||||
description files.
|
||||
|
||||
See [docs/bootup.md](docs/bootup.md) for more information on what the init
|
||||
daemon does during system boot.
|
||||
A default setup for the Pygos system, including helper scripts for setting up
|
||||
mount points and for network configuration, is provided in a seperate package.
|
||||
This package only contains the bare init framework without any default
|
||||
configuration.
|
||||
|
||||
|
||||
Right now, the system is in a "basically works" proof of concept stage and
|
||||
needs some more work to become usable.
|
||||
|
||||
There are plans for *maybe* *eventually* adding support for Linux name
|
||||
spaces, seccomp filters and cgroups as needed in the medium future.
|
||||
There are plans for *maybe* *eventually* adding more fancy features like
|
||||
support for Linux name spaces, seccomp filters and cgroups, but right now,
|
||||
features are added only when the need arises.
|
||||
|
||||
|
||||
See [docs/init.md](docs/init.md) for more information on the design,
|
||||
implementation and caveats of the init daemon.
|
||||
|
||||
See [docs/cmdline.md](docs/cmdline.md) for an explanation on the available
|
||||
command line tools.
|
||||
|
||||
See [docs/services.md](docs/services.md) for more information on service
|
||||
description files.
|
||||
|
||||
|
||||
## Why
|
||||
|
@ -87,4 +93,3 @@ that have been considered include:
|
|||
|
||||
Nice and simple. Probably the best fit if the rest of your user space is
|
||||
busybox as well.
|
||||
|
||||
|
|
|
@ -1,48 +1,34 @@
|
|||
shutdown_SOURCES = cmd/shutdown.c
|
||||
shutdown_CPPFLAGS = $(AM_CPPFLAGS) -DPROGNAME=shutdown
|
||||
shutdown_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
shutdown_CFLAGS = $(AM_CFLAGS)
|
||||
shutdown_LDFLAGS = $(AM_LDFLAGS)
|
||||
shutdown_LDADD = libinit.a
|
||||
|
||||
reboot_SOURCES = cmd/shutdown.c
|
||||
reboot_CPPFLAGS = $(AM_CPPFLAGS) -DPROGNAME=reboot
|
||||
reboot_CFLAGS = $(AM_CFLAGS)
|
||||
reboot_LDFLAGS = $(AM_LDFLAGS)
|
||||
reboot_LDADD = libinit.a
|
||||
|
||||
runsvc_SOURCES = cmd/runsvc/runsvc.c cmd/runsvc/env.c cmd/runsvc/runsvc.h
|
||||
runsvc_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
runsvc_CFLAGS = $(AM_CFLAGS)
|
||||
runsvc_LDFLAGS = $(AM_LDFLAGS)
|
||||
runsvc_LDADD = libinit.a libcfg.a
|
||||
|
||||
killall5_SOURCES = cmd/killall5.c
|
||||
killall5_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
killall5_CFLAGS = $(AM_CFLAGS)
|
||||
killall5_LDFLAGS = $(AM_LDFLAGS)
|
||||
|
||||
waitfile_SOURCES = cmd/waitfile.c
|
||||
waitfile_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
waitfile_CFLAGS = $(AM_CFLAGS)
|
||||
waitfile_LDFLAGS = $(AM_LDFLAGS)
|
||||
|
||||
SRVHEADERS = cmd/service/servicecmd.h
|
||||
|
||||
service_SOURCES = cmd/service/servicecmd.c cmd/service/help.c
|
||||
service_SOURCES += cmd/service/enable.c cmd/service/disable.c
|
||||
service_SOURCES += cmd/service/dumpscript.c cmd/service/list.c
|
||||
service_SOURCES += cmd/service/status.c cmd/service/loadsvc.c
|
||||
service_SOURCES += cmd/service/startstop.c
|
||||
service_SOURCES += $(SRVHEADERS)
|
||||
service_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
service_CFLAGS = $(AM_CFLAGS)
|
||||
service_LDFLAGS = $(AM_LDFLAGS)
|
||||
service_LDADD = libinit.a libcfg.a
|
||||
|
||||
if USYSLOGD
|
||||
syslog_SOURCES = cmd/syslog.c
|
||||
syslog_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
syslog_CFLAGS = $(AM_CFLAGS)
|
||||
syslog_LDFLAGS = $(AM_LDFLAGS)
|
||||
syslog_LDADD = libinit.a
|
||||
|
||||
bin_PROGRAMS += syslog
|
||||
endif
|
||||
dist_man8_MANS += cmd/shutdown.8 cmd/service/service.8
|
||||
|
||||
EXTRA_DIST += $(SRVHEADERS)
|
||||
|
||||
sbin_PROGRAMS += service reboot shutdown
|
||||
helper_PROGRAMS += killall5 runsvc
|
||||
sbin_PROGRAMS += service shutdown
|
||||
helper_PROGRAMS += killall5 waitfile
|
||||
|
|
|
@ -1,20 +1,4 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <signal.h>
|
||||
|
@ -25,9 +9,7 @@
|
|||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static NORETURN void usage_and_exit(void)
|
||||
static __attribute__((noreturn)) void usage_and_exit(void)
|
||||
{
|
||||
fputs("Usage: killall5 SIGNAL\n", stderr);
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
130
cmd/runsvc/env.c
130
cmd/runsvc/env.c
|
@ -1,130 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "runsvc.h"
|
||||
|
||||
struct entry {
|
||||
struct entry *next;
|
||||
char data[];
|
||||
};
|
||||
|
||||
extern char **environ;
|
||||
|
||||
static void free_list(struct entry *list)
|
||||
{
|
||||
struct entry *e;
|
||||
|
||||
while (list != NULL) {
|
||||
e = list;
|
||||
list = list->next;
|
||||
free(e);
|
||||
}
|
||||
}
|
||||
|
||||
static struct entry *parse_list(rdline_t *rd)
|
||||
{
|
||||
struct entry *e, *list = NULL;
|
||||
char *ptr;
|
||||
|
||||
while (rdline(rd) == 0) {
|
||||
ptr = rd->buffer;
|
||||
|
||||
while (*ptr != '\0' && *ptr != ' ' && *ptr != '=')
|
||||
++ptr;
|
||||
|
||||
if (*ptr == ' ')
|
||||
memmove(ptr, ptr + 1, strlen(ptr + 1) + 1);
|
||||
|
||||
if (*(ptr++) != '=') {
|
||||
fprintf(stderr, "%s: %zu: line is not of the shape "
|
||||
"'key = value', skipping\n",
|
||||
rd->filename, rd->lineno);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (*ptr == ' ')
|
||||
memmove(ptr, ptr + 1, strlen(ptr + 1) + 1);
|
||||
|
||||
if (unescape(ptr)) {
|
||||
fprintf(stderr, "%s: %zu: malformed string constant, "
|
||||
"skipping\n",
|
||||
rd->filename, rd->lineno);
|
||||
continue;
|
||||
}
|
||||
|
||||
e = calloc(1, sizeof(*e) + strlen(rd->buffer) + 1);
|
||||
if (e == NULL)
|
||||
goto fail_oom;
|
||||
|
||||
strcpy(e->data, rd->buffer);
|
||||
e->next = list;
|
||||
list = e;
|
||||
}
|
||||
|
||||
return list;
|
||||
fail_oom:
|
||||
fputs("out of memory\n", stderr);
|
||||
free_list(list);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct entry *list_from_file(void)
|
||||
{
|
||||
struct entry *list;
|
||||
rdline_t rd;
|
||||
int fd;
|
||||
|
||||
fd = open(ENVFILE, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
perror(ENVFILE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rdline_init(&rd, fd, ENVFILE, 0, NULL);
|
||||
list = parse_list(&rd);
|
||||
close(fd);
|
||||
return list;
|
||||
}
|
||||
|
||||
int initenv(void)
|
||||
{
|
||||
struct entry *list, *e;
|
||||
int i, count;
|
||||
char **envp;
|
||||
|
||||
list = list_from_file();
|
||||
if (list == NULL)
|
||||
return -1;
|
||||
|
||||
for (count = 0, e = list; e != NULL; e = e->next)
|
||||
++count;
|
||||
|
||||
envp = malloc((count + 1) * sizeof(char *));
|
||||
if (envp == NULL) {
|
||||
fputs("out of memory\n", stderr);
|
||||
free_list(list);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0, e = list; e != NULL; e = e->next)
|
||||
envp[i] = e->data;
|
||||
|
||||
envp[i] = NULL;
|
||||
|
||||
environ = envp;
|
||||
return 0;
|
||||
}
|
|
@ -1,141 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "runsvc.h"
|
||||
|
||||
static int setup_tty(service_t *svc)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (svc->ctty != NULL) {
|
||||
fd = open(svc->ctty, O_RDWR);
|
||||
if (fd < 0) {
|
||||
perror(svc->ctty);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (svc->flags & SVC_FLAG_TRUNCATE_OUT)
|
||||
ftruncate(fd, 0);
|
||||
|
||||
close(STDIN_FILENO);
|
||||
close(STDOUT_FILENO);
|
||||
close(STDERR_FILENO);
|
||||
|
||||
setsid();
|
||||
|
||||
dup2(fd, STDIN_FILENO);
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
dup2(fd, STDERR_FILENO);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
static NORETURN void argv_exec(exec_t *e)
|
||||
{
|
||||
char **argv = alloca(e->argc + 1), *ptr;
|
||||
int i;
|
||||
|
||||
for (ptr = e->args, i = 0; i < e->argc; ++i, ptr += strlen(ptr) + 1)
|
||||
argv[i] = ptr;
|
||||
|
||||
argv[i] = NULL;
|
||||
execvp(argv[0], argv);
|
||||
perror(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static int runlst_wait(exec_t *list)
|
||||
{
|
||||
pid_t ret, pid;
|
||||
int status;
|
||||
|
||||
for (; list != NULL; list = list->next) {
|
||||
pid = fork();
|
||||
|
||||
if (pid == 0)
|
||||
argv_exec(list);
|
||||
|
||||
if (pid == -1) {
|
||||
perror("fork");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
do {
|
||||
ret = waitpid(pid, &status, 0);
|
||||
} while (ret != pid);
|
||||
|
||||
if (!WIFEXITED(status))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
if (WEXITSTATUS(status) != EXIT_SUCCESS)
|
||||
return WEXITSTATUS(status);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int dirfd, ret = EXIT_FAILURE;
|
||||
service_t *svc = NULL;
|
||||
|
||||
if (argc != 3) {
|
||||
fputs("usage: runsvc <directory> <filename>\n", stderr);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (getppid() != 1) {
|
||||
fputs("must be run by init!\n", stderr);
|
||||
goto out;
|
||||
}
|
||||
|
||||
dirfd = open(argv[1], O_RDONLY | O_DIRECTORY);
|
||||
if (dirfd < 0) {
|
||||
perror(argv[1]);
|
||||
goto out;
|
||||
}
|
||||
|
||||
svc = rdsvc(dirfd, argv[2], RDSVC_NO_FNAME | RDSVC_NO_DEPS);
|
||||
close(dirfd);
|
||||
if (svc == NULL)
|
||||
goto out;
|
||||
|
||||
if (svc->exec == NULL) {
|
||||
ret = EXIT_SUCCESS;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (initenv())
|
||||
goto out;
|
||||
|
||||
if (setup_tty(svc))
|
||||
goto out;
|
||||
|
||||
if (svc->exec->next == NULL)
|
||||
argv_exec(svc->exec);
|
||||
|
||||
ret = runlst_wait(svc->exec);
|
||||
out:
|
||||
delsvc(svc);
|
||||
return ret;
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef RUNSVC_H
|
||||
#define RUNSVC_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "service.h"
|
||||
#include "libcfg.h"
|
||||
#include "util.h"
|
||||
|
||||
#define ENVFILE ETCPATH "/initd.env"
|
||||
|
||||
int initenv(void);
|
||||
|
||||
#endif /* RUNSVC_H */
|
|
@ -1,25 +1,10 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
@ -71,6 +56,8 @@ static int cmd_disable(int argc, char **argv)
|
|||
goto out;
|
||||
}
|
||||
|
||||
kill(1, SIGHUP);
|
||||
|
||||
ret = EXIT_SUCCESS;
|
||||
out:
|
||||
free(linkname);
|
||||
|
|
|
@ -1,20 +1,4 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
@ -22,37 +6,6 @@
|
|||
#include <errno.h>
|
||||
|
||||
#include "servicecmd.h"
|
||||
#include "service.h"
|
||||
|
||||
static service_t *try_load(const char *directory, const char *filename)
|
||||
{
|
||||
int dirfd, type;
|
||||
struct stat sb;
|
||||
service_t *svc;
|
||||
|
||||
dirfd = open(directory, O_RDONLY | O_DIRECTORY);
|
||||
|
||||
if (dirfd < 0) {
|
||||
perror(directory);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fstatat(dirfd, filename, &sb, AT_SYMLINK_NOFOLLOW)) {
|
||||
fprintf(stderr, "stat %s/%s: %s\n",
|
||||
directory, filename, strerror(errno));
|
||||
close(dirfd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
type = (sb.st_mode & S_IFMT);
|
||||
|
||||
if (type != S_IFREG && type != S_IFLNK)
|
||||
return NULL;
|
||||
|
||||
svc = rdsvc(dirfd, filename, 0);
|
||||
close(dirfd);
|
||||
return svc;
|
||||
}
|
||||
|
||||
enum {
|
||||
NEED_QUOTES = 0x01,
|
||||
|
@ -140,7 +93,7 @@ static int cmd_dumpscript(int argc, char **argv)
|
|||
strcat(filename, argv[i]);
|
||||
}
|
||||
|
||||
svc = try_load(SVCDIR, filename);
|
||||
svc = loadsvc(SVCDIR, filename);
|
||||
|
||||
if (svc == NULL) {
|
||||
fprintf(stderr, "Could not load service '%s'\n", filename);
|
||||
|
@ -160,7 +113,7 @@ static int cmd_dumpscript(int argc, char **argv)
|
|||
print_str(ptr);
|
||||
ptr += strlen(ptr) + 1;
|
||||
}
|
||||
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
|
|
|
@ -1,25 +1,10 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
@ -76,6 +61,8 @@ static int cmd_enable(int argc, char **argv)
|
|||
goto out;
|
||||
}
|
||||
|
||||
kill(1, SIGHUP);
|
||||
|
||||
ret = EXIT_SUCCESS;
|
||||
out:
|
||||
free(linkname);
|
||||
|
|
|
@ -1,20 +1,4 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include "servicecmd.h"
|
||||
|
||||
extern char *__progname;
|
||||
|
|
|
@ -1,20 +1,4 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include "servicecmd.h"
|
||||
#include "service.h"
|
||||
#include "config.h"
|
||||
|
@ -40,7 +24,7 @@ static int cmd_list(int argc, char **argv)
|
|||
if (check_arguments(argv[0], argc, 1, 2))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
if (svcscan(SVCDIR, &list, 0)) {
|
||||
if (svcscan(SVCDIR, &list)) {
|
||||
fprintf(stderr, "Error while reading services from %s\n",
|
||||
SVCDIR);
|
||||
ret = EXIT_FAILURE;
|
||||
|
|
23
cmd/service/loadsvc.c
Normal file
23
cmd/service/loadsvc.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include "servicecmd.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
|
||||
service_t *loadsvc(const char *directory, const char *filename)
|
||||
{
|
||||
service_t *svc;
|
||||
int dirfd;
|
||||
|
||||
dirfd = open(directory, O_RDONLY | O_DIRECTORY);
|
||||
|
||||
if (dirfd < 0) {
|
||||
perror(directory);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
svc = rdsvc(dirfd, filename);
|
||||
close(dirfd);
|
||||
return svc;
|
||||
}
|
63
cmd/service/service.8
Normal file
63
cmd/service/service.8
Normal file
|
@ -0,0 +1,63 @@
|
|||
.TH service 8 "August 2018" "Pygos Init"
|
||||
.SH NAME
|
||||
service \- configure or administrate system services
|
||||
.SH SYNOPSIS
|
||||
.B service
|
||||
<command>
|
||||
[options]
|
||||
.SH DESCRIPTION
|
||||
The service command can be used to configure, control and administrate services
|
||||
supervised by the init daemon.
|
||||
.SH COMMANDS
|
||||
.TP
|
||||
.BR help " " \fI[command]\fP
|
||||
If no arguments are provided, displays a short help text and a list of all
|
||||
available commands with a short description for each.
|
||||
|
||||
If a command name is specified, displays a more elaborate description and
|
||||
usage information for the specified command.
|
||||
.TP
|
||||
.BR list " " \fI[target]\fP
|
||||
Displays a list of currently enabled services. If an optional target is
|
||||
specified, lists services only for this target, otherwise, list services
|
||||
for all targets.
|
||||
.TP
|
||||
.BR enable " " \fI<service>\fP " " \fI[arguments]\fP
|
||||
Enable (but do not start) a system service by creating a symlink in the
|
||||
configuration directory, pointing to the service template file.
|
||||
|
||||
An optional argument can be supplied to parameterize the template.
|
||||
.TP
|
||||
.BR disable " " \fI<service>\fP " " \fI[arguments]\fP
|
||||
Disable (but do not stop) a system service by removing the corresponding
|
||||
symlink in the configuration directory.
|
||||
|
||||
If the service is parameterized, arguments have to be specified to disable
|
||||
the desired service instance.
|
||||
.TP
|
||||
.BR dumpscript " " \fI<service>\fP " " \fI[arguments]\fP
|
||||
Parse a service file from and produce a pseudo shell script containing the
|
||||
exact commands executed when starting the service.
|
||||
.TP
|
||||
.BR status " " \fI[--detail|-d]\fP " " \fI[services...]\fP
|
||||
Print a status report of all supervised services, i.e. if they are currently
|
||||
running, have exited or waiting to be scheduled. A specific list of services
|
||||
can be specified. Shell globbing patterns can be used.
|
||||
.TP
|
||||
.BR start " " \fIservices...\fP
|
||||
Start one or more currently not running services. Shell globbing patterns can be used.
|
||||
.TP
|
||||
.BR stop " " \fIservices...\fP
|
||||
Stop one or more currently running services. Shell globbing patterns can be used.
|
||||
.SH AVAILABILITY
|
||||
This program is part of the Pygos init system.
|
||||
.SH COPYRIGHT
|
||||
Copyright \(co 2018 David Oberhollenzer
|
||||
.br
|
||||
License: ISC <https://www.gnu.org/licenses/license-list.html#ISC>.
|
||||
.br
|
||||
This is free software: you are free to change and redistribute it.
|
||||
There is NO WARRANTY, to the extent permitted by law.
|
||||
.SH SEE ALSO
|
||||
.BR sync (2),
|
||||
.BR reboot (2)
|
|
@ -1,27 +1,10 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "servicecmd.h"
|
||||
#include "service.h"
|
||||
#include "config.h"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
command_t *commands;
|
||||
|
|
|
@ -1,29 +1,15 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#ifndef SERVICECMD_H
|
||||
#define SERVICECMD_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "service.h"
|
||||
#include "config.h"
|
||||
|
||||
/*
|
||||
Describes a command that can be launched by passing its name as
|
||||
|
@ -54,11 +40,13 @@ typedef struct command_t {
|
|||
/* Global list of available commands */
|
||||
extern command_t *commands;
|
||||
|
||||
service_t *loadsvc(const char *directory, const char *filename);
|
||||
|
||||
/*
|
||||
Implemented in servicecmd.c. Prints program usage message and
|
||||
terminates with the given exit status.
|
||||
*/
|
||||
void usage(int status) NORETURN;
|
||||
void usage(int status) __attribute__((noreturn));
|
||||
|
||||
/*
|
||||
Write a message to stderr that advises the user how to consult the
|
||||
|
@ -90,4 +78,3 @@ int check_arguments(const char *cmd, int argc, int minc, int maxc);
|
|||
}
|
||||
|
||||
#endif /* SERVICECMD_H */
|
||||
|
||||
|
|
104
cmd/service/startstop.c
Normal file
104
cmd/service/startstop.c
Normal file
|
@ -0,0 +1,104 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include "servicecmd.h"
|
||||
#include "initsock.h"
|
||||
#include "service.h"
|
||||
#include "config.h"
|
||||
|
||||
#include <fnmatch.h>
|
||||
#include <getopt.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int cmd_startstop(int argc, char **argv,
|
||||
E_SERVICE_STATE filter, E_INIT_REQUEST action)
|
||||
{
|
||||
int i, fd, ret = EXIT_FAILURE;
|
||||
init_status_t resp;
|
||||
char tmppath[256];
|
||||
bool found;
|
||||
|
||||
if (check_arguments(argv[0], argc, 2, 2))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
sprintf(tmppath, "/tmp/svcstatus.%d.sock", (int)getpid());
|
||||
fd = init_socket_open(tmppath);
|
||||
|
||||
if (fd < 0) {
|
||||
unlink(tmppath);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (init_socket_send_request(fd, EIR_STATUS, filter))
|
||||
goto out;
|
||||
|
||||
for (;;) {
|
||||
memset(&resp, 0, sizeof(resp));
|
||||
|
||||
if (init_socket_recv_status(fd, &resp)) {
|
||||
perror("reading from initd socket");
|
||||
free_init_status(&resp);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (resp.state == ESS_NONE) {
|
||||
free_init_status(&resp);
|
||||
break;
|
||||
}
|
||||
|
||||
found = false;
|
||||
|
||||
for (i = optind; i < argc; ++i) {
|
||||
if (fnmatch(argv[i], resp.service_name, 0) == 0) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
if (fnmatch(argv[i], resp.filename, 0) == 0) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
if (init_socket_send_request(fd, action, resp.id))
|
||||
goto out;
|
||||
}
|
||||
|
||||
free_init_status(&resp);
|
||||
}
|
||||
|
||||
ret = EXIT_SUCCESS;
|
||||
out:
|
||||
close(fd);
|
||||
unlink(tmppath);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int cmd_start(int argc, char **argv)
|
||||
{
|
||||
return cmd_startstop(argc, argv, ESS_NONE, EIR_START);
|
||||
}
|
||||
|
||||
static int cmd_stop(int argc, char **argv)
|
||||
{
|
||||
return cmd_startstop(argc, argv, ESS_NONE, EIR_STOP);
|
||||
}
|
||||
|
||||
static command_t start = {
|
||||
.cmd = "start",
|
||||
.usage = "services...",
|
||||
.s_desc = "start a currently not running service",
|
||||
.l_desc = "Start one or more service that are currently not running. "
|
||||
"Shell style globbing patterns can used for service names.",
|
||||
.run_cmd = cmd_start,
|
||||
};
|
||||
|
||||
static command_t stop = {
|
||||
.cmd = "stop",
|
||||
.usage = "services...",
|
||||
.s_desc = "stop a currently running service",
|
||||
.l_desc = "Stop one or more service that are currently running. "
|
||||
"Shell style globbing patterns can used for service names.",
|
||||
.run_cmd = cmd_stop,
|
||||
};
|
||||
|
||||
REGISTER_COMMAND(start)
|
||||
REGISTER_COMMAND(stop)
|
170
cmd/service/status.c
Normal file
170
cmd/service/status.c
Normal file
|
@ -0,0 +1,170 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include "servicecmd.h"
|
||||
#include "initsock.h"
|
||||
#include "service.h"
|
||||
#include "config.h"
|
||||
|
||||
#include <fnmatch.h>
|
||||
#include <getopt.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static const struct option long_opts[] = {
|
||||
{ "detail", no_argument, NULL, 'd' },
|
||||
{ NULL, 0, NULL, 0 },
|
||||
};
|
||||
|
||||
static const char *short_opts = "d";
|
||||
|
||||
static int cmd_status(int argc, char **argv)
|
||||
{
|
||||
bool is_tty, found, show_details = false;
|
||||
int i, fd, ret = EXIT_FAILURE;
|
||||
init_status_t resp;
|
||||
char tmppath[256];
|
||||
const char *state;
|
||||
service_t *svc;
|
||||
|
||||
for (;;) {
|
||||
i = getopt_long(argc, argv, short_opts, long_opts, NULL);
|
||||
if (i == -1)
|
||||
break;
|
||||
|
||||
switch (i) {
|
||||
case 'd':
|
||||
show_details = true;
|
||||
break;
|
||||
default:
|
||||
tell_read_help(argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
sprintf(tmppath, "/tmp/svcstatus.%d.sock", (int)getpid());
|
||||
fd = init_socket_open(tmppath);
|
||||
|
||||
if (fd < 0) {
|
||||
unlink(tmppath);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (init_socket_send_request(fd, EIR_STATUS, ESS_NONE))
|
||||
goto out;
|
||||
|
||||
is_tty = (isatty(STDOUT_FILENO) == 1);
|
||||
|
||||
for (;;) {
|
||||
memset(&resp, 0, sizeof(resp));
|
||||
|
||||
if (init_socket_recv_status(fd, &resp)) {
|
||||
perror("reading from initd socket");
|
||||
free_init_status(&resp);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (resp.state == ESS_NONE) {
|
||||
free_init_status(&resp);
|
||||
break;
|
||||
}
|
||||
|
||||
if (optind < argc) {
|
||||
found = false;
|
||||
|
||||
for (i = optind; i < argc; ++i) {
|
||||
if (fnmatch(argv[i],
|
||||
resp.service_name, 0) == 0) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
if (fnmatch(argv[i], resp.filename, 0) == 0) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
free_init_status(&resp);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
switch (resp.state) {
|
||||
case ESS_RUNNING:
|
||||
if (!is_tty) {
|
||||
state = "UP";
|
||||
break;
|
||||
}
|
||||
state = "\033[22;32m UP \033[0m";
|
||||
break;
|
||||
case ESS_ENQUEUED:
|
||||
state = "SCHED";
|
||||
break;
|
||||
case ESS_FAILED:
|
||||
if (!is_tty) {
|
||||
state = "FAIL";
|
||||
break;
|
||||
}
|
||||
state = "\033[22;31mFAIL\033[0m";
|
||||
break;
|
||||
case ESS_DONE:
|
||||
if (!is_tty) {
|
||||
state = "DONE";
|
||||
break;
|
||||
}
|
||||
|
||||
state = "\033[22;33mDONE\033[0m";
|
||||
break;
|
||||
default:
|
||||
if (!is_tty) {
|
||||
state = "UNKNOWN";
|
||||
break;
|
||||
}
|
||||
state = "\033[22;31mUNKNOWN\033[0m";
|
||||
break;
|
||||
}
|
||||
|
||||
if (show_details) {
|
||||
printf("Service: %s\n", resp.filename);
|
||||
printf("\tStatus: %s\n", state);
|
||||
printf("\tTemplate name: %s\n", resp.service_name);
|
||||
printf("\tExit status: %d\n", resp.exit_status);
|
||||
|
||||
svc = loadsvc(SVCDIR, resp.filename);
|
||||
|
||||
if (svc == NULL) {
|
||||
fputs("\tError loading service file\n", stdout);
|
||||
} else {
|
||||
printf("\tDescription: %s\n", svc->desc);
|
||||
printf("\tType: %s\n",
|
||||
svc_type_to_string(svc->type));
|
||||
printf("\tTarget: %s\n",
|
||||
svc_target_to_string(svc->target));
|
||||
delsvc(svc);
|
||||
}
|
||||
} else {
|
||||
printf("[%s] %s\n", state, resp.filename);
|
||||
}
|
||||
|
||||
free_init_status(&resp);
|
||||
}
|
||||
|
||||
ret = EXIT_SUCCESS;
|
||||
out:
|
||||
close(fd);
|
||||
unlink(tmppath);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static command_t status = {
|
||||
.cmd = "status",
|
||||
.usage = "[-d|--detail] [services...]",
|
||||
.s_desc = "report the status of the currently enabled services",
|
||||
.l_desc = "Gathers a list of all currently running services and the "
|
||||
"state that they are in (currently running, done, failed, "
|
||||
"wating to get scheduled). A list of services with wildcard "
|
||||
"globbing patterns can be specified. If ommitted, produces "
|
||||
"a general overview of all services. If the --detail "
|
||||
"is given, more details are shown about a service.",
|
||||
.run_cmd = cmd_status,
|
||||
};
|
||||
|
||||
REGISTER_COMMAND(status)
|
48
cmd/shutdown.8
Normal file
48
cmd/shutdown.8
Normal file
|
@ -0,0 +1,48 @@
|
|||
.TH shutdown 8 "August 2018" "Pygos Init"
|
||||
.SH NAME
|
||||
shutdown \- shutdown or reboot the system
|
||||
.SH SYNOPSIS
|
||||
.B shutdown
|
||||
[options]
|
||||
.SH DESCRIPTION
|
||||
The shutdown command can notify the init system to shut the system down or to
|
||||
reboot the system.
|
||||
.PP
|
||||
If no option is specified, the default action is to initiate a shutdown.
|
||||
.PP
|
||||
If invoked as reboot, for instance through a symlink or hard link, the default
|
||||
action is to reboot the system.
|
||||
.SH OPTIONS
|
||||
.TP
|
||||
.BR \-h , " \-\-help"
|
||||
Display a help text and exit.
|
||||
.TP
|
||||
.BR \-V , " \-\-version"
|
||||
Print version information and exit.
|
||||
.TP
|
||||
.BR \-p , " \-\-poweroff"
|
||||
Do a shutdown and turn the system power off if possible.
|
||||
.TP
|
||||
.BR \-r , " \-\-reboot"
|
||||
Do a system reboot.
|
||||
.TP
|
||||
.BR \-f , " \-\-force"
|
||||
Bypass the init system and directly call the kernel to do a hard
|
||||
shutdown or reboot.
|
||||
.TP
|
||||
.BR \-n , " \-\-no-sync"
|
||||
If \-\-force is specified, do not call
|
||||
.BR sync (2)
|
||||
before bringing the system down.
|
||||
.SH AVAILABILITY
|
||||
This program is part of the Pygos init system.
|
||||
.SH COPYRIGHT
|
||||
Copyright \(co 2018 David Oberhollenzer
|
||||
.br
|
||||
License: ISC <https://www.gnu.org/licenses/license-list.html#ISC>.
|
||||
.br
|
||||
This is free software: you are free to change and redistribute it.
|
||||
There is NO WARRANTY, to the extent permitted by law.
|
||||
.SH SEE ALSO
|
||||
.BR sync (2),
|
||||
.BR reboot (2)
|
109
cmd/shutdown.c
109
cmd/shutdown.c
|
@ -1,43 +1,20 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <getopt.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <sys/reboot.h>
|
||||
#include <linux/reboot.h>
|
||||
|
||||
#include "telinit.h"
|
||||
#include "util.h"
|
||||
|
||||
#define STRINIFY(x) #x
|
||||
#define STRINIFY_VALUE(x) STRINIFY(x)
|
||||
#define PROGRAM_NAME STRINIFY_VALUE(PROGNAME)
|
||||
|
||||
#define FL_FORCE 0x01
|
||||
#define FL_NOSYNC 0x02
|
||||
|
||||
static const struct option options[] = {
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "version", no_argument, NULL, 'V' },
|
||||
{ "poweroff", no_argument, NULL, 'p' },
|
||||
{ "reboot", no_argument, NULL, 'r' },
|
||||
{ "force", no_argument, NULL, 'f' },
|
||||
|
@ -45,12 +22,12 @@ static const struct option options[] = {
|
|||
{ NULL, 0, NULL, 0 },
|
||||
};
|
||||
|
||||
static const char *shortopt = "hVprfn";
|
||||
static const char *shortopt = "hprfn";
|
||||
|
||||
static const char *defact_str = "power-off";
|
||||
static int defact = TI_SHUTDOWN;
|
||||
static int defact = RB_POWER_OFF;
|
||||
|
||||
static NORETURN void usage(int status)
|
||||
static __attribute__((noreturn)) void usage(const char *progname, int status)
|
||||
{
|
||||
fprintf(status == EXIT_SUCCESS ? stdout : stderr,
|
||||
"%s [OPTIONS...]\n\n"
|
||||
|
@ -63,32 +40,21 @@ static NORETURN void usage(int status)
|
|||
" init system.\n"
|
||||
" -n, --no-sync Don't sync storage media before power-off or reboot.\n\n"
|
||||
"If no option is specified, the default action is %s.\n",
|
||||
PROGRAM_NAME, defact_str);
|
||||
progname, defact_str);
|
||||
exit(status);
|
||||
}
|
||||
|
||||
static NORETURN void version(void)
|
||||
{
|
||||
fputs(
|
||||
PROGRAM_NAME " (Pygos init) " PACKAGE_VERSION "\n"
|
||||
"Copyright (C) 2018 David Oberhollenzer\n"
|
||||
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
|
||||
"This is free software: you are free to change and redistribute it.\n"
|
||||
"There is NO WARRANTY, to the extent permitted by law.\n",
|
||||
stdout);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int c, fd, flags = 0;
|
||||
ti_msg_t msg;
|
||||
ssize_t ret;
|
||||
int c, ret, flags = 0;
|
||||
char *ptr;
|
||||
|
||||
if (!strcmp(PROGRAM_NAME, "reboot")) {
|
||||
ptr = strrchr(argv[0], '/');
|
||||
ptr = (ptr == NULL) ? argv[0] : (ptr + 1);
|
||||
|
||||
if (strcmp(ptr, "reboot") == 0) {
|
||||
defact_str = "reboot";
|
||||
defact = TI_REBOOT;
|
||||
defact = RB_AUTOBOOT;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
|
@ -104,53 +70,40 @@ int main(int argc, char **argv)
|
|||
flags |= FL_NOSYNC;
|
||||
break;
|
||||
case 'p':
|
||||
defact = TI_SHUTDOWN;
|
||||
defact = RB_POWER_OFF;
|
||||
break;
|
||||
case 'r':
|
||||
defact = TI_REBOOT;
|
||||
defact = RB_AUTOBOOT;
|
||||
break;
|
||||
case 'V':
|
||||
version();
|
||||
case 'h':
|
||||
usage(EXIT_SUCCESS);
|
||||
usage(ptr, EXIT_SUCCESS);
|
||||
default:
|
||||
exit(EXIT_FAILURE);
|
||||
usage(ptr, EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & FL_FORCE) {
|
||||
if (!(flags & FL_NOSYNC))
|
||||
sync();
|
||||
|
||||
switch (defact) {
|
||||
case TI_REBOOT:
|
||||
reboot(RB_AUTOBOOT);
|
||||
break;
|
||||
case TI_SHUTDOWN:
|
||||
reboot(RB_POWER_OFF);
|
||||
break;
|
||||
}
|
||||
|
||||
reboot(defact);
|
||||
perror("reboot system call");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
fd = opensock();
|
||||
if (fd < 0)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
msg.type = defact;
|
||||
retry:
|
||||
ret = write(fd, &msg, sizeof(msg));
|
||||
|
||||
if (ret < 0) {
|
||||
if (errno == EINTR)
|
||||
goto retry;
|
||||
perror("write on init socket");
|
||||
close(fd);
|
||||
return EXIT_FAILURE;
|
||||
switch (defact) {
|
||||
case RB_AUTOBOOT:
|
||||
ret = kill(1, SIGINT);
|
||||
break;
|
||||
case RB_POWER_OFF:
|
||||
ret = kill(1, SIGTERM);
|
||||
break;
|
||||
default:
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
if (ret) {
|
||||
perror("sending signal to init");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
236
cmd/syslog.c
236
cmd/syslog.c
|
@ -1,236 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <getopt.h>
|
||||
#include <syslog.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include "util.h"
|
||||
|
||||
static int facility = LOG_USER;
|
||||
static int level = LOG_INFO;
|
||||
static int flags = LOG_NDELAY | LOG_NOWAIT;
|
||||
static const char *ident = "(shell)";
|
||||
|
||||
static const enum_map_t facility_map[] = {
|
||||
{ "auth", LOG_AUTH },
|
||||
{ "cron", LOG_CRON },
|
||||
{ "daemon", LOG_DAEMON },
|
||||
{ "ftp", LOG_FTP },
|
||||
{ "local0", LOG_LOCAL0 },
|
||||
{ "local1", LOG_LOCAL1 },
|
||||
{ "local2", LOG_LOCAL2 },
|
||||
{ "local3", LOG_LOCAL3 },
|
||||
{ "local4", LOG_LOCAL4 },
|
||||
{ "local5", LOG_LOCAL5 },
|
||||
{ "local6", LOG_LOCAL6 },
|
||||
{ "local7", LOG_LOCAL7 },
|
||||
{ "lpr", LOG_LPR },
|
||||
{ "news", LOG_NEWS },
|
||||
{ "user", LOG_USER },
|
||||
{ "uucp", LOG_UUCP },
|
||||
{ NULL, 0 },
|
||||
};
|
||||
|
||||
static const enum_map_t level_map[] = {
|
||||
{ "emergency", LOG_EMERG },
|
||||
{ "alert", LOG_ALERT },
|
||||
{ "critical", LOG_CRIT },
|
||||
{ "error", LOG_ERR },
|
||||
{ "warning", LOG_WARNING },
|
||||
{ "notice", LOG_NOTICE },
|
||||
{ "info", LOG_INFO },
|
||||
{ "debug", LOG_DEBUG },
|
||||
{ NULL, 0 },
|
||||
};
|
||||
|
||||
static const struct option options[] = {
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "version", no_argument, NULL, 'V' },
|
||||
{ "console", required_argument, NULL, 'c' },
|
||||
{ "facility", required_argument, NULL, 'f' },
|
||||
{ "level", required_argument, NULL, 'l' },
|
||||
{ "ident", required_argument, NULL, 'i' },
|
||||
{ NULL, 0, NULL, 0 },
|
||||
};
|
||||
|
||||
static const char *shortopt = "hVcf:l:i:";
|
||||
|
||||
static const char *versiontext =
|
||||
"syslog (Pygos init) "PACKAGE_VERSION"\n"
|
||||
"Copyright (C) 2018 David Oberhollenzer\n"
|
||||
"License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.\n"
|
||||
"This is free software: you are free to change and redistribute it.\n"
|
||||
"There is NO WARRANTY, to the extent permitted by law.\n";
|
||||
|
||||
static const char *helptext =
|
||||
"Usage: syslog [OPTION]... [STRING]...\n\n"
|
||||
"Concatenate the given STRINGs and send a log message to the syslog daemon.\n"
|
||||
"\n"
|
||||
"The following OPTIONSs can be used:\n"
|
||||
" -f, --facility <facility> Logging facilty name or numeric identifier.\n"
|
||||
" -l, --level <level> Log level name or numeric identifier.\n"
|
||||
" -i, --ident <name> Program name for log syslog message.\n"
|
||||
" Default is %s.\n\n"
|
||||
" -c, --console Write to the console if opening the syslog\n"
|
||||
" socket fails.\n\n"
|
||||
" -h, --help Print this help text and exit\n"
|
||||
" -V, --version Print version information and exit\n\n";
|
||||
|
||||
static void print_map(const enum_map_t *map, int defaultval,
|
||||
const char *option)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
printf("The following values can be used for %s:\n", option);
|
||||
|
||||
for (i = 0; map[i].name != NULL; ++i) {
|
||||
if (map[i].value == defaultval) {
|
||||
printf(" %s (=%d), set as default\n",
|
||||
map[i].name, map[i].value);
|
||||
} else {
|
||||
printf(" %s (=%d)\n", map[i].name, map[i].value);
|
||||
}
|
||||
}
|
||||
|
||||
fputc('\n', stdout);
|
||||
}
|
||||
|
||||
static NORETURN void usage(int status)
|
||||
{
|
||||
if (status != EXIT_SUCCESS) {
|
||||
fputs("Try `syslog --help' for more information\n", stderr);
|
||||
} else {
|
||||
printf(helptext, ident);
|
||||
print_map(level_map, level, "--level");
|
||||
print_map(facility_map, facility, "--facility");
|
||||
}
|
||||
|
||||
exit(status);
|
||||
}
|
||||
|
||||
static NORETURN void version(void)
|
||||
{
|
||||
fputs(versiontext, stdout);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
static int readint(const char *str)
|
||||
{
|
||||
int x = 0;
|
||||
|
||||
if (!isdigit(*str))
|
||||
return -1;
|
||||
|
||||
while (isdigit(*str))
|
||||
x = x * 10 + (*(str++)) - '0';
|
||||
|
||||
return (*str == '\0') ? x : -1;
|
||||
}
|
||||
|
||||
static void process_options(int argc, char **argv)
|
||||
{
|
||||
const enum_map_t *e;
|
||||
int c;
|
||||
|
||||
for (;;) {
|
||||
c = getopt_long(argc, argv, shortopt, options, NULL);
|
||||
if (c == -1)
|
||||
break;
|
||||
|
||||
switch (c) {
|
||||
case 'f':
|
||||
facility = readint(optarg);
|
||||
if (facility >= 0)
|
||||
break;
|
||||
e = enum_by_name(facility_map, optarg);
|
||||
if (e == NULL) {
|
||||
fprintf(stderr, "Unknown facility name '%s'\n",
|
||||
optarg);
|
||||
usage(EXIT_FAILURE);
|
||||
}
|
||||
facility = e->value;
|
||||
break;
|
||||
case 'l':
|
||||
level = readint(optarg);
|
||||
if (level >= 0)
|
||||
break;
|
||||
e = enum_by_name(level_map, optarg);
|
||||
if (e == NULL) {
|
||||
fprintf(stderr, "Unknown log level '%s'\n",
|
||||
optarg);
|
||||
usage(EXIT_FAILURE);
|
||||
}
|
||||
level = e->value;
|
||||
break;
|
||||
case 'i':
|
||||
ident = optarg;
|
||||
break;
|
||||
case 'c':
|
||||
flags |= LOG_CONS;
|
||||
break;
|
||||
case 'h':
|
||||
usage(EXIT_SUCCESS);
|
||||
case 'V':
|
||||
version();
|
||||
default:
|
||||
usage(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
size_t len = 0;
|
||||
char *str;
|
||||
int i;
|
||||
|
||||
process_options(argc, argv);
|
||||
|
||||
if (optind >= argc) {
|
||||
fputs("Error: no log string provided.\n", stderr);
|
||||
usage(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (i = optind; i < argc; ++i)
|
||||
len += strlen(argv[i]);
|
||||
|
||||
len += argc - optind - 1;
|
||||
|
||||
str = calloc(1, len + 1);
|
||||
if (str == NULL) {
|
||||
fputs("syslog: out of memory\n", stderr);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
for (i = optind; i < argc; ++i) {
|
||||
if (i > optind)
|
||||
strcat(str, " ");
|
||||
strcat(str, argv[i]);
|
||||
}
|
||||
|
||||
openlog(ident, flags, facility);
|
||||
syslog(level, "%s", str);
|
||||
closelog();
|
||||
|
||||
free(str);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
82
cmd/waitfile.c
Normal file
82
cmd/waitfile.c
Normal file
|
@ -0,0 +1,82 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <poll.h>
|
||||
|
||||
static int strtoui(const char *str)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
if (!isdigit(*str))
|
||||
return -1;
|
||||
|
||||
while (isdigit(*str)) {
|
||||
if (i > (INT_MAX / 10))
|
||||
return -1;
|
||||
|
||||
i = i * 10 + (*(str++)) - '0';
|
||||
}
|
||||
|
||||
if (*str != '\0')
|
||||
return -1;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
static void sigproc(int signo)
|
||||
{
|
||||
if (signo == SIGALRM) {
|
||||
fputs("waitfile timeout\n", stderr);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, found, timeout, probetime;
|
||||
struct stat sb;
|
||||
|
||||
if (argc < 4)
|
||||
goto fail_usage;
|
||||
|
||||
timeout = strtoui(argv[1]);
|
||||
probetime = strtoui(argv[2]);
|
||||
if (timeout < 0 || probetime < 0)
|
||||
goto fail_timeout;
|
||||
|
||||
signal(SIGALRM, sigproc);
|
||||
alarm(timeout);
|
||||
|
||||
for (;;) {
|
||||
found = 1;
|
||||
|
||||
for (i = 3; i < argc; ++i) {
|
||||
if (stat(argv[i], &sb) != 0) {
|
||||
found = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
alarm(0);
|
||||
break;
|
||||
}
|
||||
|
||||
poll(NULL, 0, probetime);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
fail_timeout:
|
||||
fputs("Timeout values must be integers!\n", stderr);
|
||||
goto fail_usage;
|
||||
fail_usage:
|
||||
fputs("Usage: waitfile <timeout secs> <probe time ms> FILES...\n",
|
||||
stderr);
|
||||
return EXIT_FAILURE;
|
||||
}
|
32
configure.ac
32
configure.ac
|
@ -1,8 +1,8 @@
|
|||
AC_PREREQ([2.60])
|
||||
|
||||
m4_define([RELEASE], 0.5)
|
||||
m4_define([RELEASE], 0.10)
|
||||
|
||||
AC_INIT([init], [RELEASE], [david.oberhollenzer@tele2.at], init)
|
||||
AC_INIT([init], [RELEASE], [goliath@infraroot.at], init)
|
||||
AC_CONFIG_MACRO_DIR([m4])
|
||||
AM_INIT_AUTOMAKE([foreign subdir-objects dist-xz])
|
||||
AM_SILENT_RULES([yes])
|
||||
|
@ -36,17 +36,6 @@ UL_WARN_ADD([-pedantic])
|
|||
|
||||
AC_SUBST([WARN_CFLAGS])
|
||||
|
||||
|
||||
AC_ARG_WITH([usyslogd],
|
||||
[AS_HELP_STRING([--without-usyslogd], [Build without syslog daemon])],
|
||||
[case "${withval}" in
|
||||
yes) AM_CONDITIONAL([USYSLOGD], [true]) ;;
|
||||
no) AM_CONDITIONAL([USYSLOGD], [false]) ;;
|
||||
*) AC_MSG_ERROR([bad value ${withval} for --without-usyslogd]) ;;
|
||||
esac],
|
||||
[AM_CONDITIONAL([USYSLOGD], [true])])
|
||||
|
||||
|
||||
AC_CONFIG_HEADERS([lib/include/config.h])
|
||||
AC_DEFINE_DIR(SVCDIR, sysconfdir/init.d, [Startup service directory])
|
||||
AC_DEFINE_DIR(TEMPLATEDIR, datadir/init, [Service template directory])
|
||||
|
@ -60,23 +49,6 @@ AC_DEFINE_DIR(STATEFILESPATH, prefix/var/lib, [Path for persistent state files])
|
|||
|
||||
AC_DEFINE_DIR(PREFIXPATH, prefix, [Fully expaneded installation prefix])
|
||||
|
||||
AC_CONFIG_FILES([services/sigkill])
|
||||
AC_CONFIG_FILES([services/sigterm])
|
||||
AC_CONFIG_FILES([services/sysfs])
|
||||
AC_CONFIG_FILES([services/devfs])
|
||||
AC_CONFIG_FILES([services/procfs])
|
||||
AC_CONFIG_FILES([services/ifrename])
|
||||
AC_CONFIG_FILES([services/ifcfg])
|
||||
AC_CONFIG_FILES([services/ifdown])
|
||||
AC_CONFIG_FILES([services/modules])
|
||||
AC_CONFIG_FILES([services/hostapd])
|
||||
AC_CONFIG_FILES([services/swclock])
|
||||
AC_CONFIG_FILES([services/swclocksave])
|
||||
AC_CONFIG_FILES([services/nft])
|
||||
AC_CONFIG_FILES([scripts/devfs.sh])
|
||||
AC_CONFIG_FILES([scripts/ifrename.sh])
|
||||
AC_CONFIG_FILES([scripts/ifcfg.sh])
|
||||
AC_CONFIG_FILES([scripts/modules_load.sh])
|
||||
AC_CONFIG_FILES([etc/initd.env])
|
||||
|
||||
AC_OUTPUT([Makefile])
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
# System Bootup Process
|
||||
|
||||
## Initial Ram Disk to Rootfs transition
|
||||
|
||||
After mounting the root filesystem, either the kernel or the initial ram disk
|
||||
startup process is expected to exec the init program from the root filesystem.
|
||||
|
||||
At the current time, there is no support for re-scanning the service files
|
||||
*yet*, so when init is started, the final configuration in `/etc/init.d` has to
|
||||
be present. As a result, we currently cannot perform mounting of `/etc/` or
|
||||
packing init into the initial ram disk and doing the rootfs transition.
|
||||
|
||||
Also, as a result of this, changing the service configuration requires a system
|
||||
reboot to be effective.
|
||||
|
||||
This _will_ change in the future.
|
||||
|
||||
|
||||
## Processing Service Descriptions
|
||||
|
||||
The init process reads service description files from `/etc/init.d` which are
|
||||
usually symlinks to actual files in `/usr/share/init`.
|
||||
|
||||
The exact locations may be changed through configure flags when compiling init.
|
||||
|
||||
Service files specify a *target* which is basically like a SystemV runlevel and
|
||||
can be one of the following:
|
||||
|
||||
* boot
|
||||
* reboot
|
||||
* shutdown
|
||||
* ctrlaltdel
|
||||
|
||||
After parsing the configuration files, the init process starts running the
|
||||
services for the `boot` target in a topological order as determined by their
|
||||
*before* and *after* dependencies.
|
||||
|
||||
Services can be of one of the following *types*:
|
||||
|
||||
* wait
|
||||
* once
|
||||
* respawn
|
||||
|
||||
Services of type `wait` are started exactly once and the init process waits
|
||||
until they terminate before continuing with other services.
|
||||
|
||||
The type `once` also only runs services once, but immediately continues
|
||||
starting other services in the mean time without waiting.
|
||||
|
||||
Services of type `respawn` also don't stall the init process and are re-started
|
||||
whenever they terminate.
|
||||
|
||||
## Service Process Setup
|
||||
|
||||
If a service description contains only a single `exec` line, the init process
|
||||
forks and then execs the command directly in the child process.
|
||||
|
||||
If the service description contains a `tty` field, the specified device file
|
||||
is opened in the child process and standard I/O is redirected to it before
|
||||
calling exec. Also, a new session is created.
|
||||
|
||||
|
||||
If a service description contains multiple `exec` lines, the init process forks
|
||||
off to a single child process that does the same setup as above, and then runs
|
||||
the command lines sequentially by forking a second time for each one, followed
|
||||
by an exec in the grand child and a wait in the original child.
|
||||
|
||||
If a single command line returns something other than `EXIT_SUCCESS`,
|
||||
processing of multiple command lines is immediately stopped and the offending
|
||||
exit status is returned to init.
|
||||
|
||||
|
||||
The init process reads environment variables from `/etc/initd.env`.
|
||||
|
35
docs/cmdline.md
Normal file
35
docs/cmdline.md
Normal file
|
@ -0,0 +1,35 @@
|
|||
# Available Command Line Tools
|
||||
|
||||
## The service command
|
||||
|
||||
The `service` utility can be used for control and administration of services.
|
||||
|
||||
It is composed of several sub commands run by issuing
|
||||
`service <command> [arguments..]`.
|
||||
|
||||
Currently available service commands are:
|
||||
|
||||
* enable - enable a service with the given list of arguments.
|
||||
* disable - disable a service. If the service is parameterized, requires the
|
||||
same arguments used for enabling, to disable the specific instance of the
|
||||
service.
|
||||
* dumpscript - generate an equivalent shell script from the `exec` lines of
|
||||
a service after applying all parameter substitutions.
|
||||
* list - list all enabled service. A target can be specified to only list
|
||||
services for the specified target.
|
||||
* help - display a short help text and a list of available commands.
|
||||
* start - start one or more services listed on the command line.
|
||||
* stop - stop one or more services listed on the command line.
|
||||
* status - display status of all services or the ones specified
|
||||
on the command line.
|
||||
|
||||
|
||||
## shutdown and reboot
|
||||
|
||||
The programs `shudown` and `reboot` can be used to signal to the `init` program
|
||||
that it should transition to the `shutdown` or `reboot` target respectively.
|
||||
|
||||
The option `-f` or `--force` can be used to by pass the init system entirely
|
||||
and force a hard reset or power off by directly signalling the kernel.
|
||||
|
||||
Running any one of those programs requires superuser privileges.
|
71
docs/init.md
Normal file
71
docs/init.md
Normal file
|
@ -0,0 +1,71 @@
|
|||
# The Init Daemon
|
||||
|
||||
## Initial Ram Disk to Rootfs transition
|
||||
|
||||
After mounting the root filesystem, either the kernel or the initial ram disk
|
||||
startup process is expected to exec the init program from the root filesystem.
|
||||
|
||||
At the current time, there is no support for re-scanning the service files
|
||||
*yet*, so when init is started, the final configuration in `/etc/init.d` has to
|
||||
be present. As a result, we currently cannot perform mounting of `/etc/` or
|
||||
packing init into the initial ram disk and doing the rootfs transition.
|
||||
|
||||
Also, as a result of this, changing the service configuration requires a system
|
||||
reboot to be effective.
|
||||
|
||||
This _will_ change in the future.
|
||||
|
||||
|
||||
## Service Types and Targets
|
||||
|
||||
A service file has a *type*, specifying whether the service should be run once
|
||||
or restarted when it terminates and a *target*, specifying when the service
|
||||
should be run.
|
||||
|
||||
The *target* is similar to a runlevel in System V init. The init daemon
|
||||
currently knows about the following targets:
|
||||
|
||||
* boot
|
||||
* reboot
|
||||
* shutdown
|
||||
|
||||
When `init` is run, it starts all the services for the `boot` target. From the
|
||||
`boot` target it can transition to any other target and execute the services
|
||||
for the specified target.
|
||||
|
||||
The `reboot` and `shutdown` targets cannot transition to any other target and
|
||||
when invoked, cause initd to drop everything else it intended to do.
|
||||
|
||||
For the `reboot` and `shutdown` targets, respawn type processes are no longer
|
||||
restarted when they terminate and once all services have been executed, the
|
||||
`init` program performs a hard system reboot or power off.
|
||||
|
||||
The init program tries to capture the `CTRL+ALT+DELETE` key sequence (or its
|
||||
local equivalent) and transitions to the reboot target if pressed.
|
||||
|
||||
|
||||
## Service Configuration Rescan
|
||||
|
||||
TBD
|
||||
|
||||
|
||||
## Control Socket and Signals
|
||||
|
||||
The `init` program catches the following signals:
|
||||
|
||||
* `SIGCHLD`
|
||||
* `SIGINT`
|
||||
* `SIGTERM`
|
||||
|
||||
The `SIGCHLD` handler implements standard process reaping. If a terminated
|
||||
process belongs to one of the supervised services, the configured action is
|
||||
taken (e.g. restarting it).
|
||||
|
||||
When `SIGINT` is caugth, `init` transitions to the `reboot` target. Similarly,
|
||||
`SIGTERM` causes `init` to transition to the `shutdown` target.
|
||||
|
||||
|
||||
For more complex tasks, `init` creates a control socket that the command line
|
||||
tools included in this package can use. For the time being, the control socket
|
||||
can only tell the init daemon to transition to the `reboot` or `shutdown`
|
||||
target.
|
193
docs/services.md
193
docs/services.md
|
@ -1,84 +1,31 @@
|
|||
# Service Files
|
||||
|
||||
Services that can be started and managed by init are described by service
|
||||
description files stored in `/usr/share/init`.
|
||||
The init process reads service descriptions from `/etc/init.d` which usually
|
||||
contains symlinks to the actual service files which can be conveniently removed
|
||||
or added to disable or enable services.
|
||||
|
||||
The init process actually reads from `/etc/init.d` which contains symlinks to
|
||||
the actual service files.
|
||||
Default services provided by this package are installed in `/usr/share/init`,
|
||||
i.e. this is where the symlinks point to.
|
||||
|
||||
|
||||
Note that the actual locations may be different, depending on the configure
|
||||
flags used.
|
||||
|
||||
Enabling a service means adding a symlink, disabling means removing a symlink.
|
||||
|
||||
Service descriptions can be parameterized. The arguments are extracted from the
|
||||
name of the symlink. Currently only 1 parameter is supported. The argument
|
||||
value is separated from the service name by an '@' character in the symlink
|
||||
name.
|
||||
|
||||
The file name of the sysmlink, excluding any parameters, is accepted as the
|
||||
canonical name of a service and used when referring to it via command line
|
||||
utilities or when injecting dependencies from a service file.
|
||||
|
||||
|
||||
Below is an annotated example for a simple, service description for a
|
||||
generic, parameterized agetty service:
|
||||
## Syntax
|
||||
|
||||
#
|
||||
# The text that init should print out when the status of the
|
||||
# service changes.
|
||||
#
|
||||
# The '%0' is replaced with the first argument extracted from the
|
||||
# symlink name.
|
||||
#
|
||||
description "agetty on %0"
|
||||
|
||||
#
|
||||
# How to run the service. 'respawn' means restart the service when it
|
||||
# terminates, 'once' means run it only once and continue with other
|
||||
# services in the mean while, 'wait' means run it once, but block until
|
||||
# it exits.
|
||||
#
|
||||
type respawn
|
||||
|
||||
#
|
||||
# When to start the service. 'boot' means when booting the system. Other
|
||||
# options are 'reboot', 'shutdown' and 'ctrlaltdel'. The system always
|
||||
# starts into the 'boot' target and then later transitions to one of the
|
||||
# others.
|
||||
#
|
||||
target boot
|
||||
|
||||
#
|
||||
# A list of service names that must be started before this service can
|
||||
# be run, i.e. this services needs to be started after those.
|
||||
#
|
||||
# This can only refer to generic names, not specific instances. For
|
||||
# instance, you can say "after getty" to make sure a service comes up after
|
||||
# all gettys are started, but you cannot specify "after agetty@tty1".
|
||||
#
|
||||
# Similar to 'after', there is also a 'before' keyword for specifying
|
||||
# dependencies.
|
||||
#
|
||||
after sysinit
|
||||
|
||||
#
|
||||
# The 'tty' directive specifies a file to which all I/O of the process is
|
||||
# redirected. The specified device file is used as a controlling tty for
|
||||
# the process and a new session is created with the service process as
|
||||
# session leader.
|
||||
#
|
||||
# In this example, we derive the controlling tty from the service
|
||||
# description argument.
|
||||
#
|
||||
tty "/dev/%0"
|
||||
|
||||
#
|
||||
# The 'exec' directive specifies the command to execute in order to start
|
||||
# the service. See in the example below on how to run multiple commands.
|
||||
#
|
||||
# Again we use the argument to specify what terminal our getty
|
||||
# should run on.
|
||||
#
|
||||
exec agetty %0 linux
|
||||
|
||||
As can be seen in this simple example, each line in a service description is
|
||||
made up of a keyword, followed by one or more arguments and terminated by a
|
||||
line break.
|
||||
Each line in a service description is made up of a keyword, followed by one
|
||||
or more arguments and terminated by a line break.
|
||||
|
||||
Blank lines are ignored and shell-style comments can be used.
|
||||
|
||||
|
@ -92,6 +39,115 @@ done using a '%' sign, followed by the argument index. A '%' sign can be
|
|||
escaped by writing '%%'.
|
||||
|
||||
|
||||
## Targets and Types
|
||||
|
||||
Service files specify a *target* which is basically like a SystemV runlevel
|
||||
and can be one of the following:
|
||||
|
||||
* boot
|
||||
* reboot
|
||||
* shutdown
|
||||
|
||||
After parsing the configuration files, the init process starts running the
|
||||
services for the `boot` target in a topological order as determined by their
|
||||
dependencies.
|
||||
|
||||
Services can be of one of the following *types*:
|
||||
|
||||
* wait
|
||||
* once
|
||||
* respawn
|
||||
|
||||
Services of type `wait` are started exactly once and the init process waits
|
||||
until they terminate before continuing with other services.
|
||||
|
||||
The type `once` also only runs services once, but immediately continues
|
||||
starting other services in the mean time without waiting. The init process
|
||||
only waits for `once` types when transitioning to another target.
|
||||
|
||||
Services of type `respawn` also don't stall the init process and are re-started
|
||||
whenever they terminate.
|
||||
|
||||
The keyword `limit` can be used a after `respawn` to specify how often a service
|
||||
may be restarted before giving up.
|
||||
|
||||
|
||||
## Dependencies
|
||||
|
||||
A service description file can specify dependencies using the keywords `after`
|
||||
and `before`, each followed by a space separated list of service names.
|
||||
|
||||
The init program executes a service after all the services specified by the
|
||||
`after` keyword have been started (type once or respawn) or have been completed
|
||||
(type wait).
|
||||
|
||||
The `before` keyword injects dependencies in reverse, i.e. all services
|
||||
specified after the `before` keyword are only executed after the service in
|
||||
question has been started.
|
||||
|
||||
If a service specified by `after` or `before` does not exist, it is simply
|
||||
ignored. This can occur for instance if the specified service is not enabled
|
||||
at all in the current configuration.
|
||||
|
||||
|
||||
## Running Services
|
||||
|
||||
If a service contains an `exec` line, the init process attempts to run it
|
||||
using the `runsvc` helper program that sets up the environment, attempts to
|
||||
execute the specified command line and passes the exit status back to the init
|
||||
process.
|
||||
|
||||
If multiple exec lines are specified, `runsvc` executes them sequentially and
|
||||
stops if any one returns a non-zero exit status.
|
||||
|
||||
The environment variables visible to the service processes are read
|
||||
from `/etc/initd.env`.
|
||||
|
||||
If the service description contains a `tty` field, the specified device file
|
||||
is opened by runsvc and standard I/O is redirected to it and a new session
|
||||
is created. The keyword `truncate` can be used to make `runsvc` truncate the
|
||||
file to zero size first.
|
||||
|
||||
For convenience, multiple exec lines can be wrapped into braces, as can be
|
||||
seen in one of the examples below.
|
||||
|
||||
|
||||
## Example
|
||||
|
||||
Below is an annotated example for a simple, service description for a
|
||||
generic, parameterized getty service:
|
||||
|
||||
#
|
||||
# The text that init should print out when the status of the
|
||||
# service changes.
|
||||
#
|
||||
# The '%0' is replaced with the first argument extracted from the
|
||||
# symlink name.
|
||||
#
|
||||
description "agetty on %0"
|
||||
|
||||
# Restart the getty when it terminates.
|
||||
type respawn
|
||||
|
||||
# We want to spawn gettys when booting the system
|
||||
target boot
|
||||
|
||||
# Only run this service after the 'sysinit' service is done
|
||||
after sysinit
|
||||
|
||||
#
|
||||
# Redirect all I/O of the process to this file. The specified device file
|
||||
# is used as a controlling tty for the process and a new session is created
|
||||
# with the service process as session leader.
|
||||
#
|
||||
# In this example, we derive the controlling tty from the service
|
||||
# description argument.
|
||||
#
|
||||
tty "/dev/%0"
|
||||
|
||||
# In order to run the service, simply fire up the agetty program
|
||||
exec agetty %0 linux
|
||||
|
||||
If a service should sequentially run multiple commands, they can be grouped
|
||||
inside braces as can be seen in the following, abbreviated example:
|
||||
|
||||
|
@ -107,4 +163,3 @@ inside braces as can be seen in the following, abbreviated example:
|
|||
mkdir /var/tmp -m 0755
|
||||
mount --bind /cfg/preserve/var_lib /var/lib
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
init_SOURCES = initd/main.c initd/init.h initd/signal_linux.c initd/runsvc.c
|
||||
init_SOURCES += initd/status.c initd/svclist.c
|
||||
init_SOURCES = initd/main.c initd/init.h initd/runsvc.c
|
||||
init_SOURCES += initd/status.c initd/supervisor.c initd/initsock.c
|
||||
init_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
init_CFLAGS = $(AM_CFLAGS)
|
||||
init_LDFLAGS = $(AM_LDFLAGS)
|
||||
|
|
96
initd/init.h
96
initd/init.h
|
@ -1,33 +1,31 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#ifndef INIT_H
|
||||
#define INIT_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <endian.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <linux/reboot.h>
|
||||
#include <sys/signalfd.h>
|
||||
#include <sys/reboot.h>
|
||||
#include <stdbool.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "initsock.h"
|
||||
#include "service.h"
|
||||
#include "telinit.h"
|
||||
#include "util.h"
|
||||
#include "config.h"
|
||||
|
||||
#define RUNSVCBIN SCRIPTDIR "/runsvc"
|
||||
#define ENVFILE ETCPATH "/initd.env"
|
||||
#define PROCFDDIR "/proc/self/fd"
|
||||
|
||||
enum {
|
||||
STATUS_OK = 0,
|
||||
|
@ -36,6 +34,10 @@ enum {
|
|||
STATUS_STARTED,
|
||||
};
|
||||
|
||||
/********** main.c **********/
|
||||
|
||||
void target_completed(int target);
|
||||
|
||||
/********** runsvc.c **********/
|
||||
|
||||
/*
|
||||
|
@ -45,12 +47,6 @@ enum {
|
|||
*/
|
||||
pid_t runsvc(service_t *svc);
|
||||
|
||||
/*
|
||||
Start a service using runsvc, but wait until the child process
|
||||
terminats and return its exit status.
|
||||
*/
|
||||
int runsvc_wait(service_t *svc);
|
||||
|
||||
/********** status.c **********/
|
||||
|
||||
/*
|
||||
|
@ -64,48 +60,30 @@ int runsvc_wait(service_t *svc);
|
|||
*/
|
||||
void print_status(const char *msg, int type, bool update);
|
||||
|
||||
/********** svclist.c **********/
|
||||
/********** supervisor.c **********/
|
||||
|
||||
/*
|
||||
Returns true if the list of running services contains
|
||||
single shot processes.
|
||||
*/
|
||||
bool svclist_have_singleshot(void);
|
||||
void supervisor_handle_exited(pid_t pid, int status);
|
||||
|
||||
/* Add a service to the list of running services */
|
||||
void svclist_add(service_t *svc);
|
||||
void supervisor_set_target(int next);
|
||||
|
||||
/*
|
||||
Remove a service, identifierd by PID, from the list of
|
||||
running services.
|
||||
void supervisor_init(void);
|
||||
|
||||
Returns the service identified by the PID or NULL if there
|
||||
is no such service.
|
||||
*/
|
||||
service_t *svclist_remove(pid_t pid);
|
||||
bool supervisor_process_queues(void);
|
||||
|
||||
/********** signal_<platform>.c **********/
|
||||
void supervisor_reload_config(void);
|
||||
|
||||
/*
|
||||
Setup signal handling. Returns -1 on error, a file descriptor on
|
||||
success.
|
||||
void supervisor_answer_status_request(int fd, const void *dest_addr,
|
||||
size_t addrlen, E_SERVICE_STATE filter);
|
||||
|
||||
The returned file descriptor can be polled and becomes readable
|
||||
when a signal arrives. Reading from it returns a signalfd_siginfo
|
||||
structure.
|
||||
void supervisor_start(int id);
|
||||
|
||||
The returned file descriptor has the close on exec flag set.
|
||||
void supervisor_stop(int id);
|
||||
|
||||
The kernel is also told to send us SIGINT signals if a user presses
|
||||
the local equivalent of CTRL+ALT+DEL.
|
||||
*/
|
||||
int sigsetup(void);
|
||||
/********** initsock.c **********/
|
||||
|
||||
/*
|
||||
Undo everything that sigsetup() changed about signal handling and
|
||||
restore the default.
|
||||
*/
|
||||
void sigreset(void);
|
||||
int init_socket_create(void);
|
||||
|
||||
int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen,
|
||||
E_SERVICE_STATE state, service_t *svc);
|
||||
|
||||
#endif /* INIT_H */
|
||||
|
||||
|
|
88
initd/initsock.c
Normal file
88
initd/initsock.c
Normal file
|
@ -0,0 +1,88 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include "init.h"
|
||||
|
||||
static int send_retry(int fd, const void *dst, size_t addrlen,
|
||||
const void *buffer, size_t size)
|
||||
{
|
||||
ssize_t ret;
|
||||
retry:
|
||||
ret = sendto(fd, buffer, size, MSG_NOSIGNAL, dst, addrlen);
|
||||
if (ret < 0 && errno == EINTR)
|
||||
goto retry;
|
||||
|
||||
if (ret < 0 || (size_t)ret < size)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int send_string(int fd, const void *dst, size_t addrlen,
|
||||
const char *str)
|
||||
{
|
||||
size_t len = strlen(str);
|
||||
uint16_t raw;
|
||||
|
||||
if (len > 0xFFFF)
|
||||
return -1;
|
||||
|
||||
raw = htobe16(len);
|
||||
if (send_retry(fd, dst, addrlen, &raw, 2))
|
||||
return -1;
|
||||
|
||||
return len > 0 ? send_retry(fd, dst, addrlen, str, len) : 0;
|
||||
}
|
||||
|
||||
int init_socket_create(void)
|
||||
{
|
||||
struct sockaddr_un un;
|
||||
int fd;
|
||||
|
||||
fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
|
||||
if (fd < 0) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&un, 0, sizeof(un));
|
||||
un.sun_family = AF_UNIX;
|
||||
|
||||
strcpy(un.sun_path, INIT_SOCK_PATH);
|
||||
unlink(INIT_SOCK_PATH);
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&un, sizeof(un))) {
|
||||
perror("bind: " INIT_SOCK_PATH);
|
||||
close(fd);
|
||||
unlink(INIT_SOCK_PATH);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
int init_socket_send_status(int fd, const void *dest_addr, size_t addrlen,
|
||||
E_SERVICE_STATE state, service_t *svc)
|
||||
{
|
||||
init_response_status_t info;
|
||||
|
||||
memset(&info, 0, sizeof(info));
|
||||
|
||||
if (svc == NULL || state == ESS_NONE) {
|
||||
info.state = ESS_NONE;
|
||||
info.id = -1;
|
||||
} else {
|
||||
info.state = state;
|
||||
info.exit_status = svc->status & 0xFF;
|
||||
info.id = htobe32(svc->id);
|
||||
}
|
||||
|
||||
if (send_retry(fd, dest_addr, addrlen, &info, sizeof(info)))
|
||||
return -1;
|
||||
|
||||
if (svc != NULL && state != ESS_NONE) {
|
||||
if (send_string(fd, dest_addr, addrlen, svc->fname))
|
||||
return -1;
|
||||
if (send_string(fd, dest_addr, addrlen, svc->name))
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
276
initd/main.c
276
initd/main.c
|
@ -1,74 +1,12 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include "init.h"
|
||||
|
||||
static service_list_t cfg;
|
||||
static int sigfd = -1;
|
||||
static int sockfd = -1;
|
||||
|
||||
static int target = TGT_BOOT; /* runlevel we are targetting */
|
||||
static int runlevel = -1; /* runlevel we are currently on */
|
||||
|
||||
static void handle_exited(service_t *svc)
|
||||
{
|
||||
switch (svc->type) {
|
||||
case SVC_RESPAWN:
|
||||
if (target == TGT_REBOOT || target == TGT_SHUTDOWN)
|
||||
break;
|
||||
|
||||
if (svc->rspwn_limit > 0) {
|
||||
svc->rspwn_limit -= 1;
|
||||
|
||||
if (svc->rspwn_limit == 0) {
|
||||
print_status(svc->desc, STATUS_FAIL, false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
svc->pid = runsvc(svc);
|
||||
if (svc->pid == -1) {
|
||||
print_status(svc->desc, STATUS_FAIL, false);
|
||||
break;
|
||||
}
|
||||
|
||||
svclist_add(svc);
|
||||
return;
|
||||
case SVC_ONCE:
|
||||
print_status(svc->desc,
|
||||
svc->status == EXIT_SUCCESS ?
|
||||
STATUS_OK : STATUS_FAIL, false);
|
||||
break;
|
||||
}
|
||||
delsvc(svc);
|
||||
}
|
||||
|
||||
static void handle_signal(int sigfd)
|
||||
static void handle_signal(void)
|
||||
{
|
||||
struct signalfd_siginfo info;
|
||||
service_t *svc;
|
||||
int status;
|
||||
pid_t pid;
|
||||
|
||||
|
@ -83,103 +21,107 @@ static void handle_signal(int sigfd)
|
|||
status = WIFEXITED(status) ? WEXITSTATUS(status) :
|
||||
EXIT_FAILURE;
|
||||
|
||||
svc = svclist_remove(pid);
|
||||
|
||||
if (svc != NULL)
|
||||
handle_exited(svc);
|
||||
supervisor_handle_exited(pid, status);
|
||||
}
|
||||
break;
|
||||
case SIGTERM:
|
||||
supervisor_set_target(TGT_SHUTDOWN);
|
||||
break;
|
||||
case SIGINT:
|
||||
/* TODO: ctrl-alt-del */
|
||||
supervisor_set_target(TGT_REBOOT);
|
||||
break;
|
||||
case SIGHUP:
|
||||
supervisor_reload_config();
|
||||
break;
|
||||
case SIGUSR1:
|
||||
if (sockfd >= 0) {
|
||||
close(sockfd);
|
||||
unlink(INIT_SOCK_PATH);
|
||||
sockfd = -1;
|
||||
}
|
||||
sockfd = init_socket_create();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void start_runlevel(int level)
|
||||
{
|
||||
service_t *svc;
|
||||
int status;
|
||||
|
||||
while (cfg.targets[level] != NULL) {
|
||||
svc = cfg.targets[level];
|
||||
cfg.targets[level] = svc->next;
|
||||
|
||||
if (svc->type == SVC_WAIT) {
|
||||
print_status(svc->desc, STATUS_WAIT, false);
|
||||
|
||||
status = runsvc_wait(svc);
|
||||
|
||||
print_status(svc->desc,
|
||||
status == EXIT_SUCCESS ?
|
||||
STATUS_OK : STATUS_FAIL,
|
||||
true);
|
||||
delsvc(svc);
|
||||
} else {
|
||||
svc->pid = runsvc(svc);
|
||||
if (svc->pid == -1) {
|
||||
print_status(svc->desc, STATUS_FAIL, false);
|
||||
delsvc(svc);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (svc->type == SVC_RESPAWN)
|
||||
print_status(svc->desc, STATUS_STARTED, false);
|
||||
|
||||
svclist_add(svc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int read_msg(int fd, ti_msg_t *msg)
|
||||
static void handle_request(void)
|
||||
{
|
||||
struct sockaddr_un addr;
|
||||
init_request_t rq;
|
||||
socklen_t addrlen;
|
||||
ssize_t ret;
|
||||
retry:
|
||||
ret = read(fd, msg, sizeof(*msg));
|
||||
memset(&rq, 0, sizeof(rq));
|
||||
addrlen = sizeof(addr);
|
||||
ret = recvfrom(sockfd, &rq, sizeof(rq), MSG_DONTWAIT | MSG_TRUNC,
|
||||
(struct sockaddr *)&addr, &addrlen);
|
||||
|
||||
if (ret < 0) {
|
||||
if (errno == EINTR)
|
||||
goto retry;
|
||||
perror("read on telinit socket");
|
||||
return -1;
|
||||
if (ret < 0 && errno == EINTR)
|
||||
goto retry;
|
||||
|
||||
if ((size_t)ret < sizeof(rq))
|
||||
return;
|
||||
|
||||
switch (rq.rq) {
|
||||
case EIR_STATUS:
|
||||
supervisor_answer_status_request(sockfd, &addr, addrlen,
|
||||
rq.arg.status.filter);
|
||||
break;
|
||||
case EIR_START:
|
||||
rq.arg.startstop.id = be32toh(rq.arg.startstop.id);
|
||||
supervisor_start(rq.arg.startstop.id);
|
||||
break;
|
||||
case EIR_STOP:
|
||||
rq.arg.startstop.id = be32toh(rq.arg.startstop.id);
|
||||
supervisor_stop(rq.arg.startstop.id);
|
||||
break;
|
||||
}
|
||||
|
||||
if ((size_t)ret < sizeof(*msg)) {
|
||||
fputs("short read on telinit socket", stderr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void handle_tellinit(int ti_sock)
|
||||
void target_completed(int target)
|
||||
{
|
||||
ti_msg_t msg;
|
||||
int fd;
|
||||
|
||||
fd = accept(ti_sock, NULL, NULL);
|
||||
if (fd == -1)
|
||||
return;
|
||||
|
||||
if (read_msg(fd, &msg)) {
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (msg.type) {
|
||||
case TI_SHUTDOWN:
|
||||
target = TGT_SHUTDOWN;
|
||||
switch (target) {
|
||||
case TGT_BOOT:
|
||||
if (sockfd < 0)
|
||||
sockfd = init_socket_create();
|
||||
break;
|
||||
case TI_REBOOT:
|
||||
target = TGT_REBOOT;
|
||||
case TGT_SHUTDOWN:
|
||||
for (;;)
|
||||
reboot(RB_POWER_OFF);
|
||||
break;
|
||||
case TGT_REBOOT:
|
||||
for (;;)
|
||||
reboot(RB_AUTOBOOT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
static int sigsetup(void)
|
||||
{
|
||||
sigset_t mask;
|
||||
int sfd;
|
||||
|
||||
sigfillset(&mask);
|
||||
if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1) {
|
||||
perror("sigprocmask");
|
||||
return -1;
|
||||
}
|
||||
|
||||
sfd = signalfd(-1, &mask, SFD_CLOEXEC);
|
||||
if (sfd == -1) {
|
||||
perror("signalfd");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (reboot(LINUX_REBOOT_CMD_CAD_OFF))
|
||||
perror("cannot disable CTRL+ALT+DEL");
|
||||
|
||||
return sfd;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int ti_sock = -1, sfd, ret, count;
|
||||
int i, ret, count;
|
||||
struct pollfd pfd[2];
|
||||
|
||||
if (getpid() != 1) {
|
||||
|
@ -187,44 +129,40 @@ int main(void)
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (svcscan(SVCDIR, &cfg, RDSVC_NO_EXEC | RDSVC_NO_CTTY)) {
|
||||
fputs("Error reading service list from " SVCDIR "\n"
|
||||
"Trying to continue anyway\n", stderr);
|
||||
}
|
||||
supervisor_init();
|
||||
|
||||
sfd = sigsetup();
|
||||
if (sfd < 0)
|
||||
sigfd = sigsetup();
|
||||
if (sigfd < 0)
|
||||
return -1;
|
||||
|
||||
memset(pfd, 0, sizeof(pfd));
|
||||
pfd[0].fd = sfd;
|
||||
pfd[0].events = POLLIN;
|
||||
count = 1;
|
||||
|
||||
for (;;) {
|
||||
if (!svclist_have_singleshot() && target != runlevel) {
|
||||
start_runlevel(target);
|
||||
runlevel = target;
|
||||
while (supervisor_process_queues())
|
||||
;
|
||||
|
||||
if (target == TGT_BOOT && ti_sock == -1) {
|
||||
ti_sock = mksock(INITSOCK, SOCK_FLAG_ROOT_ONLY);
|
||||
if (ti_sock != -1) {
|
||||
pfd[1].fd = ti_sock;
|
||||
pfd[1].events = POLLIN;
|
||||
count = 2;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
memset(pfd, 0, sizeof(pfd));
|
||||
count = 0;
|
||||
|
||||
pfd[count].fd = sigfd;
|
||||
pfd[count].events = POLLIN;
|
||||
++count;
|
||||
|
||||
if (sockfd >= 0) {
|
||||
pfd[count].fd = sockfd;
|
||||
pfd[count].events = POLLIN;
|
||||
++count;
|
||||
}
|
||||
|
||||
ret = poll(pfd, count, -1);
|
||||
if (ret <= 0)
|
||||
continue;
|
||||
|
||||
if (ret > 0) {
|
||||
if (pfd[0].revents & POLLIN)
|
||||
handle_signal(sfd);
|
||||
|
||||
if (ti_sock != -1 && pfd[1].revents & POLLIN)
|
||||
handle_tellinit(ti_sock);
|
||||
for (i = 0; i < count; ++i) {
|
||||
if (pfd[i].revents & POLLIN) {
|
||||
if (pfd[i].fd == sigfd)
|
||||
handle_signal();
|
||||
if (pfd[i].fd == sockfd)
|
||||
handle_request();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
200
initd/runsvc.c
200
initd/runsvc.c
|
@ -1,66 +1,178 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <dirent.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "init.h"
|
||||
|
||||
static int setup_env(void)
|
||||
{
|
||||
int status = -1;
|
||||
ssize_t ret;
|
||||
FILE *fp;
|
||||
|
||||
clearenv();
|
||||
|
||||
fp = fopen(ENVFILE, "r");
|
||||
if (fp == NULL) {
|
||||
perror(ENVFILE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
char *line = NULL;
|
||||
size_t n = 0;
|
||||
|
||||
errno = 0;
|
||||
ret = getline(&line, &n, fp);
|
||||
|
||||
if (ret < 0) {
|
||||
if (errno == 0) {
|
||||
status = 0;
|
||||
} else {
|
||||
perror(ENVFILE);
|
||||
}
|
||||
} else if (ret > 0 && putenv(line) != 0) {
|
||||
perror("putenv");
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
free(line);
|
||||
} while (ret >= 0);
|
||||
|
||||
fclose(fp);
|
||||
return status;
|
||||
}
|
||||
|
||||
static int close_all_files(void)
|
||||
{
|
||||
struct dirent *ent;
|
||||
DIR *dir;
|
||||
int fd;
|
||||
|
||||
dir = opendir(PROCFDDIR);
|
||||
if (dir == NULL) {
|
||||
perror(PROCFDDIR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while ((ent = readdir(dir)) != NULL) {
|
||||
if (!isdigit(ent->d_name[0]))
|
||||
continue;
|
||||
|
||||
fd = atoi(ent->d_name);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int setup_tty(const char *tty, bool truncate)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (tty == NULL)
|
||||
return 0;
|
||||
|
||||
fd = open(tty, O_RDWR);
|
||||
if (fd < 0) {
|
||||
perror(tty);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (truncate)
|
||||
ftruncate(fd, 0);
|
||||
|
||||
setsid();
|
||||
|
||||
dup2(fd, STDIN_FILENO);
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
dup2(fd, STDERR_FILENO);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static __attribute__((noreturn)) void argv_exec(exec_t *e)
|
||||
{
|
||||
char **argv = alloca(sizeof(char *) * (e->argc + 1)), *ptr;
|
||||
int i;
|
||||
|
||||
for (ptr = e->args, i = 0; i < e->argc; ++i, ptr += strlen(ptr) + 1)
|
||||
argv[i] = ptr;
|
||||
|
||||
argv[i] = NULL;
|
||||
execvp(argv[0], argv);
|
||||
perror(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static int run_sequentially(exec_t *list)
|
||||
{
|
||||
pid_t ret, pid;
|
||||
int status;
|
||||
|
||||
for (; list != NULL; list = list->next) {
|
||||
if (list->next == NULL)
|
||||
argv_exec(list);
|
||||
|
||||
pid = fork();
|
||||
|
||||
if (pid == 0)
|
||||
argv_exec(list);
|
||||
|
||||
if (pid == -1) {
|
||||
perror("fork");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
do {
|
||||
ret = waitpid(pid, &status, 0);
|
||||
} while (ret != pid);
|
||||
|
||||
if (!WIFEXITED(status))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
if (WEXITSTATUS(status) != EXIT_SUCCESS)
|
||||
return WEXITSTATUS(status);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
pid_t runsvc(service_t *svc)
|
||||
{
|
||||
char *argv[4], *envp[1];
|
||||
sigset_t mask;
|
||||
pid_t pid;
|
||||
|
||||
argv[0] = (char *)RUNSVCBIN;
|
||||
argv[1] = (char *)SVCDIR;
|
||||
argv[2] = svc->fname;
|
||||
argv[3] = NULL;
|
||||
|
||||
envp[0] = NULL;
|
||||
|
||||
pid = fork();
|
||||
|
||||
if (pid == -1)
|
||||
perror("fork");
|
||||
|
||||
if (pid == 0) {
|
||||
sigreset();
|
||||
execve(argv[0], argv, envp);
|
||||
perror(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
sigemptyset(&mask);
|
||||
sigprocmask(SIG_SETMASK, &mask, NULL);
|
||||
|
||||
if (setup_env())
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
if (close_all_files())
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
if (setup_tty(svc->ctty,
|
||||
(svc->flags & SVC_FLAG_TRUNCATE_OUT) != 0)) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
exit(run_sequentially(svc->exec));
|
||||
}
|
||||
|
||||
return pid;
|
||||
}
|
||||
|
||||
int runsvc_wait(service_t *svc)
|
||||
{
|
||||
pid_t ret, pid = runsvc(svc);
|
||||
int status;
|
||||
|
||||
if (pid == -1)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
do {
|
||||
ret = waitpid(pid, &status, 0);
|
||||
} while (ret != pid);
|
||||
|
||||
return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
|
||||
}
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
|
||||
#include "init.h"
|
||||
|
||||
int sigsetup(void)
|
||||
{
|
||||
sigset_t mask;
|
||||
int sfd;
|
||||
|
||||
sigfillset(&mask);
|
||||
if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1) {
|
||||
perror("sigprocmask");
|
||||
return -1;
|
||||
}
|
||||
|
||||
sfd = signalfd(-1, &mask, SFD_CLOEXEC);
|
||||
if (sfd == -1) {
|
||||
perror("signalfd");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (reboot(LINUX_REBOOT_CMD_CAD_OFF))
|
||||
perror("cannot disable CTRL+ALT+DEL");
|
||||
|
||||
return sfd;
|
||||
}
|
||||
|
||||
void sigreset(void)
|
||||
{
|
||||
sigset_t mask;
|
||||
|
||||
sigemptyset(&mask);
|
||||
sigprocmask(SIG_SETMASK, &mask, NULL);
|
||||
}
|
|
@ -1,20 +1,4 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <stdio.h>
|
||||
|
||||
#include "init.h"
|
||||
|
|
361
initd/supervisor.c
Normal file
361
initd/supervisor.c
Normal file
|
@ -0,0 +1,361 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include "init.h"
|
||||
|
||||
static service_list_t cfg;
|
||||
|
||||
static int service_id = 1;
|
||||
static int target = -1;
|
||||
static service_t *running = NULL;
|
||||
static service_t *terminated = NULL;
|
||||
static service_t *queue = NULL;
|
||||
static service_t *completed = NULL;
|
||||
static service_t *failed = NULL;
|
||||
static int singleshot = 0;
|
||||
static bool waiting = false;
|
||||
|
||||
static bool find_service(service_t *list, service_t *svc)
|
||||
{
|
||||
while (list != NULL) {
|
||||
if (strcmp(list->fname, svc->fname) == 0)
|
||||
return true;
|
||||
|
||||
list = list->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void remove_not_in_list(service_t **current, service_t *list, int tgt)
|
||||
{
|
||||
service_t *it = *current, *prev = NULL;
|
||||
|
||||
while (it != NULL) {
|
||||
if (it->target == tgt && !find_service(list, it)) {
|
||||
if (prev == NULL) {
|
||||
delsvc(it);
|
||||
*current = (*current)->next;
|
||||
it = *current;
|
||||
} else {
|
||||
prev->next = it->next;
|
||||
delsvc(it);
|
||||
it = prev->next;
|
||||
}
|
||||
} else {
|
||||
prev = it;
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool have_service(service_t *svc)
|
||||
{
|
||||
return find_service(running, svc) || find_service(terminated, svc) ||
|
||||
find_service(queue, svc) || find_service(completed, svc) ||
|
||||
find_service(failed, svc);
|
||||
}
|
||||
|
||||
static int start_service(service_t *svc)
|
||||
{
|
||||
if (svc->id < 1)
|
||||
svc->id = service_id++;
|
||||
|
||||
svc->pid = runsvc(svc);
|
||||
if (svc->pid == -1) {
|
||||
print_status(svc->desc, STATUS_FAIL, false);
|
||||
svc->next = completed;
|
||||
completed = svc;
|
||||
return -1;
|
||||
}
|
||||
|
||||
svc->next = running;
|
||||
running = svc;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void handle_terminated_service(service_t *svc)
|
||||
{
|
||||
switch (svc->type) {
|
||||
case SVC_RESPAWN:
|
||||
if (target == TGT_REBOOT || target == TGT_SHUTDOWN)
|
||||
break;
|
||||
|
||||
if (svc->flags & SVC_FLAG_ADMIN_STOPPED)
|
||||
break;
|
||||
|
||||
if (svc->rspwn_limit > 0) {
|
||||
svc->rspwn_count += 1;
|
||||
|
||||
if (svc->rspwn_count >= svc->rspwn_limit) {
|
||||
print_status(svc->desc, STATUS_FAIL, false);
|
||||
goto out_failure;
|
||||
}
|
||||
}
|
||||
|
||||
start_service(svc);
|
||||
return;
|
||||
case SVC_WAIT:
|
||||
waiting = false;
|
||||
print_status(svc->desc,
|
||||
svc->status == EXIT_SUCCESS ?
|
||||
STATUS_OK : STATUS_FAIL, true);
|
||||
if (singleshot == 0 && queue == NULL)
|
||||
target_completed(target);
|
||||
if (svc->status != EXIT_SUCCESS)
|
||||
goto out_failure;
|
||||
break;
|
||||
case SVC_ONCE:
|
||||
singleshot -= 1;
|
||||
print_status(svc->desc,
|
||||
svc->status == EXIT_SUCCESS ?
|
||||
STATUS_OK : STATUS_FAIL, false);
|
||||
if (singleshot == 0 && queue == NULL && !waiting)
|
||||
target_completed(target);
|
||||
if (svc->status != EXIT_SUCCESS)
|
||||
goto out_failure;
|
||||
break;
|
||||
}
|
||||
svc->next = completed;
|
||||
completed = svc;
|
||||
return;
|
||||
out_failure:
|
||||
svc->next = failed;
|
||||
failed = svc;
|
||||
}
|
||||
|
||||
void supervisor_handle_exited(pid_t pid, int status)
|
||||
{
|
||||
service_t *prev = NULL, *svc = running;
|
||||
|
||||
while (svc != NULL && svc->pid != pid) {
|
||||
prev = svc;
|
||||
svc = svc->next;
|
||||
}
|
||||
|
||||
if (svc == NULL)
|
||||
return;
|
||||
|
||||
if (prev != NULL) {
|
||||
prev->next = svc->next;
|
||||
} else {
|
||||
running = svc->next;
|
||||
}
|
||||
|
||||
svc->status = status;
|
||||
svc->next = terminated;
|
||||
terminated = svc;
|
||||
}
|
||||
|
||||
void supervisor_set_target(int next)
|
||||
{
|
||||
service_t *svc;
|
||||
|
||||
if (target == TGT_REBOOT || target == TGT_SHUTDOWN || next == target)
|
||||
return;
|
||||
|
||||
if (next == TGT_REBOOT || next == TGT_SHUTDOWN) {
|
||||
while (queue != NULL) {
|
||||
svc = queue;
|
||||
queue = queue->next;
|
||||
delsvc(svc);
|
||||
}
|
||||
}
|
||||
|
||||
if (queue != NULL) {
|
||||
for (svc = queue; svc->next != NULL; svc = svc->next)
|
||||
;
|
||||
svc->next = cfg.targets[next];
|
||||
} else {
|
||||
queue = cfg.targets[next];
|
||||
}
|
||||
|
||||
cfg.targets[next] = NULL;
|
||||
target = next;
|
||||
}
|
||||
|
||||
void supervisor_init(void)
|
||||
{
|
||||
int status = STATUS_OK;
|
||||
|
||||
if (svcscan(SVCDIR, &cfg))
|
||||
status = STATUS_FAIL;
|
||||
|
||||
target = TGT_BOOT;
|
||||
queue = cfg.targets[TGT_BOOT];
|
||||
cfg.targets[TGT_BOOT] = NULL;
|
||||
|
||||
print_status("reading configuration from " SVCDIR, status, false);
|
||||
}
|
||||
|
||||
void supervisor_reload_config(void)
|
||||
{
|
||||
service_list_t newcfg;
|
||||
service_t *svc;
|
||||
int i;
|
||||
|
||||
if (svcscan(SVCDIR, &newcfg))
|
||||
return;
|
||||
|
||||
for (i = 0; i < TGT_MAX; ++i) {
|
||||
if (cfg.targets[i] == NULL) {
|
||||
remove_not_in_list(&queue, newcfg.targets[i], i);
|
||||
remove_not_in_list(&terminated, newcfg.targets[i], i);
|
||||
remove_not_in_list(&completed, newcfg.targets[i], i);
|
||||
remove_not_in_list(&failed, newcfg.targets[i], i);
|
||||
|
||||
while (newcfg.targets[i] != NULL) {
|
||||
svc = newcfg.targets[i];
|
||||
newcfg.targets[i] = svc->next;
|
||||
|
||||
if (have_service(svc)) {
|
||||
delsvc(svc);
|
||||
} else {
|
||||
svc->id = service_id++;
|
||||
svc->status = EXIT_SUCCESS;
|
||||
svc->next = completed;
|
||||
completed = svc;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
svc = cfg.targets[i];
|
||||
cfg.targets[i] = newcfg.targets[i];
|
||||
newcfg.targets[i] = svc;
|
||||
}
|
||||
}
|
||||
|
||||
del_svc_list(&newcfg);
|
||||
}
|
||||
|
||||
bool supervisor_process_queues(void)
|
||||
{
|
||||
service_t *svc;
|
||||
|
||||
if (terminated != NULL) {
|
||||
svc = terminated;
|
||||
terminated = terminated->next;
|
||||
|
||||
handle_terminated_service(svc);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (waiting || queue == NULL)
|
||||
return false;
|
||||
|
||||
svc = queue;
|
||||
queue = queue->next;
|
||||
|
||||
if (!(svc->flags & SVC_FLAG_HAS_EXEC)) {
|
||||
print_status(svc->desc, STATUS_OK, false);
|
||||
svc->status = EXIT_SUCCESS;
|
||||
svc->next = completed;
|
||||
completed = svc;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (start_service(svc) != 0)
|
||||
return true;
|
||||
|
||||
switch (svc->type) {
|
||||
case SVC_WAIT:
|
||||
print_status(svc->desc, STATUS_WAIT, false);
|
||||
waiting = true;
|
||||
break;
|
||||
case SVC_RESPAWN:
|
||||
print_status(svc->desc, STATUS_STARTED, false);
|
||||
break;
|
||||
case SVC_ONCE:
|
||||
singleshot += 1;
|
||||
break;
|
||||
}
|
||||
out:
|
||||
if (singleshot == 0 && queue == NULL && !waiting)
|
||||
target_completed(target);
|
||||
return true;
|
||||
}
|
||||
|
||||
static int send_svc_list(int fd, const void *dst, size_t addrlen,
|
||||
E_SERVICE_STATE filter, E_SERVICE_STATE state,
|
||||
service_t *list)
|
||||
{
|
||||
if (filter != ESS_NONE && filter != state)
|
||||
return 0;
|
||||
|
||||
while (list != NULL) {
|
||||
if (init_socket_send_status(fd, dst, addrlen, state, list))
|
||||
return -1;
|
||||
|
||||
list = list->next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void supervisor_answer_status_request(int fd, const void *dst, size_t addrlen,
|
||||
E_SERVICE_STATE filter)
|
||||
{
|
||||
if (send_svc_list(fd, dst, addrlen, filter, ESS_RUNNING, running))
|
||||
return;
|
||||
if (send_svc_list(fd, dst, addrlen, filter, ESS_DONE, completed))
|
||||
return;
|
||||
if (send_svc_list(fd, dst, addrlen, filter, ESS_FAILED, failed))
|
||||
return;
|
||||
if (send_svc_list(fd, dst, addrlen, filter, ESS_ENQUEUED, queue))
|
||||
return;
|
||||
if (send_svc_list(fd, dst, addrlen, filter, ESS_ENQUEUED, terminated))
|
||||
return;
|
||||
init_socket_send_status(fd, dst, addrlen, ESS_NONE, NULL);
|
||||
}
|
||||
|
||||
static service_t *remove_by_id(service_t **list, int id)
|
||||
{
|
||||
service_t *svc = *list, *prev = NULL;
|
||||
|
||||
while (svc != NULL && svc->id != id) {
|
||||
prev = svc;
|
||||
svc = svc->next;
|
||||
}
|
||||
|
||||
if (svc != NULL) {
|
||||
if (prev == NULL) {
|
||||
*list = svc->next;
|
||||
} else {
|
||||
prev->next = svc->next;
|
||||
}
|
||||
}
|
||||
|
||||
return svc;
|
||||
}
|
||||
|
||||
void supervisor_start(int id)
|
||||
{
|
||||
service_t *svc;
|
||||
|
||||
svc = remove_by_id(&completed, id);
|
||||
if (svc != NULL)
|
||||
goto found;
|
||||
|
||||
svc = remove_by_id(&failed, id);
|
||||
if (svc != NULL)
|
||||
goto found;
|
||||
|
||||
return;
|
||||
found:
|
||||
svc->rspwn_count = 0;
|
||||
svc->flags &= ~SVC_FLAG_ADMIN_STOPPED;
|
||||
svc->next = queue;
|
||||
queue = svc;
|
||||
}
|
||||
|
||||
void supervisor_stop(int id)
|
||||
{
|
||||
service_t *svc;
|
||||
|
||||
for (svc = running; svc != NULL; svc = svc->next) {
|
||||
if (svc->id == id)
|
||||
break;
|
||||
}
|
||||
|
||||
if (svc != NULL) {
|
||||
/* TODO: something more sophisticated? */
|
||||
svc->flags |= SVC_FLAG_ADMIN_STOPPED;
|
||||
kill(svc->pid, SIGTERM);
|
||||
}
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "init.h"
|
||||
|
||||
static service_t *running = NULL; /* currently supervised services */
|
||||
static int singleshot = 0; /* active singleshot services */
|
||||
|
||||
bool svclist_have_singleshot(void)
|
||||
{
|
||||
return singleshot > 0;
|
||||
}
|
||||
|
||||
void svclist_add(service_t *svc)
|
||||
{
|
||||
svc->next = running;
|
||||
running = svc;
|
||||
|
||||
if (svc->type == SVC_ONCE)
|
||||
singleshot += 1;
|
||||
}
|
||||
|
||||
service_t *svclist_remove(pid_t pid)
|
||||
{
|
||||
service_t *prev = NULL, *svc = running;
|
||||
|
||||
while (svc != NULL) {
|
||||
if (svc->pid == pid) {
|
||||
if (prev != NULL) {
|
||||
prev->next = svc->next;
|
||||
} else {
|
||||
running = svc->next;
|
||||
}
|
||||
svc->next = NULL;
|
||||
|
||||
if (svc->type == SVC_ONCE)
|
||||
singleshot -= 1;
|
||||
break;
|
||||
}
|
||||
|
||||
prev = svc;
|
||||
svc = svc->next;
|
||||
}
|
||||
|
||||
return svc;
|
||||
}
|
|
@ -1,17 +1,15 @@
|
|||
HEADRS = lib/include/util.h lib/include/service.h lib/include/telinit.h
|
||||
|
||||
libinit_a_SOURCES = lib/util/delsvc.c lib/util/svcmap.c lib/util/enum_by_name.c
|
||||
libinit_a_SOURCES += lib/util/rdsvc.c lib/util/svcscan.c lib/util/mksock.c
|
||||
libinit_a_SOURCES += lib/util/del_svc_list.c lib/util/svc_tsort.c
|
||||
libinit_a_SOURCES += lib/util/opensock.c lib/util/enum_to_name.c $(HEADRS)
|
||||
libinit_a_SOURCES = lib/init/delsvc.c lib/init/svcmap.c lib/init/rdsvc.c
|
||||
libinit_a_SOURCES += lib/init/svcscan.c lib/init/del_svc_list.c
|
||||
libinit_a_SOURCES += lib/init/svc_tsort.c lib/include/service.h
|
||||
libinit_a_SOURCES += lib/init/init_socket_open.c lib/init/free_init_status.c
|
||||
libinit_a_SOURCES += lib/include/initsock.h lib/init/init_socket_send_request.c
|
||||
libinit_a_SOURCES += lib/init/init_socket_recv_status.c
|
||||
libinit_a_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
libinit_a_CFLAGS = $(AM_CFLAGS)
|
||||
|
||||
libcfg_a_SOURCES = lib/libcfg/rdline.c lib/libcfg/unescape.c
|
||||
libcfg_a_SOURCES = lib/libcfg/rdline.c lib/libcfg/unescape.c lib/libcfg/rdcfg.c
|
||||
libcfg_a_SOURCES += lib/libcfg/pack_argv.c lib/include/libcfg.h
|
||||
libcfg_a_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
libcfg_a_CFLAGS = $(AM_CFLAGS)
|
||||
|
||||
EXTRA_DIST += $(HEADRS) lib/include/libcfg.h
|
||||
|
||||
noinst_LIBRARIES += libinit.a libcfg.a
|
||||
|
|
65
lib/include/initsock.h
Normal file
65
lib/include/initsock.h
Normal file
|
@ -0,0 +1,65 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#ifndef INITSOCK_H
|
||||
#define INITSOCK_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "service.h"
|
||||
|
||||
#define INIT_SOCK_PATH SOCKDIR "/init.sock"
|
||||
|
||||
typedef enum {
|
||||
EIR_STATUS = 0x00,
|
||||
EIR_START = 0x01,
|
||||
EIR_STOP = 0x02,
|
||||
} E_INIT_REQUEST;
|
||||
|
||||
typedef enum {
|
||||
ESS_NONE = 0x00,
|
||||
ESS_RUNNING = 0x01,
|
||||
ESS_ENQUEUED = 0x02,
|
||||
ESS_DONE = 0x03,
|
||||
ESS_FAILED = 0x04
|
||||
} E_SERVICE_STATE;
|
||||
|
||||
typedef struct {
|
||||
uint8_t rq;
|
||||
uint8_t padd[3];
|
||||
|
||||
union {
|
||||
struct {
|
||||
uint8_t filter;
|
||||
uint8_t padd[3];
|
||||
} status;
|
||||
|
||||
struct {
|
||||
uint32_t id;
|
||||
} startstop;
|
||||
} arg;
|
||||
} init_request_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t state;
|
||||
uint8_t exit_status;
|
||||
uint8_t padd[2];
|
||||
int32_t id;
|
||||
} init_response_status_t;
|
||||
|
||||
typedef struct {
|
||||
E_SERVICE_STATE state;
|
||||
int exit_status;
|
||||
int id;
|
||||
char *filename;
|
||||
char *service_name;
|
||||
} init_status_t;
|
||||
|
||||
int init_socket_open(const char *tmppath);
|
||||
|
||||
int init_socket_send_request(int fd, E_INIT_REQUEST rq, ...);
|
||||
|
||||
int init_socket_recv_status(int fd, init_status_t *resp);
|
||||
|
||||
void free_init_status(init_status_t *resp);
|
||||
|
||||
#endif /* INITSOCK_H */
|
|
@ -1,52 +1,48 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#ifndef LIBCONFIG_H
|
||||
#define LIBCONFIG_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
int fd; /* input file descriptor */
|
||||
const char *argstr; /* if not NULL, read from this instead */
|
||||
|
||||
const char *filename; /* input file name */
|
||||
size_t lineno; /* current line number */
|
||||
|
||||
size_t i; /* buffer offset */
|
||||
char buffer[256]; /* current line, null-terminated */
|
||||
FILE *fp;
|
||||
char *line;
|
||||
|
||||
int argc;
|
||||
const char *const *argv;
|
||||
|
||||
bool string; /* inside a string? */
|
||||
bool escape; /* reading an escape sequence? */
|
||||
bool comment; /* inside a comment */
|
||||
} rdline_t;
|
||||
|
||||
typedef struct {
|
||||
/* keyword to map the callback to */
|
||||
const char *key;
|
||||
|
||||
/*
|
||||
If set, allow grouping repetitions of the keyword in a single
|
||||
multi line '{' ... '}' block. The callback is called for each
|
||||
line.
|
||||
*/
|
||||
unsigned int allow_block : 1;
|
||||
|
||||
int (*handle)(void *obj, char *arg, rdline_t *rd);
|
||||
} cfg_param_t;
|
||||
|
||||
/*
|
||||
Initialize the config line scanner.
|
||||
|
||||
The scanner reads from the provided fd. The filename is used for
|
||||
error reporting. An argument count and vector can be set for argument
|
||||
substitution in rdline.
|
||||
The scanner opens the filename relative to the passed dirfd. An
|
||||
argument count and vector can be set for argument substitution
|
||||
in rdline.
|
||||
|
||||
Returns 0 on success.
|
||||
*/
|
||||
void rdline_init(rdline_t *t, int fd, const char *filename,
|
||||
int argc, const char *const *argv);
|
||||
int rdline_init(rdline_t *t, int dirfd, const char *filename,
|
||||
int argc, const char *const *argv);
|
||||
|
||||
void rdline_cleanup(rdline_t *t);
|
||||
|
||||
/*
|
||||
Read from file until end-of-file or a line feed is encountered.
|
||||
|
@ -69,9 +65,8 @@ void rdline_init(rdline_t *t, int fd, const char *filename,
|
|||
outside the bounds set by argc, processing fails. On success,
|
||||
the argv value is inserted and processed as described above.
|
||||
- A '%' character can be escaped by writing '%%' or, if inside
|
||||
a double quite string, by writing \%.
|
||||
- An attempt to use such an indexed argument inside an argument
|
||||
expansion, results in failure.
|
||||
a double quoted string, by writing \%.
|
||||
- Arguments are pasted as is. Substitution is not recursive.
|
||||
- If the resulting line is empty, processing is restarted.
|
||||
*/
|
||||
int rdline(rdline_t *t);
|
||||
|
@ -95,4 +90,12 @@ int unescape(char *src);
|
|||
*/
|
||||
int pack_argv(char *str);
|
||||
|
||||
/*
|
||||
Parse a configuration file containing '<keyword> [arguments...]' lines.
|
||||
The cfgobj is passed to the callback in the params array.
|
||||
|
||||
Returns zero on success.
|
||||
*/
|
||||
int rdcfg(void *cfgobj, rdline_t *rd, const cfg_param_t *params, size_t count);
|
||||
|
||||
#endif /* LIBCONFIG_H */
|
||||
|
|
|
@ -1,25 +1,15 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#ifndef SERVICE_H
|
||||
#define SERVICE_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef struct exec_t {
|
||||
struct exec_t *next;
|
||||
int argc; /* number of elements in argument vector */
|
||||
char args[]; /* argument vectot string blob */
|
||||
} exec_t;
|
||||
|
||||
enum {
|
||||
/*
|
||||
Start the service in the background and continue with
|
||||
|
@ -33,34 +23,25 @@ enum {
|
|||
it terminates.
|
||||
*/
|
||||
SVC_RESPAWN,
|
||||
|
||||
SVC_MAX
|
||||
};
|
||||
|
||||
enum {
|
||||
TGT_BOOT = 0, /* run service when the system boots */
|
||||
TGT_SHUTDOWN, /* run service when at system shut down */
|
||||
TGT_REBOOT, /* run service when during system reboot */
|
||||
TGT_CAD, /* run service when CTRL+ALT+DEL is pressed */
|
||||
|
||||
TGT_MAX
|
||||
};
|
||||
|
||||
enum {
|
||||
RDSVC_NO_FNAME = 0x01, /* do not store a copy of the filename */
|
||||
RDSVC_NO_EXEC = 0x02, /* do not store executable script */
|
||||
RDSVC_NO_CTTY = 0x04, /* do not store the controlling tty */
|
||||
RDSVC_NO_DEPS = 0x08, /* do not store dependencies */
|
||||
};
|
||||
|
||||
enum {
|
||||
/* truncate stdout */
|
||||
SVC_FLAG_TRUNCATE_OUT = 0x01,
|
||||
};
|
||||
|
||||
typedef struct exec_t {
|
||||
struct exec_t *next;
|
||||
int argc; /* number of elements in argument vector */
|
||||
char args[]; /* argument vectot string blob */
|
||||
} exec_t;
|
||||
SVC_FLAG_HAS_EXEC = 0x10,
|
||||
SVC_FLAG_ADMIN_STOPPED = 0x20,
|
||||
};
|
||||
|
||||
typedef struct service_t {
|
||||
struct service_t *next;
|
||||
|
@ -72,6 +53,7 @@ typedef struct service_t {
|
|||
char *desc; /* description string */
|
||||
char *ctty; /* controlling tty or log file */
|
||||
int rspwn_limit; /* maximum respawn count */
|
||||
int rspwn_count; /* services respawn counter */
|
||||
unsigned int flags; /* SVC_FLAG_* bit field */
|
||||
|
||||
/* linked list of command lines to execute */
|
||||
|
@ -85,6 +67,7 @@ typedef struct service_t {
|
|||
|
||||
pid_t pid;
|
||||
int status; /* process exit status */
|
||||
int id; /* service ID used by initd */
|
||||
|
||||
char name[]; /* canonical service name */
|
||||
} service_t;
|
||||
|
@ -96,7 +79,7 @@ typedef struct {
|
|||
/*
|
||||
Read a service from a file.
|
||||
*/
|
||||
service_t *rdsvc(int dirfd, const char *filename, int flags);
|
||||
service_t *rdsvc(int dirfd, const char *filename);
|
||||
|
||||
void delsvc(service_t *svc);
|
||||
|
||||
|
@ -107,7 +90,7 @@ void delsvc(service_t *svc);
|
|||
Returns 0 on success, -1 on failure. The function takes care of
|
||||
printing error messages on failure.
|
||||
*/
|
||||
int svcscan(const char *directory, service_list_t *list, int flags);
|
||||
int svcscan(const char *directory, service_list_t *list);
|
||||
|
||||
void del_svc_list(service_list_t *list);
|
||||
|
||||
|
@ -125,4 +108,3 @@ const char *svc_target_to_string(int target);
|
|||
int svc_target_from_string(const char *target);
|
||||
|
||||
#endif /* SERVICE_H */
|
||||
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef TELINIT_H
|
||||
#define TELINIT_H
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#define INITSOCK SOCKDIR "/" "initd.socket"
|
||||
|
||||
enum {
|
||||
TI_SHUTDOWN = 1,
|
||||
TI_REBOOT = 2,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
int type; /* TI_* message type identifier */
|
||||
} ti_msg_t;
|
||||
|
||||
/* Try to connect to the init socket. */
|
||||
int opensock(void);
|
||||
|
||||
#endif /* TELINIT_H */
|
||||
|
|
@ -1,74 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define NORETURN __attribute__((noreturn))
|
||||
#endif
|
||||
|
||||
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
int value;
|
||||
} enum_map_t;
|
||||
|
||||
enum {
|
||||
/* only allow root to connect */
|
||||
SOCK_FLAG_ROOT_ONLY = 0x01,
|
||||
|
||||
/* allow everyone to connect */
|
||||
SOCK_FLAG_EVERYONE = 0x02,
|
||||
|
||||
/* create a datagram socket, otherwise use a stream socket */
|
||||
SOCK_FLAG_DGRAM = 0x04,
|
||||
};
|
||||
|
||||
/*
|
||||
Search through an array of enum_map_t entries to resolve a string to
|
||||
a numeric value. The end of the map is indicated by a sentinel entry
|
||||
with the name set to NULL.
|
||||
*/
|
||||
const enum_map_t *enum_by_name(const enum_map_t *map, const char *name);
|
||||
|
||||
/*
|
||||
Search through an array of enum_map_t entries to resolve a numeric
|
||||
value to a string name. The end of the map is indicated by a sentinel
|
||||
entry with the name set to NULL.
|
||||
*/
|
||||
const char *enum_to_name(const enum_map_t *map, int value);
|
||||
|
||||
/*
|
||||
Create a UNIX stream socket at the given path.
|
||||
|
||||
Returns the socket fd, -1 on failure. The function takes care of
|
||||
printing error messages on failure.
|
||||
|
||||
The socket has the CLOEXEC flag set.
|
||||
*/
|
||||
int mksock(const char *path, int flags);
|
||||
|
||||
#endif /* UTIL_H */
|
||||
|
19
lib/init/del_svc_list.c
Normal file
19
lib/init/del_svc_list.c
Normal file
|
@ -0,0 +1,19 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "service.h"
|
||||
|
||||
void del_svc_list(service_list_t *list)
|
||||
{
|
||||
service_t *svc;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < TGT_MAX; ++i) {
|
||||
while (list->targets[i] != NULL) {
|
||||
svc = list->targets[i];
|
||||
list->targets[i] = svc->next;
|
||||
|
||||
delsvc(svc);
|
||||
}
|
||||
}
|
||||
}
|
27
lib/init/delsvc.c
Normal file
27
lib/init/delsvc.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "service.h"
|
||||
|
||||
void delsvc(service_t *svc)
|
||||
{
|
||||
exec_t *e;
|
||||
|
||||
if (svc == NULL)
|
||||
return;
|
||||
|
||||
while (svc->exec != NULL) {
|
||||
e = svc->exec;
|
||||
svc->exec = e->next;
|
||||
|
||||
free(e);
|
||||
}
|
||||
|
||||
free(svc->before);
|
||||
free(svc->after);
|
||||
free(svc->fname);
|
||||
free(svc->desc);
|
||||
free(svc->exec);
|
||||
free(svc->ctty);
|
||||
free(svc);
|
||||
}
|
10
lib/init/free_init_status.c
Normal file
10
lib/init/free_init_status.c
Normal file
|
@ -0,0 +1,10 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "initsock.h"
|
||||
|
||||
void free_init_status(init_status_t *resp)
|
||||
{
|
||||
free(resp->filename);
|
||||
free(resp->service_name);
|
||||
}
|
46
lib/init/init_socket_open.c
Normal file
46
lib/init/init_socket_open.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "initsock.h"
|
||||
|
||||
int init_socket_open(const char *tmppath)
|
||||
{
|
||||
struct sockaddr_un un;
|
||||
int fd;
|
||||
|
||||
fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
|
||||
if (fd < 0) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&un, 0, sizeof(un));
|
||||
un.sun_family = AF_UNIX;
|
||||
|
||||
strcpy(un.sun_path, tmppath);
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&un, sizeof(un))) {
|
||||
fprintf(stderr, "bind: %s: %s", tmppath, strerror(errno));
|
||||
close(fd);
|
||||
unlink(tmppath);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&un, 0, sizeof(un));
|
||||
un.sun_family = AF_UNIX;
|
||||
|
||||
strcpy(un.sun_path, INIT_SOCK_PATH);
|
||||
|
||||
if (connect(fd, (struct sockaddr *)&un, sizeof(un))) {
|
||||
perror("connect: " INIT_SOCK_PATH);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
87
lib/init/init_socket_recv_status.c
Normal file
87
lib/init/init_socket_recv_status.c
Normal file
|
@ -0,0 +1,87 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "initsock.h"
|
||||
|
||||
static int read_retry(int fd, void *buffer, size_t size)
|
||||
{
|
||||
ssize_t ret;
|
||||
retry:
|
||||
ret = read(fd, buffer, size);
|
||||
|
||||
if (ret < 0) {
|
||||
if (errno == EINTR)
|
||||
goto retry;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((size_t)ret < size) {
|
||||
errno = EPROTO;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static char *read_string(int fd)
|
||||
{
|
||||
uint16_t len;
|
||||
char *buffer;
|
||||
int ret;
|
||||
|
||||
ret = read_retry(fd, &len, sizeof(len));
|
||||
if (ret <= 0)
|
||||
return NULL;
|
||||
|
||||
len = be16toh(len);
|
||||
|
||||
buffer = calloc(1, len + 1);
|
||||
if (buffer == NULL)
|
||||
return NULL;
|
||||
|
||||
if (len > 0) {
|
||||
ret = read_retry(fd, buffer, len);
|
||||
|
||||
if (ret <= 0) {
|
||||
ret = errno;
|
||||
free(buffer);
|
||||
errno = ret;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int init_socket_recv_status(int fd, init_status_t *resp)
|
||||
{
|
||||
init_response_status_t info;
|
||||
|
||||
memset(resp, 0, sizeof(*resp));
|
||||
|
||||
if (read_retry(fd, &info, sizeof(info)) <= 0)
|
||||
return -1;
|
||||
|
||||
resp->state = info.state;
|
||||
resp->exit_status = info.exit_status;
|
||||
resp->id = be32toh(info.id);
|
||||
|
||||
if (resp->state == ESS_NONE)
|
||||
return 0;
|
||||
|
||||
resp->filename = read_string(fd);
|
||||
if (resp->filename == NULL)
|
||||
return -1;
|
||||
|
||||
resp->service_name = read_string(fd);
|
||||
if (resp->service_name == NULL)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
45
lib/init/init_socket_send_request.c
Normal file
45
lib/init/init_socket_send_request.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <endian.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "initsock.h"
|
||||
|
||||
int init_socket_send_request(int fd, E_INIT_REQUEST rq, ...)
|
||||
{
|
||||
init_request_t request;
|
||||
ssize_t ret;
|
||||
va_list ap;
|
||||
|
||||
memset(&request, 0, sizeof(request));
|
||||
request.rq = rq;
|
||||
|
||||
va_start(ap, rq);
|
||||
switch (rq) {
|
||||
case EIR_STATUS:
|
||||
request.arg.status.filter = va_arg(ap, E_SERVICE_STATE);
|
||||
break;
|
||||
case EIR_START:
|
||||
case EIR_STOP:
|
||||
request.arg.startstop.id = htobe32(va_arg(ap, int));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
retry:
|
||||
ret = write(fd, &request, sizeof(request));
|
||||
|
||||
if (ret < 0) {
|
||||
if (errno == EINTR)
|
||||
goto retry;
|
||||
perror(INIT_SOCK_PATH);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,20 +1,4 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
@ -27,7 +11,6 @@
|
|||
|
||||
#include "service.h"
|
||||
#include "libcfg.h"
|
||||
#include "util.h"
|
||||
|
||||
static int try_unescape(char *arg, rdline_t *rd)
|
||||
{
|
||||
|
@ -60,16 +43,20 @@ static int try_pack_argv(char *str, rdline_t *rd)
|
|||
return count;
|
||||
}
|
||||
|
||||
static int svc_desc(service_t *svc, char *arg, rdline_t *rd)
|
||||
static int svc_desc(void *user, char *arg, rdline_t *rd)
|
||||
{
|
||||
service_t *svc = user;
|
||||
|
||||
if (try_unescape(arg, rd))
|
||||
return -1;
|
||||
svc->desc = try_strdup(arg, rd);
|
||||
return svc->desc == NULL ? -1 : 0;
|
||||
}
|
||||
|
||||
static int svc_tty(service_t *svc, char *arg, rdline_t *rd)
|
||||
static int svc_tty(void *user, char *arg, rdline_t *rd)
|
||||
{
|
||||
service_t *svc = user;
|
||||
|
||||
if (strncmp(arg, "truncate", 8) == 0 && isspace(arg[8])) {
|
||||
svc->flags |= SVC_FLAG_TRUNCATE_OUT;
|
||||
arg += 8;
|
||||
|
@ -84,10 +71,13 @@ static int svc_tty(service_t *svc, char *arg, rdline_t *rd)
|
|||
return svc->ctty == NULL ? -1 : 0;
|
||||
}
|
||||
|
||||
static int svc_exec(service_t *svc, char *arg, rdline_t *rd)
|
||||
static int svc_exec(void *user, char *arg, rdline_t *rd)
|
||||
{
|
||||
service_t *svc = user;
|
||||
exec_t *e, *end;
|
||||
|
||||
svc->flags |= SVC_FLAG_HAS_EXEC;
|
||||
|
||||
e = calloc(1, sizeof(*e) + strlen(arg) + 1);
|
||||
if (e == NULL) {
|
||||
fprintf(stderr, "%s: %zu: out of memory\n",
|
||||
|
@ -111,8 +101,10 @@ static int svc_exec(service_t *svc, char *arg, rdline_t *rd)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int svc_before(service_t *svc, char *arg, rdline_t *rd)
|
||||
static int svc_before(void *user, char *arg, rdline_t *rd)
|
||||
{
|
||||
service_t *svc = user;
|
||||
|
||||
if (svc->before != NULL) {
|
||||
fprintf(stderr, "%s: %zu: 'before' dependencies respecified\n",
|
||||
rd->filename, rd->lineno);
|
||||
|
@ -127,8 +119,10 @@ static int svc_before(service_t *svc, char *arg, rdline_t *rd)
|
|||
return (svc->num_before < 0) ? -1 : 0;
|
||||
}
|
||||
|
||||
static int svc_after(service_t *svc, char *arg, rdline_t *rd)
|
||||
static int svc_after(void *user, char *arg, rdline_t *rd)
|
||||
{
|
||||
service_t *svc = user;
|
||||
|
||||
if (svc->after != NULL) {
|
||||
fprintf(stderr, "%s: %zu: 'after' dependencies respecified\n",
|
||||
rd->filename, rd->lineno);
|
||||
|
@ -143,8 +137,9 @@ static int svc_after(service_t *svc, char *arg, rdline_t *rd)
|
|||
return (svc->num_after < 0) ? -1 : 0;
|
||||
}
|
||||
|
||||
static int svc_type(service_t *svc, char *arg, rdline_t *rd)
|
||||
static int svc_type(void *user, char *arg, rdline_t *rd)
|
||||
{
|
||||
service_t *svc = user;
|
||||
int count = try_pack_argv(arg, rd);
|
||||
|
||||
if (count < 1)
|
||||
|
@ -189,8 +184,9 @@ fail_limit:
|
|||
return -1;
|
||||
}
|
||||
|
||||
static int svc_target(service_t *svc, char *arg, rdline_t *rd)
|
||||
static int svc_target(void *user, char *arg, rdline_t *rd)
|
||||
{
|
||||
service_t *svc = user;
|
||||
int target;
|
||||
|
||||
if (try_unescape(arg, rd))
|
||||
|
@ -208,81 +204,22 @@ static int svc_target(service_t *svc, char *arg, rdline_t *rd)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static const struct svc_param {
|
||||
const char *key;
|
||||
|
||||
unsigned int allow_block : 1;
|
||||
|
||||
int flags;
|
||||
|
||||
int (*handle)(service_t *svc, char *arg, rdline_t *rd);
|
||||
} svc_params[] = {
|
||||
{ "description", 0, 0, svc_desc },
|
||||
{ "exec", 1, RDSVC_NO_EXEC, svc_exec },
|
||||
{ "type", 0, 0, svc_type },
|
||||
{ "target", 0, 0, svc_target },
|
||||
{ "tty", 0, RDSVC_NO_CTTY, svc_tty },
|
||||
{ "before", 0, RDSVC_NO_DEPS, svc_before },
|
||||
{ "after", 0, RDSVC_NO_DEPS, svc_after },
|
||||
static const cfg_param_t svc_params[] = {
|
||||
{ "description", 0, svc_desc },
|
||||
{ "exec", 1, svc_exec },
|
||||
{ "type", 0, svc_type },
|
||||
{ "target", 0, svc_target },
|
||||
{ "tty", 0, svc_tty },
|
||||
{ "before", 0, svc_before },
|
||||
{ "after", 0, svc_after },
|
||||
};
|
||||
|
||||
static int splitkv(rdline_t *rd, char **k, char **v)
|
||||
service_t *rdsvc(int dirfd, const char *filename)
|
||||
{
|
||||
char *key = rd->buffer, *value = rd->buffer;
|
||||
|
||||
while (*value != ' ' && *value != '\0') {
|
||||
if (!isalpha(*value)) {
|
||||
fprintf(stderr,
|
||||
"%s: %zu: unexpected '%c' in keyword\n",
|
||||
rd->filename, rd->lineno, *value);
|
||||
return -1;
|
||||
}
|
||||
++value;
|
||||
}
|
||||
|
||||
if (*value != ' ') {
|
||||
fprintf(stderr, "%s: %zu: expected argument after '%s'\n",
|
||||
rd->filename, rd->lineno, key);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*(value++) = '\0';
|
||||
|
||||
*k = key;
|
||||
*v = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct svc_param *find_param(rdline_t *rd, const char *name)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(svc_params); ++i) {
|
||||
if (!strcmp(svc_params[i].key, name))
|
||||
return svc_params + i;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s: %zu: unknown keyword '%s'\n",
|
||||
rd->filename, rd->lineno, name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
service_t *rdsvc(int dirfd, const char *filename, int flags)
|
||||
{
|
||||
const struct svc_param *p;
|
||||
const char *arg, *args[1];
|
||||
service_t *svc = NULL;
|
||||
char *key, *value;
|
||||
size_t argc, nlen;
|
||||
rdline_t rd;
|
||||
int fd, ret;
|
||||
|
||||
fd = openat(dirfd, filename, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
perror(filename);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
arg = strchr(filename, '@');
|
||||
if (arg != NULL) {
|
||||
|
@ -292,7 +229,8 @@ service_t *rdsvc(int dirfd, const char *filename, int flags)
|
|||
argc = 0;
|
||||
}
|
||||
|
||||
rdline_init(&rd, fd, filename, argc, args);
|
||||
if (rdline_init(&rd, dirfd, filename, argc, args))
|
||||
return NULL;
|
||||
|
||||
nlen = (arg != NULL) ? (size_t)(arg - filename) : strlen(filename);
|
||||
|
||||
|
@ -300,66 +238,25 @@ service_t *rdsvc(int dirfd, const char *filename, int flags)
|
|||
if (svc == NULL)
|
||||
goto fail_oom;
|
||||
|
||||
if (!(flags & RDSVC_NO_FNAME)) {
|
||||
svc->fname = strdup(filename);
|
||||
if (svc->fname == NULL)
|
||||
goto fail_oom;
|
||||
}
|
||||
svc->fname = strdup(filename);
|
||||
if (svc->fname == NULL)
|
||||
goto fail_oom;
|
||||
|
||||
memcpy(svc->name, filename, nlen);
|
||||
svc->id = -1;
|
||||
|
||||
while ((ret = rdline(&rd)) == 0) {
|
||||
if (splitkv(&rd, &key, &value))
|
||||
goto fail;
|
||||
|
||||
p = find_param(&rd, key);
|
||||
if (p == NULL)
|
||||
goto fail;
|
||||
|
||||
if (p->allow_block && *value == '{') {
|
||||
for (++value; *value == ' '; ++value)
|
||||
;
|
||||
|
||||
if (!(flags & p->flags)) {
|
||||
if (*value != '\0' &&
|
||||
p->handle(svc, value, &rd)) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
while ((ret = rdline(&rd)) == 0) {
|
||||
if (strcmp(rd.buffer, "}") == 0)
|
||||
break;
|
||||
if (flags & p->flags)
|
||||
continue;
|
||||
if (p->handle(svc, rd.buffer, &rd))
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
goto fail;
|
||||
if (ret > 0)
|
||||
goto fail_bra;
|
||||
} else {
|
||||
if (flags & p->flags)
|
||||
continue;
|
||||
if (p->handle(svc, value, &rd))
|
||||
goto fail;
|
||||
}
|
||||
if (rdcfg(svc, &rd, svc_params,
|
||||
sizeof(svc_params) / sizeof(svc_params[0]))) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
goto fail;
|
||||
|
||||
close(fd);
|
||||
out:
|
||||
rdline_cleanup(&rd);
|
||||
return svc;
|
||||
fail_bra:
|
||||
fprintf(stderr, "%s: missing '}' before end-of-file\n", filename);
|
||||
goto fail;
|
||||
fail_oom:
|
||||
fputs("out of memory\n", stderr);
|
||||
fail:
|
||||
delsvc(svc);
|
||||
close(fd);
|
||||
return NULL;
|
||||
svc = NULL;
|
||||
goto out;
|
||||
}
|
|
@ -1,20 +1,4 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
49
lib/init/svcmap.c
Normal file
49
lib/init/svcmap.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include <string.h>
|
||||
#include "service.h"
|
||||
|
||||
static const char *type_map[] = {
|
||||
"once",
|
||||
"wait",
|
||||
"respawn",
|
||||
};
|
||||
|
||||
static const char *target_map[] = {
|
||||
"boot",
|
||||
"shutdown",
|
||||
"reboot",
|
||||
};
|
||||
|
||||
const char *svc_type_to_string(int type)
|
||||
{
|
||||
return type >= 0 && type < SVC_MAX ? type_map[type] : NULL;
|
||||
}
|
||||
|
||||
int svc_type_from_string(const char *type)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(type_map) / sizeof(type_map[0]); ++i) {
|
||||
if (strcmp(type_map[i], type) == 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *svc_target_to_string(int target)
|
||||
{
|
||||
return target >= 0 && target < TGT_MAX ? target_map[target] : NULL;
|
||||
}
|
||||
|
||||
int svc_target_from_string(const char *target)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(target_map) / sizeof(target_map[0]); ++i) {
|
||||
if (strcmp(target_map[i], target) == 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
|
@ -1,20 +1,4 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stddef.h>
|
||||
|
@ -27,7 +11,7 @@
|
|||
|
||||
#include "service.h"
|
||||
|
||||
int svcscan(const char *directory, service_list_t *list, int flags)
|
||||
int svcscan(const char *directory, service_list_t *list)
|
||||
{
|
||||
int i, dfd, type, ret = 0;
|
||||
struct dirent *ent;
|
||||
|
@ -82,7 +66,7 @@ int svcscan(const char *directory, service_list_t *list, int flags)
|
|||
if (type != S_IFREG && type != S_IFLNK)
|
||||
continue;
|
||||
|
||||
svc = rdsvc(dfd, ent->d_name, flags);
|
||||
svc = rdsvc(dfd, ent->d_name);
|
||||
if (svc == NULL) {
|
||||
ret = -1;
|
||||
continue;
|
|
@ -1,20 +1,4 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
|
94
lib/libcfg/rdcfg.c
Normal file
94
lib/libcfg/rdcfg.c
Normal file
|
@ -0,0 +1,94 @@
|
|||
/* SPDX-License-Identifier: ISC */
|
||||
#include "libcfg.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
static const cfg_param_t *find_param(rdline_t *rd, const char *name,
|
||||
const cfg_param_t *params, size_t count)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < count; ++i) {
|
||||
if (!strcmp(params[i].key, name))
|
||||
return params + i;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s: %zu: unknown keyword '%s'\n",
|
||||
rd->filename, rd->lineno, name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int splitkv(rdline_t *rd, char **k, char **v)
|
||||
{
|
||||
char *key = rd->line, *value = rd->line;
|
||||
|
||||
while (*value != ' ' && *value != '\0') {
|
||||
if (!isalpha(*value)) {
|
||||
fprintf(stderr,
|
||||
"%s: %zu: unexpected '%c' in keyword\n",
|
||||
rd->filename, rd->lineno, *value);
|
||||
return -1;
|
||||
}
|
||||
++value;
|
||||
}
|
||||
|
||||
if (*value != ' ') {
|
||||
fprintf(stderr, "%s: %zu: expected argument after '%s'\n",
|
||||
rd->filename, rd->lineno, key);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*(value++) = '\0';
|
||||
|
||||
*k = key;
|
||||
*v = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rdcfg(void *cfgobj, rdline_t *rd, const cfg_param_t *params, size_t count)
|
||||
{
|
||||
const cfg_param_t *p;
|
||||
char *key, *value;
|
||||
int ret;
|
||||
|
||||
while ((ret = rdline(rd)) == 0) {
|
||||
if (splitkv(rd, &key, &value))
|
||||
return -1;
|
||||
|
||||
p = find_param(rd, key, params, count);
|
||||
if (p == NULL)
|
||||
return -1;
|
||||
|
||||
if (p->allow_block && *value == '{') {
|
||||
for (++value; *value == ' '; ++value)
|
||||
;
|
||||
|
||||
if (*value != '\0') {
|
||||
ret = p->handle(cfgobj, value, rd);
|
||||
if (ret)
|
||||
return -1;
|
||||
}
|
||||
|
||||
while ((ret = rdline(rd)) == 0) {
|
||||
if (strcmp(rd->line, "}") == 0)
|
||||
break;
|
||||
if (p->handle(cfgobj, rd->line, rd))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
if (ret > 0)
|
||||
goto fail_bra;
|
||||
} else if (p->handle(cfgobj, value, rd)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return ret < 0 ? -1 : 0;
|
||||
fail_bra:
|
||||
fprintf(stderr, "%s: missing '}' before end-of-file\n", rd->filename);
|
||||
return -1;
|
||||
}
|
|
@ -1,174 +1,172 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "libcfg.h"
|
||||
|
||||
static int rdline_getc(rdline_t *t)
|
||||
int rdline_init(rdline_t *t, int dirfd, const char *filename,
|
||||
int argc, const char *const *argv)
|
||||
{
|
||||
int ret;
|
||||
char c;
|
||||
int fd = openat(dirfd, filename, O_RDONLY);
|
||||
|
||||
if (t->argstr != NULL) {
|
||||
c = *(t->argstr++);
|
||||
if (c != '\0')
|
||||
goto out;
|
||||
if (fd == -1)
|
||||
goto fail_open;
|
||||
|
||||
t->argstr = NULL;
|
||||
}
|
||||
|
||||
do {
|
||||
ret = read(t->fd, &c, 1);
|
||||
} while (ret < 0 && errno == EINTR);
|
||||
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
|
||||
if (ret == 0) {
|
||||
if (t->i == 0) {
|
||||
errno = 0;
|
||||
return -1;
|
||||
}
|
||||
c = '\0';
|
||||
}
|
||||
out:
|
||||
return (c == '\n') ? '\0' : c;
|
||||
}
|
||||
|
||||
static int rdline_append(rdline_t *t, int c)
|
||||
{
|
||||
if (t->comment) {
|
||||
if (c != '\0')
|
||||
return 0;
|
||||
} else if (t->string) {
|
||||
if (t->escape) {
|
||||
t->escape = false;
|
||||
} else {
|
||||
if (c == '\\')
|
||||
t->escape = true;
|
||||
if (c == '"')
|
||||
t->string = false;
|
||||
}
|
||||
} else {
|
||||
if (isspace(c))
|
||||
c = ' ';
|
||||
if (c == ' ' && (t->i == 0 || t->buffer[t->i - 1] == ' '))
|
||||
return 0;
|
||||
if (c == '#') {
|
||||
t->comment = true;
|
||||
return 0;
|
||||
}
|
||||
if (c == '"')
|
||||
t->string = true;
|
||||
}
|
||||
|
||||
if (c == '\0') {
|
||||
while (t->i > 0 && t->buffer[t->i - 1] == ' ')
|
||||
t->i -= 1;
|
||||
}
|
||||
|
||||
if (t->i == sizeof(t->buffer))
|
||||
return -1;
|
||||
|
||||
t->buffer[t->i++] = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void rdline_init(rdline_t *t, int fd, const char *filename,
|
||||
int argc, const char *const *argv)
|
||||
{
|
||||
memset(t, 0, sizeof(*t));
|
||||
t->fd = fd;
|
||||
|
||||
t->fp = fdopen(fd, "r");
|
||||
if (t->fp == NULL) {
|
||||
close(fd);
|
||||
goto fail_open;
|
||||
}
|
||||
|
||||
t->filename = filename;
|
||||
t->argc = argc;
|
||||
t->argv = argv;
|
||||
return 0;
|
||||
fail_open:
|
||||
perror(filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int rdline(rdline_t *t)
|
||||
void rdline_cleanup(rdline_t *t)
|
||||
{
|
||||
const char *errstr;
|
||||
int c;
|
||||
retry:
|
||||
t->i = 0;
|
||||
t->argstr = NULL;
|
||||
t->string = t->escape = t->comment = false;
|
||||
free(t->line);
|
||||
fclose(t->fp);
|
||||
}
|
||||
|
||||
static int read_raw_line(rdline_t *t)
|
||||
{
|
||||
size_t len = 0;
|
||||
|
||||
free(t->line);
|
||||
t->line = NULL;
|
||||
|
||||
errno = 0;
|
||||
|
||||
if (getline(&t->line, &len, t->fp) < 0) {
|
||||
if (errno) {
|
||||
fprintf(stderr, "%s: %zu: %s\n", t->filename,
|
||||
t->lineno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
t->lineno += 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int normalize_line(rdline_t *t)
|
||||
{
|
||||
char *dst = t->line, *src = t->line;
|
||||
bool string = false;
|
||||
const char *errstr;
|
||||
int c, ret = 0;
|
||||
|
||||
while (isspace(*src))
|
||||
++src;
|
||||
|
||||
do {
|
||||
errno = 0;
|
||||
c = rdline_getc(t);
|
||||
if (c < 0) {
|
||||
if (errno == 0)
|
||||
return 1;
|
||||
errstr = strerror(errno);
|
||||
goto fail;
|
||||
}
|
||||
if (c == 0 && t->string) {
|
||||
errstr = "missing \"";
|
||||
goto fail;
|
||||
}
|
||||
c = *(src++);
|
||||
|
||||
if (c == '%') {
|
||||
c = rdline_getc(t);
|
||||
if (c == 0) {
|
||||
errstr = "unexpected end of line after '%%'";
|
||||
goto fail;
|
||||
}
|
||||
if (c < 0) {
|
||||
errstr = strerror(errno);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (c != '%') {
|
||||
if (!isdigit(c)) {
|
||||
errstr = "exptected digit after '%%'";
|
||||
goto fail;
|
||||
}
|
||||
if (c == '"') {
|
||||
string = !string;
|
||||
} else if (!string && c == '#') {
|
||||
c = '\0';
|
||||
} else if (!string && isspace(c)) {
|
||||
if (*src == '#' || *src == '\0' || isspace(*src))
|
||||
continue;
|
||||
c = ' ';
|
||||
} else if (c == '%') {
|
||||
*(dst++) = c;
|
||||
c = *(src++);
|
||||
if (isdigit(c)) {
|
||||
if ((c - '0') >= t->argc) {
|
||||
errstr = "argument out of range";
|
||||
goto fail;
|
||||
}
|
||||
if (t->argstr != NULL) {
|
||||
errstr = "recursive argument "
|
||||
"expansion";
|
||||
goto fail;
|
||||
}
|
||||
t->argstr = t->argv[c - '0'];
|
||||
continue;
|
||||
ret += strlen(t->argv[c - '0']);
|
||||
} else if (c != '%') {
|
||||
errstr = "expected digit after '%%'";
|
||||
goto fail;
|
||||
}
|
||||
} else if (string && c == '\\' && *src != '\0') {
|
||||
*(dst++) = c;
|
||||
c = *(src++);
|
||||
}
|
||||
|
||||
if (rdline_append(t, c)) {
|
||||
errstr = "line too long";
|
||||
goto fail;
|
||||
}
|
||||
*(dst++) = c;
|
||||
} while (c != '\0');
|
||||
|
||||
if (t->buffer[0] == '\0')
|
||||
goto retry;
|
||||
|
||||
return 0;
|
||||
if (string) {
|
||||
errstr = "missing \"";
|
||||
goto fail;
|
||||
}
|
||||
return ret;
|
||||
fail:
|
||||
fprintf(stderr, "%s: %zu: %s\n", t->filename, t->lineno, errstr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void substitute(rdline_t *t, char *dst, char *src)
|
||||
{
|
||||
bool string = false;
|
||||
|
||||
while (*src != '\0') {
|
||||
if (src[0] == '%' && isdigit(src[1])) {
|
||||
strcpy(dst, t->argv[src[1] - '0']);
|
||||
dst += strlen(dst);
|
||||
src += 2;
|
||||
} else if (src[0] == '%' && src[1] == '%') {
|
||||
*(dst++) = '%';
|
||||
src += 2;
|
||||
} else {
|
||||
if (*src == '"')
|
||||
string = !string;
|
||||
if (string && *src == '\\')
|
||||
*(dst++) = *(src++);
|
||||
*(dst++) = *(src++);
|
||||
}
|
||||
}
|
||||
|
||||
*(dst++) = '\0';
|
||||
}
|
||||
|
||||
int rdline(rdline_t *t)
|
||||
{
|
||||
char *buffer = NULL;
|
||||
int ret;
|
||||
|
||||
do {
|
||||
if ((ret = read_raw_line(t)))
|
||||
goto out;
|
||||
if ((ret = normalize_line(t)) < 0)
|
||||
goto out;
|
||||
} while (t->line[0] == '\0');
|
||||
|
||||
if (ret == 0) {
|
||||
substitute(t, t->line, t->line);
|
||||
return 0;
|
||||
}
|
||||
|
||||
buffer = calloc(1, strlen(t->line) + ret + 1);
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "%s: %zu: out of memory\n",
|
||||
t->filename, t->lineno);
|
||||
ret = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
substitute(t, buffer, t->line);
|
||||
ret = 0;
|
||||
out:
|
||||
free(t->line);
|
||||
t->line = buffer;
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -1,20 +1,4 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* SPDX-License-Identifier: ISC */
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "service.h"
|
||||
|
||||
void del_svc_list(service_list_t *list)
|
||||
{
|
||||
service_t *svc;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < TGT_MAX; ++i) {
|
||||
while (list->targets[i] != NULL) {
|
||||
svc = list->targets[i];
|
||||
list->targets[i] = svc->next;
|
||||
|
||||
delsvc(svc);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "service.h"
|
||||
|
||||
void delsvc(service_t *svc)
|
||||
{
|
||||
exec_t *e;
|
||||
|
||||
if (svc == NULL)
|
||||
return;
|
||||
|
||||
while (svc->exec != NULL) {
|
||||
e = svc->exec;
|
||||
svc->exec = e->next;
|
||||
|
||||
free(e);
|
||||
}
|
||||
|
||||
free(svc->before);
|
||||
free(svc->after);
|
||||
free(svc->fname);
|
||||
free(svc->desc);
|
||||
free(svc->exec);
|
||||
free(svc->ctty);
|
||||
free(svc);
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
const enum_map_t *enum_by_name(const enum_map_t *map, const char *name)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; map[i].name != NULL; ++i) {
|
||||
if (!strcmp(map[i].name, name))
|
||||
return map + i;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
const char *enum_to_name(const enum_map_t *map, int value)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; map[i].name != NULL; ++i) {
|
||||
if (map[i].value == value)
|
||||
return map[i].name;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
int mksock(const char *path, int flags)
|
||||
{
|
||||
struct sockaddr_un un;
|
||||
const char *errmsg;
|
||||
int fd, type;
|
||||
|
||||
type = (flags & SOCK_FLAG_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
|
||||
|
||||
fd = socket(AF_UNIX, type | SOCK_CLOEXEC, 0);
|
||||
if (fd < 0) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&un, 0, sizeof(un));
|
||||
un.sun_family = AF_UNIX;
|
||||
|
||||
strcpy(un.sun_path, path);
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&un, sizeof(un))) {
|
||||
errmsg ="bind";
|
||||
goto fail_errno;
|
||||
}
|
||||
|
||||
if (flags & SOCK_FLAG_ROOT_ONLY) {
|
||||
if (chown(path, 0, 0)) {
|
||||
errmsg = "chown";
|
||||
goto fail_errno;
|
||||
}
|
||||
|
||||
if (chmod(path, 0770)) {
|
||||
errmsg = "chmod";
|
||||
goto fail_errno;
|
||||
}
|
||||
} else if (flags & SOCK_FLAG_EVERYONE) {
|
||||
if (chmod(path, 0777)) {
|
||||
errmsg = "chmod";
|
||||
goto fail_errno;
|
||||
}
|
||||
}
|
||||
|
||||
if (!(flags & SOCK_FLAG_DGRAM)) {
|
||||
if (listen(fd, 10)) {
|
||||
errmsg = "listen";
|
||||
goto fail_errno;
|
||||
}
|
||||
}
|
||||
|
||||
return fd;
|
||||
fail_errno:
|
||||
fprintf(stderr, "%s: %s: %s\n", path, errmsg, strerror(errno));
|
||||
close(fd);
|
||||
unlink(path);
|
||||
return -1;
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "telinit.h"
|
||||
|
||||
int opensock(void)
|
||||
{
|
||||
struct sockaddr_un un;
|
||||
int fd;
|
||||
|
||||
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fd < 0) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&un, 0, sizeof(un));
|
||||
un.sun_family = AF_UNIX;
|
||||
|
||||
strcpy(un.sun_path, INITSOCK);
|
||||
|
||||
if (connect(fd, (struct sockaddr *)&un, sizeof(un))) {
|
||||
perror("connect: " INITSOCK);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/*
|
||||
* Copyright (C) 2018 - David Oberhollenzer
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "service.h"
|
||||
#include "util.h"
|
||||
|
||||
static const enum_map_t type_map[] = {
|
||||
{ "once", SVC_ONCE },
|
||||
{ "wait", SVC_WAIT },
|
||||
{ "respawn", SVC_RESPAWN },
|
||||
{ NULL, 0 },
|
||||
};
|
||||
|
||||
static const enum_map_t target_map[] = {
|
||||
{ "boot", TGT_BOOT },
|
||||
{ "shutdown", TGT_SHUTDOWN },
|
||||
{ "reboot", TGT_REBOOT },
|
||||
{ "ctrlaltdel", TGT_CAD },
|
||||
{ NULL, 0 },
|
||||
};
|
||||
|
||||
const char *svc_type_to_string(int type)
|
||||
{
|
||||
return enum_to_name(type_map, type);
|
||||
}
|
||||
|
||||
int svc_type_from_string(const char *type)
|
||||
{
|
||||
const enum_map_t *ent = enum_by_name(type_map, type);
|
||||
|
||||
return ent == NULL ? -1 : ent->value;
|
||||
}
|
||||
|
||||
const char *svc_target_to_string(int target)
|
||||
{
|
||||
return enum_to_name(target_map, target);
|
||||
}
|
||||
|
||||
int svc_target_from_string(const char *target)
|
||||
{
|
||||
const enum_map_t *ent = enum_by_name(target_map, target);
|
||||
|
||||
return ent == NULL ? -1 : ent->value;
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
#!/bin/sh -uef
|
||||
|
||||
# A small helper script taken from mtd-utils to generate a release tar ball.
|
||||
|
||||
fatal() {
|
||||
printf "Error: %s\n" "$1" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
Usage: ${0##*/} <new_ver> <outdir>
|
||||
|
||||
<new_ver> - mtd utils version to create in X.Y[.Z[-rcX]] format
|
||||
<outdir> - the output directory where to store the tarball
|
||||
EOF
|
||||
exit 0
|
||||
}
|
||||
|
||||
[ $# -eq 0 ] && usage
|
||||
[ $# -eq 2 ] || fatal "Insufficient or too many argumetns"
|
||||
|
||||
new_ver="$1"
|
||||
outdir="$2"
|
||||
|
||||
release_name="init-$new_ver"
|
||||
tag_name="v$new_ver"
|
||||
|
||||
# Make sure the input is sane and the makefile contains sensible version
|
||||
VER_REGEX="[0-9]\+.[0-9]\+\(.[0-9]\+\)\?\(-rc[0-9]\+\)\?"
|
||||
|
||||
echo "$new_ver" | grep -q -x "$VER_REGEX" ||
|
||||
fatal "please, provide new version in X.Y[.Z][-rcX] format"
|
||||
|
||||
grep -q -x "m4_define(\[RELEASE\], $VER_REGEX)" configure.ac ||
|
||||
fatal "configure.ac does not contain a valid version string"
|
||||
|
||||
# Make sure the git index is up-to-date
|
||||
[ -z "$(git status --porcelain)" ] || fatal "Git index is not up-to-date"
|
||||
|
||||
# Make sure the tag does not exist
|
||||
[ -z "$(git tag -l "$tag_name")" ] || fatal "Tag $tag_name already exists"
|
||||
|
||||
# Change the version in the configure.ac
|
||||
sed -i -e "s/^m4_define(\[RELEASE\], $VER_REGEX)/m4_define([RELEASE], $new_ver)/" configure.ac
|
||||
|
||||
# And commit the change
|
||||
git commit -m "Release $release_name" configure.ac
|
||||
|
||||
# Create new tag
|
||||
echo "Signing tag $tag_name"
|
||||
git tag -m "$release_name" "$tag_name"
|
||||
|
||||
# Prepare tarball
|
||||
./autogen.sh
|
||||
./configure
|
||||
make distcheck
|
||||
mkdir -p "$outdir"
|
||||
mv "$release_name.tar.xz" "$outdir"
|
|
@ -1,13 +0,0 @@
|
|||
#
|
||||
# Interface renaming rules
|
||||
#
|
||||
# Format: NAME,MAC,NEWNAME
|
||||
#
|
||||
# NAME and MAC are shell glob patterns. Both must match for a rule to apply.
|
||||
# The first matching rule is chosen (top to bottom).
|
||||
#
|
||||
# Interfaces with the same NEWNAME are sorted by MAC and have a running
|
||||
# index appended to their new name.
|
||||
#
|
||||
# Example: rename all ethernet interfaces to "port<X>"
|
||||
# eth*,*,port
|
|
@ -1,4 +0,0 @@
|
|||
helper_SCRIPTS += scripts/devfs.sh scripts/trymount.sh scripts/ifrename.sh
|
||||
helper_SCRIPTS += scripts/ifcfg.sh scripts/ifdown.sh scripts/modules_load.sh
|
||||
|
||||
EXTRA_DIST += scripts/trymount.sh scripts/ifdown.sh
|
|
@ -1,21 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
mount -t devtmpfs none /dev
|
||||
|
||||
[ -c /dev/console ] || mknod -m 600 /dev/console c 5 1
|
||||
[ -c /dev/tty ] || mknod -m 666 /dev/tty c 5 0
|
||||
[ -c /dev/null ] || mknod -m 666 /dev/null c 1 3
|
||||
[ -c /dev/kmsg ] || mknod -m 660 /dev/kmsg c 1 11
|
||||
[ -e /dev/fd ] || ln -snf /proc/self/fd /dev/fd
|
||||
[ -e /dev/stdin ] || ln -snf /proc/self/fd/0 /dev/stdin
|
||||
[ -e /dev/stdout ] || ln -snf /proc/self/fd/1 /dev/stdout
|
||||
[ -e /dev/stderr ] || ln -snf /proc/self/fd/2 /dev/stderr
|
||||
[ -e /proc/kcore ] && ln -snf /proc/kcore /dev/core
|
||||
|
||||
mkdir -p /dev/mqueue -m 1777
|
||||
mkdir -p /dev/pts -m 0755
|
||||
mkdir -p /dev/shm -m 1777
|
||||
|
||||
@SCRIPTDIR@/trymount.sh "/dev/mqueue" "mqueue" "noexec,nosuid,nodev"
|
||||
@SCRIPTDIR@/trymount.sh "/dev/pts" "devpts" "noexec,nosuid,gid=5,mode=0620"
|
||||
@SCRIPTDIR@/trymount.sh "/dev/shm" "tmpfs" "noexec,nosuid,nodev,mode=1777"
|
|
@ -1,89 +0,0 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#
|
||||
# Copyright (C) 2018 - David Oberhollenzer
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program 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 General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
CFGPATH="@ETCPATH@/netcfg"
|
||||
|
||||
[ -d "$CFGPATH" ] || exit 0
|
||||
|
||||
# configure interfaces
|
||||
for IFPATH in /sys/class/net/*; do
|
||||
[ "$IFPATH" == "/sys/class/net/lo" ] && continue
|
||||
|
||||
IF=`basename $IFPATH`
|
||||
CFGFILE="$CFGPATH/$IF"
|
||||
|
||||
[ -f "$CFGFILE" ] || continue
|
||||
|
||||
ip link set dev "$IF" down
|
||||
|
||||
while read LINE;
|
||||
do
|
||||
trimmed=`echo -- $LINE`
|
||||
[ ! -z "$trimmed" ] || continue
|
||||
set $trimmed
|
||||
|
||||
case "$1" in
|
||||
address|addr|ip|ip6|ipv6)
|
||||
shift
|
||||
ip address add $@ dev "$IF"
|
||||
;;
|
||||
arp|multicast|mtu)
|
||||
ip link set dev "$IF" $@
|
||||
;;
|
||||
offload)
|
||||
shift
|
||||
ethtool -K "$IF" $@
|
||||
;;
|
||||
*)
|
||||
;;
|
||||
esac
|
||||
done < "$CFGFILE"
|
||||
done
|
||||
|
||||
# configure static routs
|
||||
if [ -f "$CFGPATH/routes" ]; then
|
||||
while read LINE;
|
||||
do
|
||||
trimmed=`echo -- $LINE`
|
||||
[ ! -z "$trimmed" ] || continue
|
||||
set $trimmed
|
||||
|
||||
case "$1" in
|
||||
route)
|
||||
shift
|
||||
ip route add $@
|
||||
;;
|
||||
rule)
|
||||
shift
|
||||
ip rule add $@
|
||||
;;
|
||||
*)
|
||||
;;
|
||||
esac
|
||||
done < "$CFGFILE"
|
||||
fi
|
||||
|
||||
# activate interfaces
|
||||
for IFPATH in /sys/class/net/*; do
|
||||
[ "$IFPATH" == "/sys/class/net/lo" ] && continue
|
||||
|
||||
IF=`basename $IFPATH`
|
||||
|
||||
[ ! -f "$CFGPATH/$IF" ] || ip link set dev "$IF" up
|
||||
done
|
|
@ -1,26 +0,0 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#
|
||||
# Copyright (C) 2018 - David Oberhollenzer
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program 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 General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
for IFPATH in /sys/class/net/*; do
|
||||
[ "$IFPATH" == "/sys/class/net/lo" ] && continue
|
||||
|
||||
IF=`basename $IFPATH`
|
||||
|
||||
ip link set dev "$IF" down
|
||||
done
|
|
@ -1,65 +0,0 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#
|
||||
# Copyright (C) 2018 - David Oberhollenzer
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program 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 General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
NAMERULES="@ETCPATH@/netcfg/ifrename"
|
||||
TMPPATH="/tmp/ifrename"
|
||||
|
||||
[ -f "$NAMERULES" ] || exit 0
|
||||
|
||||
mkdir -p "$TMPPATH"
|
||||
|
||||
for IFPATH in /sys/class/net/*; do
|
||||
[ "$IFPATH" == "/sys/class/net/lo" ] && continue
|
||||
|
||||
IF=`basename $IFPATH`
|
||||
MAC=`cat $IFPATH/address`
|
||||
|
||||
grep "^[^,]\+,[^,]\+,[a-zA-Z0-9]\+$" $NAMERULES | while read LINE;
|
||||
do
|
||||
NAMECMP=$(echo $LINE | cut -d',' -f1)
|
||||
ADDRCMP=$(echo $LINE | cut -d',' -f2)
|
||||
RULE=$(echo $LINE | cut -d',' -f3)
|
||||
|
||||
case $IF in ($NAMECMP) ;; *) continue;; esac
|
||||
case $MAC in ($ADDRCMP) ;; *) continue;; esac
|
||||
|
||||
echo "$MAC,$IF" >> "$TMPPATH/$RULE"
|
||||
break
|
||||
done
|
||||
done
|
||||
|
||||
for FNAME in $TMPPATH/*; do
|
||||
[ ! -f "$FNAME" ] && break
|
||||
|
||||
IDX=0
|
||||
PREFIX=$(basename $FNAME)
|
||||
|
||||
sort -t',' -k1 -u $FNAME | while read LINE;
|
||||
do
|
||||
OLDNAME=$(echo $LINE | cut -d',' -f2)
|
||||
NEWNAME="$PREFIX$IDX"
|
||||
IDX=`expr $IDX + 1`
|
||||
|
||||
ip link set "$OLDNAME" name "$NEWNAME"
|
||||
done
|
||||
|
||||
rm "$FNAME"
|
||||
done
|
||||
|
||||
rmdir "$TMPPATH"
|
|
@ -1,39 +0,0 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#
|
||||
# Copyright (C) 2018 - David Oberhollenzer
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program 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 General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
MODLIST="@ETCPATH@/modules"
|
||||
|
||||
if [ ! -f "$MODLIST" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
while read LINE;
|
||||
do
|
||||
trimmed=`echo -- $LINE`
|
||||
[ ! -z "$trimmed" ] || continue
|
||||
set $trimmed
|
||||
|
||||
case "$1" in
|
||||
\#*)
|
||||
;;
|
||||
*)
|
||||
modprobe "$1"
|
||||
;;
|
||||
esac
|
||||
done < "$MODLIST"
|
|
@ -1,7 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
if [ -d "$1" ]; then
|
||||
if grep -qsE "[[:space:]]+$2$" "/proc/filesystems"; then
|
||||
mount -n -t "$2" -o "$3" "$2" "$1"
|
||||
fi
|
||||
fi
|
|
@ -1,21 +0,0 @@
|
|||
initdir = @TEMPLATEDIR@
|
||||
init_DATA = services/agetty services/hostname services/loopback
|
||||
init_DATA += services/sysctl services/hwclock services/sysinit
|
||||
init_DATA += services/reboot services/shutdown services/sigkill
|
||||
init_DATA += services/sigterm services/sync services/devfs
|
||||
init_DATA += services/sysfs services/procfs services/tmpfs
|
||||
init_DATA += services/vfs services/ifrename services/ifcfg
|
||||
init_DATA += services/dhcpcd services/dhcpcdmaster services/unbound
|
||||
init_DATA += services/dnsmasq services/ifdown services/modules
|
||||
init_DATA += services/network services/hostapd services/swclock
|
||||
init_DATA += services/swclocksave services/nft
|
||||
|
||||
if USYSLOGD
|
||||
init_DATA += services/usyslogd
|
||||
endif
|
||||
|
||||
EXTRA_DIST += services/sysinit services/vfs services/agetty services/hostname
|
||||
EXTRA_DIST += services/hwclock services/loopback services/reboot
|
||||
EXTRA_DIST += services/shutdown services/sync services/sysctl services/tmpfs
|
||||
EXTRA_DIST += services/dhcpcd services/dhcpcdmaster services/unbound
|
||||
EXTRA_DIST += services/usyslogd services/dnsmasq services/network
|
|
@ -1,6 +0,0 @@
|
|||
description agetty on %0
|
||||
exec agetty %0 linux
|
||||
type respawn
|
||||
target boot
|
||||
after network
|
||||
tty "/dev/%0"
|
|
@ -1,6 +0,0 @@
|
|||
description "mount /dev"
|
||||
type wait
|
||||
target boot
|
||||
after procfs sysfs
|
||||
before vfs
|
||||
exec "@SCRIPTDIR@/devfs.sh"
|
|
@ -1,6 +0,0 @@
|
|||
description "DHCP client on %0"
|
||||
type once
|
||||
target boot
|
||||
after dhcpcdmaster network
|
||||
|
||||
exec dhcpcd -n %0
|
|
@ -1,9 +0,0 @@
|
|||
description "DHCP client - master service"
|
||||
type wait
|
||||
target boot
|
||||
after network
|
||||
|
||||
exec {
|
||||
mkdir -p /var/db/dhcpcd
|
||||
dhcpcd --inactive
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
description "dnsmasq DNS & DHCP server"
|
||||
type respawn limit 5
|
||||
target boot
|
||||
after network unbound
|
||||
|
||||
exec dnsmasq -k
|
|
@ -1,7 +0,0 @@
|
|||
description "WIFI access point daemon"
|
||||
type respawn limit 10
|
||||
target boot
|
||||
after sysinit ifrename
|
||||
before network ifcfg
|
||||
|
||||
exec hostapd "@ETCPATH@/hostapd.conf"
|
|
@ -1,6 +0,0 @@
|
|||
description reload hostname
|
||||
exec hostname --file /etc/hostname
|
||||
type wait
|
||||
target boot
|
||||
before sysinit
|
||||
after hwclock vfs
|
|
@ -1,6 +0,0 @@
|
|||
description restore time from RTC
|
||||
exec hwclock --hctosys --utc
|
||||
type wait
|
||||
target boot
|
||||
before sysinit
|
||||
after vfs modules
|
|
@ -1,7 +0,0 @@
|
|||
description "static network configuration"
|
||||
type wait
|
||||
target boot
|
||||
after sysinit ifrename
|
||||
before network
|
||||
|
||||
exec "@SCRIPTDIR@/ifcfg.sh"
|
|
@ -1,7 +0,0 @@
|
|||
description "stop all network interfaces"
|
||||
type wait
|
||||
target %0
|
||||
after sigkill sigterm
|
||||
before sync
|
||||
|
||||
exec "@SCRIPTDIR@/ifdown.sh"
|
|
@ -1,7 +0,0 @@
|
|||
description "rename network interfaces"
|
||||
type wait
|
||||
target boot
|
||||
after sysinit
|
||||
before network
|
||||
|
||||
exec "@SCRIPTDIR@/ifrename.sh"
|
|
@ -1,10 +0,0 @@
|
|||
description configure network loopback device
|
||||
type wait
|
||||
target boot
|
||||
before sysinit
|
||||
after hostname vfs
|
||||
|
||||
exec {
|
||||
ip addr add 127.0.0.1/8 dev lo brd +
|
||||
ip link set lo up
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
description "load kernel modules"
|
||||
type wait
|
||||
target boot
|
||||
after vfs usyslogd
|
||||
before sysinit
|
||||
|
||||
exec "@SCRIPTDIR@/modules_load.sh"
|
|
@ -1,4 +0,0 @@
|
|||
description "static network configuration completed"
|
||||
type wait
|
||||
target boot
|
||||
after sysinit
|
|
@ -1,7 +0,0 @@
|
|||
description "restore netfilter rule set"
|
||||
type wait
|
||||
target boot
|
||||
after sysinit ifrename
|
||||
before network ifcfg
|
||||
|
||||
exec nft -f "@ETCPATH@/nftables.rules"
|
|
@ -1,9 +0,0 @@
|
|||
description "mount /proc"
|
||||
type wait
|
||||
target boot
|
||||
before vfs
|
||||
|
||||
exec {
|
||||
mount -t proc proc /proc
|
||||
"@SCRIPTDIR@/trymount.sh" /proc/sys/fs/binfmt_misc binfmt_misc nodev,noexec,nosuid
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
description system reboot
|
||||
exec shutdown -nrf
|
||||
type wait
|
||||
target reboot
|
||||
after sync sigkill sigterm
|
|
@ -1,5 +0,0 @@
|
|||
description system shutdown
|
||||
exec shutdown -npf
|
||||
type wait
|
||||
target shutdown
|
||||
after sync sigkill sigterm
|
|
@ -1,6 +0,0 @@
|
|||
description send SIGKILL to remaining processes
|
||||
exec "@SCRIPTDIR@/killall5" 9
|
||||
type wait
|
||||
target %0
|
||||
after sigterm
|
||||
before sync shutdown reboot
|
|
@ -1,9 +0,0 @@
|
|||
description send SIGTERM to all processes
|
||||
type wait
|
||||
target %0
|
||||
before sigkill sync reboot shutdown
|
||||
|
||||
exec {
|
||||
"@SCRIPTDIR@/killall5" 15
|
||||
sleep 5
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
description restore saved time from last shutdown
|
||||
type wait
|
||||
target boot
|
||||
before sysinit
|
||||
after vfs modules
|
||||
exec xargs -a @STATEFILESPATH@/swclock date --utc
|
|
@ -1,7 +0,0 @@
|
|||
description write current time to backup file
|
||||
type wait
|
||||
target %0
|
||||
after sigkill
|
||||
before sync
|
||||
tty truncate @STATEFILESPATH@/swclock
|
||||
exec date --utc +%%m%%d%%H%%M%%Y.%%S
|
|
@ -1,6 +0,0 @@
|
|||
description sync
|
||||
exec sync
|
||||
type wait
|
||||
target %0
|
||||
after sigkill sigterm
|
||||
before reboot shutdown
|
|
@ -1,6 +0,0 @@
|
|||
description configure kernel paramters
|
||||
exec sysctl --system
|
||||
type wait
|
||||
target boot
|
||||
before sysinit
|
||||
after vfs loopback
|
|
@ -1,13 +0,0 @@
|
|||
description "mount /sys"
|
||||
type wait
|
||||
target boot
|
||||
after procfs
|
||||
before vfs
|
||||
|
||||
exec {
|
||||
mount -t sysfs sysfs /sys
|
||||
"@SCRIPTDIR@/trymount.sh" /sys/kernel/security securityfs nodev,noexec,nosuid
|
||||
"@SCRIPTDIR@/trymount.sh" /sys/kernel/config configfs nodev,noexec,nosuid
|
||||
"@SCRIPTDIR@/trymount.sh" /sys/fs/fuse/connections fusectl nodev,noexec,nosuid
|
||||
"@SCRIPTDIR@/trymount.sh" /sys/firmware/efi/efivars efivarfs ro
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue