1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-05-18 19:56:14 +02:00

Compare commits

...

48 commits
v0.8 ... master

Author SHA1 Message Date
David Oberhollenzer 5307b95b93 Cleanup: remove flag mechanism from config parser entirely
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2020-04-24 12:28:06 +02:00
David Oberhollenzer 70ea16b0b4 Cleanup: remove rdsvc flags
With the previous changes, there were only used by the status
command.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2020-04-24 12:26:26 +02:00
David Oberhollenzer 5f28289731 cleanup: merge runsvc back into initd
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2020-04-24 12:09:18 +02:00
David Oberhollenzer 0975ed0fb7 runsvc: make sure we close all fds before running a service
Just in case initd leaks anything. Also, the service has no
buisness writing all over /dev/console. It's a system service, it
better use syslog or its own internal logging service.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2020-04-06 15:55:47 +02:00
David Oberhollenzer 9b43890591 cleanup: simplify runsvc environment config parsing
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2020-04-06 15:44:15 +02:00
David Oberhollenzer 87a524d931 cleanup: delete remains of libutil
- exec_t belongs to service.h, the main place where it is used/needed
 - code for executing exec_t is moved to runsvc for the same reason
 - what is left are NORETURN and ARRAY_SIZE
   - the former can be replaced with direct attribute usage since
     the only relevant compilers all support the attribute.
   - the later is only used in 3 places and can be trivially replaced
     with direct usage of sizeof().

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2020-03-31 18:19:27 +02:00
David Oberhollenzer 9f9807d4d3 cleanup: initd: simplify and merge linux specific code into main.c
Targetting anything else than Linux isn't really relevant. All
other systems ($BSD and other Unices) are a closed ecosystem
where kernel & userspace are developed together. They don't need
something like a third party init system, so compatibillity can
be largely ignored.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2020-03-31 13:09:04 +02:00
David Oberhollenzer 0d985a7430 Add RDSVC_NO_DESC flag
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-06-16 20:51:23 +02:00
David Oberhollenzer 60efd9dc33 Remove unused SOCK_FLAG_* enum
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-06-16 18:20:26 +02:00
David Oberhollenzer 1c72dd2c2f Fix remove by id
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-06-16 16:09:49 +02:00
David Oberhollenzer 2e89d32a75 Release init-0.10
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-29 21:00:53 +01:00
David Oberhollenzer c1cb8491f9 fix: actually remove started service from list
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-29 21:00:53 +01:00
David Oberhollenzer c8c0f10ce1 Keep original respawn limit
We also want this meachanism to still work for manually started
service (especially after reloading services).

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-29 21:00:53 +01:00
David Oberhollenzer be06641904 cleanup: init socket wire format
Replace array adhockery with structs and make use of the handy
endianness conversion macros.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-29 21:00:53 +01:00
David Oberhollenzer c3d14cbfa8 cleanup: init status response
- rename init_status_response_t to init_status_t
 - merge code for handling it
 - fix memory leak in status command

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-29 21:00:53 +01:00
David Oberhollenzer 7cfe6e8458 cleanup: move init specific stuff of init socket to initd
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-29 21:00:53 +01:00
David Oberhollenzer f844c4e2c2 Update documentation
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-29 21:00:53 +01:00
David Oberhollenzer 028394b8a5 Add service configuration reloading
This commit add the ability to initd to reload the service configuration
while running. The new configuration is merged with the existing one as
follows:

For each target:
 - If the existing service list is not NULL, we have not started that
   target yet. Simply replace it with the new list.
 - If it is NULL, the services have already been started.
    - First, remove all entries for services in that target that no
      loner exist (except from the 'running' list).
    - Second, add new services that we don't have yet. Treat them as
      recently diseased and let the user start them manualy.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-29 21:00:53 +01:00
David Oberhollenzer 6fa0393be4 Add start/stop commands to service tool
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-28 15:32:10 +01:00
David Oberhollenzer ba12700080 Add start/stop commands to init socket
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-28 15:32:10 +01:00
David Oberhollenzer 9e7478397a Include service ID in initsock status response
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-28 15:19:06 +01:00
David Oberhollenzer c16735414b initd: Hand out unique IDs to services
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-28 13:45:33 +01:00
David Oberhollenzer d16d260181 Add filter argument to status request
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-27 17:48:32 +01:00
David Oberhollenzer affe9e4b88 Use fixed size integer for init socket request
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-27 17:48:32 +01:00
David Oberhollenzer 72c02308cd Fix: actually process SIGUSR1 in initd
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-25 16:32:45 +01:00
David Oberhollenzer 9fece2eb88 Add hacky utiliy that waits for the presence of some files
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-24 18:02:17 +01:00
David Oberhollenzer ec04681c4d Make service status matching fuzzier (also allow service name match)
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-21 17:11:52 +01:00
David Oberhollenzer a6c059203b Fix warnings from gcc 8.3 + musl build
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-20 20:43:41 +01:00
David Oberhollenzer e21840cfce initd: don't start runsvc for services without exec block
First in rdsvc, tag the services that *do* have exec lines, even if we don't
read them.

Second, if a service does not have that flag set, don't try to execute it.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-20 15:44:14 +01:00
David Oberhollenzer 390175c406 cleanup runsvc: merge codepaths for execution, remove cleanup code
Simply execute the last entry in the list directly instead of forking and
remove the cleanup code.

If the list is empty, we return success.

If the list only has one entry, we directly execute that. No need to make a
distinction between single entry vs list anymore.

If the list is an actual list, we run it as before but execute the last one
directly. Typically, the last one is something like a daemon preceeded by
setup code. The daemon ends up directly underneath init, without a dummy
waiting runsvc stuck in the process list.

