1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdlib.h>
5 
6 #include "sd-daemon.h"
7 
8 #include "pager.h"
9 #include "selinux-util.h"
10 #include "spawn-ask-password-agent.h"
11 #include "spawn-polkit-agent.h"
12 #include "static-destruct.h"
13 #include "util.h"
14 
15 #define _DEFINE_MAIN_FUNCTION(intro, impl, ret)                         \
16         int main(int argc, char *argv[]) {                              \
17                 int r;                                                  \
18                 assert_se(argc > 0 && !isempty(argv[0]));               \
19                 save_argc_argv(argc, argv);                             \
20                 intro;                                                  \
21                 r = impl;                                               \
22                 if (r < 0)                                              \
23                         (void) sd_notifyf(0, "ERRNO=%i", -r);           \
24                 ask_password_agent_close();                             \
25                 polkit_agent_close();                                   \
26                 pager_close();                                          \
27                 mac_selinux_finish();                                   \
28                 static_destruct();                                      \
29                 return ret;                                             \
30         }
31 
32 /* Negative return values from impl are mapped to EXIT_FAILURE, and
33  * everything else means success! */
34 #define DEFINE_MAIN_FUNCTION(impl)                                      \
35         _DEFINE_MAIN_FUNCTION(,impl(argc, argv), r < 0 ? EXIT_FAILURE : EXIT_SUCCESS)
36 
37 /* Zero is mapped to EXIT_SUCCESS, negative values are mapped to EXIT_FAILURE,
38  * and positive values are propagated.
39  * Note: "true" means failure! */
40 #define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(impl)                \
41         _DEFINE_MAIN_FUNCTION(,impl(argc, argv), r < 0 ? EXIT_FAILURE : r)
42