1 # Copyright 2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: acct-group.eclass
6 # Michał Górny <mgorny@gentoo.org>
8 # Michael Orlitzky <mjo@gentoo.org>
9 # Michał Górny <mgorny@gentoo.org>
11 # @BLURB: Eclass used to create and maintain a single group entry
13 # This eclass represents and creates a single group entry. The name
14 # of the group is derived from ${PN}, while (preferred) GID needs to
15 # be specified via ACCT_GROUP_ID. Packages (and users) needing the group
16 # in question should depend on the package providing it.
19 # If your package needs group 'foo', you create 'acct-group/foo' package
20 # and add an ebuild with the following contents:
28 # Then you add appropriate dependency to your package. The dependency
30 # - DEPEND (+ RDEPEND) if the group is already needed at build time,
31 # - RDEPEND if it is needed at install time (e.g. you 'fowners' files
32 # in pkg_preinst) or run time.
34 if [[ -z ${_ACCT_GROUP_ECLASS} ]]; then
39 *) die "EAPI=${EAPI:-0} not supported";;
44 [[ ${CATEGORY} == acct-group ]] ||
45 die "Ebuild error: this eclass can be used only in acct-group category!"
48 # << Eclass variables >>
50 # @ECLASS-VARIABLE: ACCT_GROUP_NAME
53 # The name of the group. This is forced to ${PN} and the policy
54 # prohibits it from being changed.
56 readonly ACCT_GROUP_NAME
58 # @ECLASS-VARIABLE: ACCT_GROUP_ID
61 # Preferred GID for the new group. This variable is obligatory, and its
62 # value must be unique across all group packages.
64 # Overlays should set this to -1 to dynamically allocate GID. Using -1
65 # in ::gentoo is prohibited by policy.
67 # @ECLASS-VARIABLE: ACCT_GROUP_ENFORCE_ID
69 # If set to a non-null value, the eclass will require the group to have
70 # specified GID. If the group already exists with another GID, or
71 # the GID is taken by another group, the install will fail.
72 : ${ACCT_GROUP_ENFORCE_ID:=}
75 # << Boilerplate ebuild variables >>
76 : ${DESCRIPTION:="System group: ${ACCT_GROUP_NAME}"}
78 : ${KEYWORDS:=alpha amd64 arm arm64 hppa ia64 m68k ~mips ppc ppc64 ~riscv s390 sparc x86 ~ppc-aix ~x64-cygwin ~amd64-linux ~x86-linux ~ppc-macos ~x64-macos ~x86-macos ~m68k-mint ~sparc-solaris ~sparc64-solaris ~x64-solaris ~x86-solaris}
82 # << Phase functions >>
83 EXPORT_FUNCTIONS pkg_pretend pkg_preinst
85 # @FUNCTION: acct-group_pkg_pretend
87 # Performs sanity checks for correct eclass usage, and early-checks
88 # whether requested GID can be enforced.
89 acct-group_pkg_pretend() {
90 debug-print-function ${FUNCNAME} "${@}"
92 # verify ACCT_GROUP_ID
93 [[ -n ${ACCT_GROUP_ID} ]] || die "Ebuild error: ACCT_GROUP_ID must be set!"
94 [[ ${ACCT_GROUP_ID} -eq -1 ]] && return
95 [[ ${ACCT_GROUP_ID} -ge 0 ]] || die "Ebuild errors: ACCT_GROUP_ID=${ACCT_GROUP_ID} invalid!"
97 # check for ACCT_GROUP_ID collisions early
98 if [[ -n ${ACCT_GROUP_ENFORCE_ID} ]]; then
99 local group_by_id=$(egetgroupname "${ACCT_GROUP_ID}")
100 local group_by_name=$(egetent group "${ACCT_GROUP_NAME}")
101 if [[ -n ${group_by_id} ]]; then
102 if [[ ${group_by_id} != ${ACCT_GROUP_NAME} ]]; then
103 eerror "The required GID is already taken by another group."
104 eerror " GID: ${ACCT_GROUP_ID}"
105 eerror " needed for: ${ACCT_GROUP_NAME}"
106 eerror " current group: ${group_by_id}"
107 die "GID ${ACCT_GROUP_ID} taken already"
109 elif [[ -n ${group_by_name} ]]; then
110 eerror "The requested group exists already with wrong GID."
111 eerror " groupname: ${ACCT_GROUP_NAME}"
112 eerror " requested GID: ${ACCT_GROUP_ID}"
113 eerror " current entry: ${group_by_name}"
114 die "Group ${ACCT_GROUP_NAME} exists with wrong GID"
119 # @FUNCTION: acct-group_pkg_preinst
121 # Creates the group if it does not exist yet.
122 acct-group_pkg_preinst() {
123 debug-print-function ${FUNCNAME} "${@}"
125 enewgroup ${ACCT_GROUP_ENFORCE_ID:+-F} "${ACCT_GROUP_NAME}" \