If we always do an exec, there is no point in doing cleanup. All our mapped
memory is evicted anyway. Same if we exit appruptly because of an error.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-20 15:09:35 +01:00
David Oberhollenzer 4f1b393cee Dump more information in service status command
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-20 14:47:45 +01:00
David Oberhollenzer 1850f31d6d Seperate service loading/error loging from dumpscript command
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-19 23:53:49 +01:00
David Oberhollenzer 065d3b678d cleanup: remove broken stat ... open pattern
confused deputy is confused.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-18 19:49:42 +01:00
David Oberhollenzer 11053ebe6a Add filtering parameters to status command
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-18 19:16:34 +01:00
David Oberhollenzer a9602ad6e0 Cleanup status reporting
- mimic format of initd
 - skip formatting if not a tty
 - distinguish exited because failed vs exited because done

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-18 18:47:20 +01:00
David Oberhollenzer 3f40c4d3ed Add status report command to service command line utility
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-18 18:42:38 +01:00
David Oberhollenzer 40ad83dc6a initd: implement handling of socket requests
Actually process requests and send an answer to status inquiries.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-18 18:29:02 +01:00
David Oberhollenzer 23b713c3b5 Add functions to transmit service status accross initd socket
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-18 18:29:02 +01:00
David Oberhollenzer 08f72865b2 Add init socket to initd
Create a socket if boot target is done. Close and reopen socket
if SIGUSR1 is received.

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-18 14:15:58 +01:00
David Oberhollenzer c78bbd2f73 Add helpers for initd socket
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-18 14:15:58 +01:00
David Oberhollenzer c544fcc7a3 initd: store completed services in list instead of deleting them
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-15 10:46:11 +01:00
David Oberhollenzer 5b106abaed Update e-mail address
Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
2019-03-14 17:18:34 +01:00
David Oberhollenzer 4ee949c1ca Release init-0.9
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
2018-11-25 02:08:33 +01:00
David Oberhollenzer d9a5736bdf Remove some no longer needed cruft
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
2018-11-23 23:00:58 +01:00
David Oberhollenzer c14c3c0173 Relicense what is left of the init system under OpenBSD style ISC license
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
2018-11-23 13:16:08 +01:00
David Oberhollenzer d983a282ed Remove auto-magic release automation script
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
2018-11-23 13:14:55 +01:00
David Oberhollenzer bf878d08dd Remove cron daemon, rewritten and split off into seperate repo
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
2018-11-21 12:01:39 +01:00
David Oberhollenzer dc6358b0e1 rdline: always do substitution of '%%'
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
2018-11-13 19:11:33 +01:00
70 changed files with 1363 additions and 3268 deletions

1
.gitignore vendored
View file

@ -21,5 +21,6 @@ shutdown
killall5
runsvc
gcrond
waitfile
etc/initd.env

687
LICENSE
View file

@ -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.

View file

@ -21,7 +21,6 @@ dist_man8_MANS =
include lib/Makemodule.am
include cmd/Makemodule.am
include initd/Makemodule.am
include crond/Makemodule.am
install-exec-hook:
(cd $(DESTDIR)$(sbindir); $(LN_S) shutdown reboot)
@ -31,6 +30,3 @@ install-data-local:
(cd $(DESTDIR)$(man8dir); $(LN_S) shutdown.8 reboot.8)
$(MKDIR_P) $(DESTDIR)$(SVCDIR)
$(MKDIR_P) $(DESTDIR)$(TEMPLATEDIR)
if GCROND
$(MKDIR_P) $(DESTDIR)$(GCRONDIR)
endif

View file

@ -1,9 +1,8 @@
# About
This directory contains the source code for a tiny service supervision
framework devised for the Pygos system, consisting of an init daemon,
a _definitely_ non standards compliant cron implementation and various
command line utilities.
framework devised for the Pygos system, consisting of an init daemon and
accompanying command line utilities.
The programs of this package are developed first and foremost for GNU/Linux
@ -46,8 +45,6 @@ command line tools.
See [docs/services.md](docs/services.md) for more information on service
description files.
See [docs/gcron.md](docs/gcron.md) for details on the cron implementation.
## Why

View file

@ -2,38 +2,33 @@ shutdown_SOURCES = cmd/shutdown.c
shutdown_CPPFLAGS = $(AM_CPPFLAGS)
shutdown_CFLAGS = $(AM_CFLAGS)
shutdown_LDFLAGS = $(AM_LDFLAGS)
shutdown_LDADD = libutil.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 libutil.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 libutil.a
if GCROND
service_SOURCES += cmd/service/schedule.c
service_SOURCES += cmd/service/unschedule.c
endif
service_LDADD = libinit.a libcfg.a
dist_man8_MANS += cmd/shutdown.8 cmd/service/service.8
EXTRA_DIST += $(SRVHEADERS)
sbin_PROGRAMS += service shutdown
helper_PROGRAMS += killall5 runsvc
helper_PROGRAMS += killall5 waitfile

View file

@ -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);

View file

@ -1,125 +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->line;
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->line) + 1);
if (e == NULL)
goto fail_oom;
strcpy(e->data, rd->line);
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;
if (rdline_init(&rd, AT_FDCWD, ENVFILE, 0, NULL))
return NULL;
list = parse_list(&rd);
rdline_cleanup(&rd);
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;
}

View file

@ -1,96 +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 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->ctty, (svc->flags & SVC_FLAG_TRUNCATE_OUT) != 0))
goto out;
if (svc->exec->next == NULL)
argv_exec(svc->exec);
ret = runlst_wait(svc->exec);
out:
delsvc(svc);
return ret;
}

View file

@ -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 */

View file

@ -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);

View file

@ -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');
}

View file

@ -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);

View file

@ -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;

View file

@ -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
View 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;
}

View file

@ -1,90 +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/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "servicecmd.h"
static int cmd_schedule(int argc, char **argv)
{
char *target, *linkname, *ptr;
int ret = EXIT_FAILURE;
struct stat sb;
if (check_arguments(argv[0], argc, 2, 2))
return EXIT_FAILURE;
for (ptr = argv[1]; isalnum(*ptr) || *ptr == '_'; ++ptr)
;
if (*ptr != '\0') {
fprintf(stderr, "Invalid service name '%s'\n", argv[1]);
tell_read_help(argv[0]);
return EXIT_FAILURE;
}
if (asprintf(&target, "%s/%s.gcron", TEMPLATEDIR, argv[1]) < 0) {
perror("asprintf");
return EXIT_FAILURE;
}
if (stat(target, &sb)) {
fprintf(stderr, "%s: %s\n", target, strerror(errno));
goto out_tgt;
}
if ((sb.st_mode & S_IFMT) != S_IFREG) {
fprintf(stderr, "%s: must be a regular file\n", target);
goto out_tgt;
}
if (asprintf(&linkname, "%s/%s", GCRONDIR, argv[1]) < 0) {
perror("asprintf");
goto out_tgt;
}
if (symlink(target, linkname)) {
fprintf(stderr, "creating symlink '%s' -> '%s: %s\n",
linkname, target, strerror(errno));
goto out;
}
ret = EXIT_SUCCESS;
out:
free(linkname);
out_tgt:
free(target);
return ret;
}
static command_t schedule = {
.cmd = "schedule",
.usage = "<name>",
.s_desc = "enable a gcrond service",
.l_desc = "This marks a gcrond service as enabled by creating a "
"symlink in " GCRONDIR " pointing to a template file in "
TEMPLATEDIR " with a .gcron extension.",
.run_cmd = cmd_schedule,
};
REGISTER_COMMAND(schedule)

View file

@ -28,12 +28,6 @@ configuration directory, pointing to the service template file.
An optional argument can be supplied to parameterize the template.
.TP
.BR schedule " " \fI<service>\fP
If built with support for gcrond, enable a gcron service by creating a symlink
in the gcrond configuration directory, pointing to the service file.
The extension \fB.gcron\fP is automatically appended to the service name.
.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.
@ -41,19 +35,26 @@ symlink in the configuration directory.
If the service is parameterized, arguments have to be specified to disable
the desired service instance.
.TP
.BR unschedule " " \fI<service>\fP
If built with support for gcrond, disable a gcron service by removing the
corresponding symlink in the gcron configuration directory.
.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: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
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.

View file

@ -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;

View file

@ -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
View 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
View 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)

View file

@ -1,82 +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/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "servicecmd.h"
static int cmd_unschedule(int argc, char **argv)
{
int ret = EXIT_FAILURE;
char *linkname, *ptr;
struct stat sb;
if (check_arguments(argv[0], argc, 2, 2))
return EXIT_FAILURE;
for (ptr = argv[1]; isalnum(*ptr) || *ptr == '_'; ++ptr)
;
if (*ptr != '\0') {
fprintf(stderr, "Invalid service name '%s'\n", argv[1]);
tell_read_help(argv[0]);
return EXIT_FAILURE;
}
if (asprintf(&linkname, "%s/%s.gcron", GCRONDIR, argv[1]) < 0) {
perror("asprintf");
return EXIT_FAILURE;
}
if (lstat(linkname, &sb)) {
fprintf(stderr, "lstat %s: %s\n", linkname, strerror(errno));
goto out;
}
if ((sb.st_mode & S_IFMT) != S_IFLNK) {
fprintf(stderr, "error: '%s' is not a symlink!", linkname);
goto out;
}
if (unlink(linkname)) {
fprintf(stderr, "removing %s: %s\n",
linkname, strerror(errno));
goto out;
}
ret = EXIT_SUCCESS;
out:
free(linkname);
return ret;
}
static command_t unschedule = {
.cmd = "unschedule",
.usage = "<name>",
.s_desc = "disable a gcrond service",
.l_desc = "This disables a gcrond service by removing the coresponding "
"symlink in " GCRONDIR ".",
.run_cmd = cmd_unschedule,
};
REGISTER_COMMAND(unschedule)

View file

@ -39,7 +39,7 @@ This program is part of the Pygos init system.
.SH COPYRIGHT
Copyright \(co 2018 David Oberhollenzer
.br
License: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
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.

View file

@ -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 <getopt.h>
#include <stdlib.h>
#include <string.h>
@ -26,14 +10,11 @@
#include <sys/reboot.h>
#include <linux/reboot.h>
#include "util.h"
#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' },
@ -41,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 = RB_POWER_OFF;
static NORETURN void usage(const char *progname, int status)
static __attribute__((noreturn)) void usage(const char *progname, int status)
{
fprintf(status == EXIT_SUCCESS ? stdout : stderr,
"%s [OPTIONS...]\n\n"
@ -94,8 +75,6 @@ int main(int argc, char **argv)
case 'r':
defact = RB_AUTOBOOT;
break;
case 'V':
print_version(ptr);
case 'h':
usage(ptr, EXIT_SUCCESS);
default:

82
cmd/waitfile.c Normal file
View 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;
}

View file

@ -1,8 +1,8 @@
AC_PREREQ([2.60])
m4_define([RELEASE], 0.8)
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,20 +36,8 @@ UL_WARN_ADD([-pedantic])
AC_SUBST([WARN_CFLAGS])
AC_ARG_WITH([gcrond],
[AS_HELP_STRING([--without-gcrond], [Build without gcron daemon])],
[case "${withval}" in
yes) AM_CONDITIONAL([GCROND], [true]) ;;
no) AM_CONDITIONAL([GCROND], [false]) ;;
*) AC_MSG_ERROR([bad value ${withval} for --without-gcron]) ;;
esac],
[AM_CONDITIONAL([GCROND], [true])])
AC_CONFIG_HEADERS([lib/include/config.h])
AC_DEFINE_DIR(SVCDIR, sysconfdir/init.d, [Startup service directory])
AC_DEFINE_DIR(GCRONDIR, sysconfdir/gcron.d, [Cron service directory])
AC_DEFINE_DIR(TEMPLATEDIR, datadir/init, [Service template directory])
AC_DEFINE_DIR(SCRIPTDIR, libexecdir/init, [Helper script directory])
AC_DEFINE_DIR(SOCKDIR, localstatedir/run, [Directory for initd socket])

View file

@ -1,11 +0,0 @@
if GCROND
gcrond_SOURCES = crond/main.c crond/gcrond.h crond/runjob.c
gcrond_SOURCES += crond/rdcron.c crond/delcron.c crond/crontab.c
gcrond_SOURCES += crond/cronscan.c crond/crontab.h
gcrond_CPPFLAGS = $(AM_CPPFLAGS)
gcrond_CFLAGS = $(AM_CFLAGS)
gcrond_LDFLAGS = $(AM_LDFLAGS)
gcrond_LDADD = libcfg.a libutil.a
sbin_PROGRAMS += gcrond
endif

View file

@ -1,77 +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/stat.h>
#include <stddef.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include "crontab.h"
int cronscan(const char *directory, crontab_t **list)
{
struct dirent *ent;
int dfd, ret = 0;
crontab_t *cron;
DIR *dir;
dir = opendir(directory);
if (dir == NULL) {
perror(directory);
return -1;
}
dfd = dirfd(dir);
if (dfd < 0) {
perror(directory);
closedir(dir);
return -1;
}
for (;;) {
errno = 0;
ent = readdir(dir);
if (ent == NULL) {
if (errno != 0) {
perror(directory);
ret = -1;
}
break;
}
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
continue;
cron = rdcron(dfd, ent->d_name);
if (cron == NULL) {
ret = -1;
continue;
}
cron->next = *list;
*list = cron;
}
closedir(dir);
return ret;
}

View file

@ -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 <string.h>
#include "crontab.h"
void cron_tm_to_mask(crontab_t *out, struct tm *t)
{
memset(out, 0, sizeof(*out));
out->minute = 1UL << ((unsigned long)t->tm_min);
out->hour = 1 << t->tm_hour;
out->dayofmonth = 1 << (t->tm_mday - 1);
out->month = 1 << t->tm_mon;
out->dayofweek = 1 << t->tm_wday;
}
bool cron_should_run(const crontab_t *t, const crontab_t *mask)
{
if ((t->minute & mask->minute) == 0)
return false;
if ((t->hour & mask->hour) == 0)
return false;
if ((t->dayofmonth & mask->dayofmonth) == 0)
return false;
if ((t->month & mask->month) == 0)
return false;
if ((t->dayofweek & mask->dayofweek) == 0)
return false;
return true;
}

View file

@ -1,55 +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 CRONTAB_H
#define CRONTAB_H
#include <sys/types.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include "service.h"
typedef struct crontab_t {
struct crontab_t *next;
exec_t *exec;
char *ctty;
uid_t uid;
gid_t gid;
uint64_t minute;
uint32_t hour;
uint32_t dayofmonth;
uint16_t month;
uint8_t dayofweek;
unsigned int tty_truncate : 1;
} crontab_t;
crontab_t *rdcron(int dirfd, const char *filename);
void delcron(crontab_t *cron);
int cronscan(const char *directory, crontab_t **list);
void cron_tm_to_mask(crontab_t *out, struct tm *t);
bool cron_should_run(const crontab_t *t, const crontab_t *mask);
#endif /* CRONTAB_H */

View file

@ -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/>.
*/
#include <stdlib.h>
#include "crontab.h"
void delcron(crontab_t *cron)
{
exec_t *e;
if (cron == NULL)
return;
while (cron->exec != NULL) {
e = cron->exec;
cron->exec = e->next;
free(e);
}
free(cron->ctty);
free(cron);
}

View file

@ -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 GCROND_H
#define GCROND_H
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include "crontab.h"
#include "config.h"
#include "util.h"
int runjob(crontab_t *tab);
#endif /* GCROND_H */

View file

@ -1,135 +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 "gcrond.h"
static crontab_t *jobs;
static sig_atomic_t run = 1;
static sig_atomic_t rescan = 1;
static void read_config(void)
{
if (cronscan(GCRONDIR, &jobs)) {
fputs("Error reading configuration. Continuing anyway.\n",
stderr);
}
}
static void cleanup_config(void)
{
crontab_t *t;
while (jobs != NULL) {
t = jobs;
jobs = jobs->next;
delcron(t);
}
}
static int calc_timeout(void)
{
time_t now = time(NULL), future;
struct tm tmstruct;
crontab_t mask, *t;
int minutes;
for (minutes = 0; minutes < 120; ++minutes) {
future = now + minutes * 60;
localtime_r(&future, &tmstruct);
cron_tm_to_mask(&mask, &tmstruct);
for (t = jobs; t != NULL; t = t->next) {
if (cron_should_run(t, &mask))
goto out;
}
}
out:
return minutes ? minutes * 60 : 60;
}
static void runjobs(void)
{
time_t now = time(NULL);
struct tm tmstruct;
crontab_t mask, *t;
localtime_r(&now, &tmstruct);
cron_tm_to_mask(&mask, &tmstruct);
for (t = jobs; t != NULL; t = t->next) {
if (cron_should_run(t, &mask))
runjob(t);
}
}
static void sighandler(int signo)
{
pid_t pid;
switch (signo) {
case SIGINT:
case SIGTERM:
run = 0;
break;
case SIGHUP:
rescan = 1;
break;
case SIGCHLD:
while ((pid = waitpid(-1, NULL, WNOHANG)) != -1)
;
break;
}
}
int main(void)
{
struct timespec stime;
struct sigaction act;
int timeout;
memset(&act, 0, sizeof(act));
act.sa_handler = sighandler;
sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
sigaction(SIGHUP, &act, NULL);
sigaction(SIGCHLD, &act, NULL);
while (run) {
if (rescan == 1) {
cleanup_config();
read_config();
timeout = 60;
rescan = 0;
} else {
runjobs();
timeout = calc_timeout();
}
stime.tv_sec = timeout;
stime.tv_nsec = 0;
while (nanosleep(&stime, &stime) != 0 && run && !rescan) {
if (errno != EINTR) {
perror("nanosleep");
break;
}
}
}
return EXIT_SUCCESS;
}

View file

@ -1,503 +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/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <pwd.h>
#include <grp.h>
#include "crontab.h"
#include "libcfg.h"
#include "util.h"
static const enum_map_t weekday[] = {
{ "MON", 1 },
{ "TUE", 2 },
{ "WED", 3 },
{ "THU", 4 },
{ "FRI", 5 },
{ "SAT", 6 },
{ "SUN", 0 },
};
static const enum_map_t month[] = {
{ "JAN", 1 },
{ "FEB", 2 },
{ "MAR", 3 },
{ "APR", 4 },
{ "MAY", 5 },
{ "JUN", 6 },
{ "JUL", 7 },
{ "AUG", 8 },
{ "SEP", 9 },
{ "OCT", 10 },
{ "NOV", 11 },
{ "DEC", 12 },
};
static const struct {
const char *macro;
crontab_t tab;
} intervals[] = {
{
.macro = "yearly",
.tab = {
.minute = 0x01,
.hour = 0x01,
.dayofmonth = 0x01,
.month = 0x01,
.dayofweek = 0xFF
},
}, {
.macro = "annually",
.tab = {
.minute = 0x01,
.hour = 0x01,
.dayofmonth = 0x01,
.month = 0x01,
.dayofweek = 0xFF
},
}, {
.macro = "monthly",
.tab = {
.minute = 0x01,
.hour = 0x01,
.dayofmonth = 0x01,
.month = 0xFFFF,
.dayofweek = 0xFF
},
}, {
.macro = "weekly",
.tab = {
.minute = 0x01,
.hour = 0x01,
.dayofmonth = 0xFFFFFFFF,
.month = 0xFFFF,
.dayofweek = 0x01
},
}, {
.macro = "daily",
.tab = {
.minute = 0x01,
.hour = 0x01,
.dayofmonth = 0xFFFFFFFF,
.month = 0xFFFF,
.dayofweek = 0xFF
},
}, {
.macro = "hourly",
.tab = {
.minute = 0x01,
.hour = 0xFFFFFFFF,
.dayofmonth = 0xFFFFFFFF,
.month = 0xFFFF,
.dayofweek = 0xFF
},
},
};
/*****************************************************************************/
static int try_unescape(char *arg, rdline_t *rd)
{
if (unescape(arg)) {
fprintf(stderr, "%s: %zu: malformed string constant\n",
rd->filename, rd->lineno);
return -1;
}
return 0;
}
static char *try_strdup(const char *str, rdline_t *rd)
{
char *out = strdup(str);
if (out == NULL) {
fprintf(stderr, "%s: %zu: out of memory\n",
rd->filename, rd->lineno);
}
return out;
}
static char *readnum(char *line, int *out, int minval, int maxval,
const enum_map_t *mnemonic, rdline_t *rd)
{
int i, temp, value = 0;
const enum_map_t *ev;
if (!isdigit(*line)) {
if (!mnemonic)
goto fail_mn;
for (i = 0; isalnum(line[i]); ++i)
;
if (i == 0)
goto fail_mn;
temp = line[i];
line[i] = '\0';
ev = enum_by_name(mnemonic, line);
if (!ev) {
fprintf(stderr, "%s: %zu: unexpected '%s'",
rd->filename, rd->lineno, line);
}
line[i] = temp;
if (!ev)
return NULL;
*out = ev->value;
return line + i;
}
while (isdigit(*line)) {
i = ((*(line++)) - '0');
if (value > (maxval - i) / 10)
goto fail_of;
value = value * 10 + i;
}
if (value < minval)
goto fail_uf;
*out = value;
return line;
fail_of:
fprintf(stderr, "%s: %zu: value exceeds maximum (%d > %d)\n",
rd->filename, rd->lineno, value, maxval);
return NULL;
fail_uf:
fprintf(stderr, "%s: %zu: value too small (%d < %d)\n",
rd->filename, rd->lineno, value, minval);
return NULL;
fail_mn:
fprintf(stderr, "%s: %zu: expected numeric value",
rd->filename, rd->lineno);
return NULL;
}
static char *readfield(char *line, uint64_t *out, int minval, int maxval,
const enum_map_t *mnemonic, rdline_t *rd)
{
int value, endvalue, step;
uint64_t v = 0;
next:
if (*line == '*') {
++line;
value = minval;
endvalue = maxval;
} else {
line = readnum(line, &value, minval, maxval, mnemonic, rd);
if (!line)
goto fail;
if (*line == '-') {
line = readnum(line + 1, &endvalue, minval, maxval,
mnemonic, rd);
if (!line)
goto fail;
} else {
endvalue = value;
}
}
if (endvalue < value)
goto fail;
if (*line == '/') {
line = readnum(line + 1, &step, 1, maxval + 1, NULL, rd);
if (!line)
goto fail;
} else {
step = 1;
}
while (value <= endvalue) {
v |= 1UL << (unsigned long)(value - minval);
value += step;
}
if (*line == ',' || *line == ' ') {
++line;
goto next;
}
if (*line != '\0')
goto fail;
*out = v;
return line;
fail:
fprintf(stderr, "%s: %zu: invalid time range expression\n",
rd->filename, rd->lineno);
return NULL;
}
/*****************************************************************************/
static int cron_exec(void *user, char *arg, rdline_t *rd, int flags)
{
crontab_t *cron = user;
exec_t *e, *end;
(void)flags;
e = calloc(1, sizeof(*e) + strlen(arg) + 1);
if (e == NULL) {
fprintf(stderr, "%s: %zu: out of memory\n",
rd->filename, rd->lineno);
return -1;
}
strcpy(e->args, arg);
e->argc = pack_argv(e->args);
if (e->argc < 0) {
fprintf(stderr, "%s: %zu: malformed string constant\n",
rd->filename, rd->lineno);
return -1;
}
if (cron->exec == NULL) {
cron->exec = e;
} else {
for (end = cron->exec; end->next != NULL; end = end->next)
;
end->next = e;
}
return 0;
}
static int cron_hour(void *user, char *arg, rdline_t *rd, int flags)
{
crontab_t *cron = user;
uint64_t value;
(void)flags;
if (!readfield(arg, &value, 0, 23, NULL, rd))
return -1;
cron->hour = value;
return 0;
}
static int cron_minute(void *user, char *arg, rdline_t *rd, int flags)
{
crontab_t *cron = user;
uint64_t value;
(void)flags;
if (!readfield(arg, &value, 0, 59, NULL, rd))
return -1;
cron->minute = value;
return 0;
}
static int cron_dayofmonth(void *user, char *arg, rdline_t *rd, int flags)
{
crontab_t *cron = user;
uint64_t value;
(void)flags;
if (!readfield(arg, &value, 1, 31, NULL, rd))
return -1;
cron->dayofmonth = value;
return 0;
}
static int cron_dayofweek(void *user, char *arg, rdline_t *rd, int flags)
{
crontab_t *cron = user;
uint64_t value;
(void)flags;
if (!readfield(arg, &value, 0, 6, weekday, rd))
return -1;
cron->dayofweek = value;
return 0;
}
static int cron_month(void *user, char *arg, rdline_t *rd, int flags)
{
crontab_t *cron = user;
uint64_t value;
(void)flags;
if (!readfield(arg, &value, 1, 12, month, rd))
return -1;
cron->month = value;
return 0;
}
static int cron_interval(void *user, char *arg, rdline_t *rd, int flags)
{
crontab_t *cron = user;
size_t i;
(void)flags;
for (i = 0; i < ARRAY_SIZE(intervals); ++i) {
if (!strcmp(intervals[i].macro, arg)) {
cron->minute = intervals[i].tab.minute;
cron->hour = intervals[i].tab.hour;
cron->dayofmonth = intervals[i].tab.dayofmonth;
cron->month = intervals[i].tab.month;
cron->dayofweek = intervals[i].tab.dayofweek;
return 0;
}
}
fprintf(stderr, "%s: %zu: unknown interval '%s'\n",
rd->filename, rd->lineno, arg);
return -1;
}
static int cron_user(void *user, char *arg, rdline_t *rd, int flags)
{
crontab_t *cron = user;
struct passwd *pwd;
bool isnumeric;
char *ptr;
int value;
(void)flags;
for (ptr = arg; isdigit(*ptr); ++ptr)
;
isnumeric = (*ptr == '\0');
pwd = getpwnam(arg);
if (pwd == NULL && !isnumeric) {
fprintf(stderr, "%s: %zu: unknown user '%s'\n",
rd->filename, rd->lineno, arg);
return -1;
}
if (pwd != NULL) {
cron->uid = pwd->pw_uid;
} else {
if (readnum(arg, &value, 0, INT_MAX, NULL, rd))
return -1;
cron->uid = value;
}
return 0;
}
static int cron_group(void *user, char *arg, rdline_t *rd, int flags)
{
crontab_t *cron = user;
struct group *group;
bool isnumeric;
char *ptr;
int value;
(void)flags;
for (ptr = arg; isdigit(*ptr); ++ptr)
;
isnumeric = (*ptr == '\0');
group = getgrnam(arg);
if (group == NULL && !isnumeric) {
fprintf(stderr, "%s: %zu: unknown group '%s'\n",
rd->filename, rd->lineno, arg);
return -1;
}
if (group != NULL) {
cron->gid = group->gr_gid;
} else {
if (readnum(arg, &value, 0, INT_MAX, NULL, rd))
return -1;
cron->gid = value;
}
return 0;
}
static int cron_tty(void *user, char *arg, rdline_t *rd, int flags)
{
crontab_t *cron = user;
(void)flags;
if (strncmp(arg, "truncate", 8) == 0 && isspace(arg[8])) {
cron->tty_truncate = 1;
arg += 8;
while (isspace(*arg))
++arg;
}
if (try_unescape(arg, rd))
return -1;
cron->ctty = try_strdup(arg, rd);
return cron->ctty == NULL ? -1 : 0;
}
static const cfg_param_t cron_params[] = {
{ "hour", 0, cron_hour },
{ "minute", 0, cron_minute },
{ "dayofmonth", 0, cron_dayofmonth },
{ "dayofweek", 0, cron_dayofweek },
{ "month", 0, cron_month },
{ "interval", 0, cron_interval },
{ "user", 0, cron_user },
{ "group", 0, cron_group },
{ "tty", 0, cron_tty },
{ "exec", 1, cron_exec },
};
crontab_t *rdcron(int dirfd, const char *filename)
{
crontab_t *cron = NULL;
rdline_t rd;
int ret;
if (rdline_init(&rd, dirfd, filename, 0, NULL))
return NULL;
cron = calloc(1, sizeof(*cron));
if (cron == NULL) {
fputs("out of memory\n", stderr);
goto out;
}
cron->minute = 0xFFFFFFFFFFFFFFFFUL;
cron->hour = 0xFFFFFFFF;
cron->dayofmonth = 0xFFFFFFFF;
cron->month = 0xFFFF;
cron->dayofweek = 0xFF;
ret = rdcfg(cron, &rd, cron_params, ARRAY_SIZE(cron_params), 0);
if (ret) {
delcron(cron);
cron = NULL;
}
out:
rdline_cleanup(&rd);
return cron;
}

View file

@ -1,86 +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 "gcrond.h"
int runjob(crontab_t *tab)
{
struct sigaction act;
pid_t pid;
exec_t *e;
int ret;
if (tab->exec == NULL)
return 0;
pid = fork();
if (pid == -1) {
perror("fork");
return -1;
}
if (pid != 0)
return 0;
/* XXX: inside the child process */
memset(&act, 0, sizeof(act));
act.sa_handler = SIG_DFL;
sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
sigaction(SIGHUP, &act, NULL);
sigaction(SIGCHLD, &act, NULL);
if (setup_tty(tab->ctty, tab->tty_truncate))
exit(EXIT_FAILURE);
if (tab->gid != 0) {
if (setresgid(tab->gid, tab->gid, tab->gid)) {
perror("setgid");
exit(EXIT_FAILURE);
}
}
if (tab->uid != 0) {
if (setresuid(tab->uid, tab->uid, tab->uid)) {
perror("setuid");
exit(EXIT_FAILURE);
}
}
if (tab->exec->next == NULL)
argv_exec(tab->exec);
for (e = tab->exec; e != NULL; e = e->next) {
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0)
argv_exec(e);
while (waitpid(pid, &ret, 0) != pid)
;
ret = WIFEXITED(ret) ? WEXITSTATUS(ret) : EXIT_FAILURE;
if (ret != EXIT_SUCCESS)
break;
}
exit(ret);
}

View file

@ -13,15 +13,15 @@ Currently available service commands are:
* disable - disable a service. If the service is parameterized, requires the
same arguments used for enabling, to disable the specific instance of the
service.
* schedule - enable a gcrond service. Only available if this package is built
with gcrond.
* unschedule - disnable a gcrond service. Only available if this package is
built with gcrond.
* 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

View file

@ -1,62 +0,0 @@
# Gcron
Gcron is a small daemon that executes batch commands once a certain
condition is met.
In a typical installation, it reads configuration files from `/etc/gcron.d`.
If used together with the init system in this package, the `service` command
can be used to administer symlinks in that directory, pointing
to `/usr/share/init/<name>.gcron`.
Each file in the configuration directory represents a single scheduled batch
job. The syntax and most of the keywords are similar to `initd` service files
(See [services.md](services.md)).
## Cron Style Patterns
The following keywords can be used to specify classic cron style patterns for
when a job should be run:
* `hour`
* `minute`
* `dayofmonth`
* `dayofweek`
* `month`
For each of those keywords, a comma separated sequence of times can be
specified. Time ranges can be specified using the syntax `<start>-<end>`,
or using `*` for every possible value. A sequence (either range or star)
can be suffixed with `/<step>` to specify an increment.
For instance, `minute */5` means every five minutes and `minute 15-30/2`
means every two minutes between quarter past and half past.
In addition to numeric values, the keywords `dayofweek` and `month` allow
specifying 3 letter, uppercase week day and moth names such as `MON`, `TUE`,
etc and `JAN`, `FEB`, ...
The job is only run when all specified conditions are met. Omitting a field
is the same as specifying `*`.
## Named Intervals
Alternatively to the above, the keyword `interval` can be used. The following
intervals can be specified:
* `yearly` or `annually` means on every January the first at midnight.
* `monthly` means on every first of the month at midnight.
* `weekly` means every Sunday at midnight.
* `daily` means every day at midnight.
* `hourly` means every first minute of the hour.
## Command Specification
To specify *what* should be done once the condition is met, the following
keywords can be used:
* `exec` - the command to run. Multiple commands can be grouped
using curly braces.
* `user` - a user name or ID to set before running the commands.
* `group` - a group name or ID to set before running the commands.
* `tty` - similar to init service files, the controlling tty or output file
for the batch commands. Like init service files, the `truncate` keyword
can be used.

View file

@ -1,8 +1,8 @@
init_SOURCES = initd/main.c initd/init.h initd/signal_linux.c initd/runsvc.c
init_SOURCES += initd/status.c initd/supervisor.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)
init_LDADD = libinit.a libcfg.a libutil.a
init_LDADD = libinit.a libcfg.a
sbin_PROGRAMS += init

View file

@ -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 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>
@ -31,12 +17,15 @@
#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 "util.h"
#include "config.h"
#define RUNSVCBIN SCRIPTDIR "/runsvc"
#define ENVFILE ETCPATH "/initd.env"
#define PROCFDDIR "/proc/self/fd"
enum {
STATUS_OK = 0,
@ -81,28 +70,20 @@ void supervisor_init(void);
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
View 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;
}

View file

@ -1,23 +1,8 @@
/* 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 "init.h"
static int sigfd = -1;
static int sockfd = -1;
static void handle_signal(void)
{
@ -45,6 +30,51 @@ static void handle_signal(void)
case SIGINT:
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 handle_request(void)
{
struct sockaddr_un addr;
init_request_t rq;
socklen_t addrlen;
ssize_t ret;
retry:
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 && 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;
}
}
@ -52,6 +82,8 @@ void target_completed(int target)
{
switch (target) {
case TGT_BOOT:
if (sockfd < 0)
sockfd = init_socket_create();
break;
case TGT_SHUTDOWN:
for (;;)
@ -64,9 +96,32 @@ void target_completed(int target)
}
}
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 ret, count;
int i, ret, count;
struct pollfd pfd[2];
if (getpid() != 1) {
@ -85,15 +140,29 @@ int main(void)
;
memset(pfd, 0, sizeof(pfd));
pfd[0].fd = sigfd;
pfd[0].events = POLLIN;
count = 1;
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();
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();
}
}
}

View file

@ -1,50 +1,177 @@
/* 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;

View file

@ -1,56 +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;
}
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
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);
}

View file

@ -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"

View file

@ -1,37 +1,68 @@
/* 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 "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);
delsvc(svc);
svc->next = completed;
completed = svc;
return -1;
}
@ -47,12 +78,15 @@ static void handle_terminated_service(service_t *svc)
if (target == TGT_REBOOT || target == TGT_SHUTDOWN)
break;
if (svc->rspwn_limit > 0) {
svc->rspwn_limit -= 1;
if (svc->flags & SVC_FLAG_ADMIN_STOPPED)
break;
if (svc->rspwn_limit == 0) {
if (svc->rspwn_limit > 0) {
svc->rspwn_count += 1;
if (svc->rspwn_count >= svc->rspwn_limit) {
print_status(svc->desc, STATUS_FAIL, false);
break;
goto out_failure;
}
}
@ -65,6 +99,8 @@ static void handle_terminated_service(service_t *svc)
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;
@ -73,9 +109,16 @@ static void handle_terminated_service(service_t *svc)
STATUS_OK : STATUS_FAIL, false);
if (singleshot == 0 && queue == NULL && !waiting)
target_completed(target);
if (svc->status != EXIT_SUCCESS)
goto out_failure;
break;
}
delsvc(svc);
svc->next = completed;
completed = svc;
return;
out_failure:
svc->next = failed;
failed = svc;
}
void supervisor_handle_exited(pid_t pid, int status)
@ -132,7 +175,7 @@ void supervisor_init(void)
{
int status = STATUS_OK;
if (svcscan(SVCDIR, &cfg, RDSVC_NO_EXEC | RDSVC_NO_CTTY))
if (svcscan(SVCDIR, &cfg))
status = STATUS_FAIL;
target = TGT_BOOT;
@ -142,6 +185,45 @@ void supervisor_init(void)
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;
@ -160,6 +242,14 @@ bool supervisor_process_queues(void)
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;
@ -175,8 +265,97 @@ bool supervisor_process_queues(void)
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);
}
}

View file

@ -1,18 +1,15 @@
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)
libutil_a_SOURCES = lib/util/argv_exec.c lib/util/enum_by_name.c
libutil_a_SOURCES += lib/util/enum_to_name.c lib/util/print_version.c
libutil_a_SOURCES += lib/util/fopenat.c lib/include/util.h
libutil_a_CPPFLAGS = $(AM_CPPFLAGS)
libutil_a_CFLAGS = $(AM_CFLAGS)
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)
noinst_LIBRARIES += libinit.a libcfg.a libutil.a
noinst_LIBRARIES += libinit.a libcfg.a

65
lib/include/initsock.h Normal file
View 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 */

View file

@ -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 */
#ifndef LIBCONFIG_H
#define LIBCONFIG_H
@ -43,7 +27,7 @@ typedef struct {
*/
unsigned int allow_block : 1;
int (*handle)(void *obj, char *arg, rdline_t *rd, int flags);
int (*handle)(void *obj, char *arg, rdline_t *rd);
} cfg_param_t;
/*
@ -108,11 +92,10 @@ int pack_argv(char *str);
/*
Parse a configuration file containing '<keyword> [arguments...]' lines.
The cfgobj and flags are passed to the callback in the params array.
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,
int flags);
int rdcfg(void *cfgobj, rdline_t *rd, const cfg_param_t *params, size_t count);
#endif /* LIBCONFIG_H */

View file

@ -1,26 +1,14 @@
/* 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>
#include "util.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 {
/*
@ -35,6 +23,8 @@ enum {
it terminates.
*/
SVC_RESPAWN,
SVC_MAX
};
enum {
@ -45,16 +35,12 @@ enum {
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,
SVC_FLAG_HAS_EXEC = 0x10,
SVC_FLAG_ADMIN_STOPPED = 0x20,
};
typedef struct service_t {
@ -67,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 */
@ -80,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;
@ -91,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);
@ -102,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);
@ -120,4 +108,3 @@ const char *svc_target_to_string(int target);
int svc_target_from_string(const char *target);
#endif /* SERVICE_H */

View file

@ -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/>.
*/
#ifndef UTIL_H
#define UTIL_H
#include <sys/types.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.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;
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 {
/* 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);
/* print a default version info and license string */
NORETURN void print_version(const char *program);
int setup_tty(const char *tty, bool truncate);
NORETURN void argv_exec(exec_t *e);
/*
Similar to openat: opens a file relative to a dirfd, but returns
a FILE pointer instead of an fd.
*/
FILE *fopenat(int fd, const char *filename, const char *mode);
#endif /* UTIL_H */

View file

@ -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 "service.h"

View file

@ -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 "service.h"

View 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);
}

View 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;
}

View 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;
}

View 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;
}

View file

@ -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,10 +43,9 @@ static int try_pack_argv(char *str, rdline_t *rd)
return count;
}
static int svc_desc(void *user, char *arg, rdline_t *rd, int flags)
static int svc_desc(void *user, char *arg, rdline_t *rd)
{
service_t *svc = user;
(void)flags;
if (try_unescape(arg, rd))
return -1;
@ -71,13 +53,10 @@ static int svc_desc(void *user, char *arg, rdline_t *rd, int flags)
return svc->desc == NULL ? -1 : 0;
}
static int svc_tty(void *user, char *arg, rdline_t *rd, int flags)
static int svc_tty(void *user, char *arg, rdline_t *rd)
{
service_t *svc = user;
if (flags & RDSVC_NO_CTTY)
return 0;
if (strncmp(arg, "truncate", 8) == 0 && isspace(arg[8])) {
svc->flags |= SVC_FLAG_TRUNCATE_OUT;
arg += 8;
@ -92,13 +71,12 @@ static int svc_tty(void *user, char *arg, rdline_t *rd, int flags)
return svc->ctty == NULL ? -1 : 0;
}
static int svc_exec(void *user, char *arg, rdline_t *rd, int flags)
static int svc_exec(void *user, char *arg, rdline_t *rd)
{
service_t *svc = user;
exec_t *e, *end;
if (flags & RDSVC_NO_EXEC)
return 0;
svc->flags |= SVC_FLAG_HAS_EXEC;
e = calloc(1, sizeof(*e) + strlen(arg) + 1);
if (e == NULL) {
@ -123,13 +101,10 @@ static int svc_exec(void *user, char *arg, rdline_t *rd, int flags)
return 0;
}
static int svc_before(void *user, char *arg, rdline_t *rd, int flags)
static int svc_before(void *user, char *arg, rdline_t *rd)
{
service_t *svc = user;
if (flags & RDSVC_NO_DEPS)
return 0;
if (svc->before != NULL) {
fprintf(stderr, "%s: %zu: 'before' dependencies respecified\n",
rd->filename, rd->lineno);
@ -144,13 +119,10 @@ static int svc_before(void *user, char *arg, rdline_t *rd, int flags)
return (svc->num_before < 0) ? -1 : 0;
}
static int svc_after(void *user, char *arg, rdline_t *rd, int flags)
static int svc_after(void *user, char *arg, rdline_t *rd)
{
service_t *svc = user;
if (flags & RDSVC_NO_DEPS)
return 0;
if (svc->after != NULL) {
fprintf(stderr, "%s: %zu: 'after' dependencies respecified\n",
rd->filename, rd->lineno);
@ -165,11 +137,10 @@ static int svc_after(void *user, char *arg, rdline_t *rd, int flags)
return (svc->num_after < 0) ? -1 : 0;
}
static int svc_type(void *user, char *arg, rdline_t *rd, int flags)
static int svc_type(void *user, char *arg, rdline_t *rd)
{
service_t *svc = user;
int count = try_pack_argv(arg, rd);
(void)flags;
if (count < 1)
return -1;
@ -213,11 +184,10 @@ fail_limit:
return -1;
}
static int svc_target(void *user, char *arg, rdline_t *rd, int flags)
static int svc_target(void *user, char *arg, rdline_t *rd)
{
service_t *svc = user;
int target;
(void)flags;
if (try_unescape(arg, rd))
return -1;
@ -244,7 +214,7 @@ static const cfg_param_t svc_params[] = {
{ "after", 0, svc_after },
};
service_t *rdsvc(int dirfd, const char *filename, int flags)
service_t *rdsvc(int dirfd, const char *filename)
{
const char *arg, *args[1];
service_t *svc = NULL;
@ -268,16 +238,17 @@ 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;
if (rdcfg(svc, &rd, svc_params, ARRAY_SIZE(svc_params), flags))
if (rdcfg(svc, &rd, svc_params,
sizeof(svc_params) / sizeof(svc_params[0]))) {
goto fail;
}
out:
rdline_cleanup(&rd);

View file

@ -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>

View file

@ -1,57 +1,49 @@
/* 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 <string.h>
#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 char *type_map[] = {
"once",
"wait",
"respawn",
};
static const enum_map_t target_map[] = {
{ "boot", TGT_BOOT },
{ "shutdown", TGT_SHUTDOWN },
{ "reboot", TGT_REBOOT },
{ NULL, 0 },
static const char *target_map[] = {
"boot",
"shutdown",
"reboot",
};
const char *svc_type_to_string(int type)
{
return enum_to_name(type_map, type);
return type >= 0 && type < SVC_MAX ? type_map[type] : NULL;
}
int svc_type_from_string(const char *type)
{
const enum_map_t *ent = enum_by_name(type_map, type);
size_t i;
return ent == NULL ? -1 : ent->value;
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 enum_to_name(target_map, target);
return target >= 0 && target < TGT_MAX ? target_map[target] : NULL;
}
int svc_target_from_string(const char *target)
{
const enum_map_t *ent = enum_by_name(target_map, target);
size_t i;
return ent == NULL ? -1 : ent->value;
for (i = 0; i < sizeof(target_map) / sizeof(target_map[0]); ++i) {
if (strcmp(target_map[i], target) == 0)
return i;
}
return -1;
}

View file

@ -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;

View file

@ -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>

View file

@ -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 "libcfg.h"
#include <string.h>
@ -63,8 +47,7 @@ static int splitkv(rdline_t *rd, char **k, char **v)
return 0;
}
int rdcfg(void *cfgobj, rdline_t *rd, const cfg_param_t *params, size_t count,
int flags)
int rdcfg(void *cfgobj, rdline_t *rd, const cfg_param_t *params, size_t count)
{
const cfg_param_t *p;
char *key, *value;
@ -83,7 +66,7 @@ int rdcfg(void *cfgobj, rdline_t *rd, const cfg_param_t *params, size_t count,
;
if (*value != '\0') {
ret = p->handle(cfgobj, value, rd, flags);
ret = p->handle(cfgobj, value, rd);
if (ret)
return -1;
}
@ -91,7 +74,7 @@ int rdcfg(void *cfgobj, rdline_t *rd, const cfg_param_t *params, size_t count,
while ((ret = rdline(rd)) == 0) {
if (strcmp(rd->line, "}") == 0)
break;
if (p->handle(cfgobj, rd->line, rd, flags))
if (p->handle(cfgobj, rd->line, rd))
return -1;
}
@ -99,7 +82,7 @@ int rdcfg(void *cfgobj, rdline_t *rd, const cfg_param_t *params, size_t count,
return -1;
if (ret > 0)
goto fail_bra;
} else if (p->handle(cfgobj, value, rd, flags)) {
} else if (p->handle(cfgobj, value, rd)) {
return -1;
}
}

View file

@ -1,45 +1,37 @@
/* 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"
#include "util.h"
int rdline_init(rdline_t *t, int dirfd, const char *filename,
int argc, const char *const *argv)
{
int fd = openat(dirfd, filename, O_RDONLY);
if (fd == -1)
goto fail_open;
memset(t, 0, sizeof(*t));
t->fp = fopenat(dirfd, filename, "r");
t->fp = fdopen(fd, "r");
if (t->fp == NULL) {
perror(filename);
return -1;
close(fd);
goto fail_open;
}
t->filename = filename;
t->argc = argc;
t->argv = argv;
return 0;
fail_open:
perror(filename);
return -1;
}
void rdline_cleanup(rdline_t *t)
@ -142,6 +134,8 @@ static void substitute(rdline_t *t, char *dst, char *src)
*(dst++) = *(src++);
}
}
*(dst++) = '\0';
}
int rdline(rdline_t *t)
@ -156,8 +150,10 @@ int rdline(rdline_t *t)
goto out;
} while (t->line[0] == '\0');
if (ret == 0)
if (ret == 0) {
substitute(t, t->line, t->line);
return 0;
}
buffer = calloc(1, strlen(t->line) + ret + 1);
if (buffer == NULL) {

View file

@ -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>

View file

@ -1,67 +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 <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
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);
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;
}
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);
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -1,70 +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/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include "util.h"
FILE *fopenat(int dirfd, const char *filename, const char *mode)
{
const char *ptr = mode;
int fd, flags = 0;
FILE *fp;
switch (*(ptr++)) {
case 'r':
flags = O_RDONLY;
break;
case 'w':
flags = O_WRONLY | O_CREAT | O_TRUNC;
break;
case 'a':
flags = O_WRONLY | O_CREAT | O_APPEND;
break;
default:
errno = EINVAL;
return NULL;
}
if (*ptr == '+') {
flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
++ptr;
}
if (*ptr == 'b')
++ptr;
if (*ptr != '\0') {
errno = EINVAL;
return NULL;
}
fd = openat(dirfd, filename, flags, 0644);
if (fd == -1)
return NULL;
fp = fdopen(fd, mode);
if (fp == NULL)
close(fd);
return fp;
}

View file

@ -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/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include "config.h"
#include "util.h"
#define GPL_URL "https://gnu.org/licenses/gpl.html"
static const char *version_string =
"%s (pygos init) " PACKAGE_VERSION "\n"
"Copyright (C) 2018 David Oberhollenzer\n\n"
"License GPLv3+: GNU GPL version 3 or later <" GPL_URL ">.\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";
void print_version(const char *program)
{
fprintf(stdout, version_string, program);
exit(EXIT_SUCCESS);
}

View file

@ -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"