arguments.h 4.53 KB
Newer Older
1 2 3
/*
 * Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)
 *
4
 * This file is part of the turris updater.
5
 *
6
 * Updater is free software: you can redistribute it and/or modify
7 8 9 10
 * 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.
 *
11
 * Updater is distributed in the hope that it will be useful,
12 13 14 15 16
 * 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
17
 * along with Updater.  If not, see <http://www.gnu.org/licenses/>.
18 19 20 21 22 23 24
 */

#ifndef UPDATER_ARGUMENTS_H
#define UPDATER_ARGUMENTS_H

// An operation type to be performed
enum cmd_op_type {
25
	// Terminate with non-zero exit code.
26 27 28
	COT_CRASH,
	// Terminate with zero exit code.
	COT_EXIT,
29 30
	// Print help.
	COT_HELP,
31 32
	// Print updater version.
	COT_VERSION,
33 34
	// Print error message stored in argument variable.
	COT_ERR_MSG,
35 36 37 38 39 40 41
	// Clean up any unfinished journal work and roll back whatever can be.
	COT_JOURNAL_ABORT,
	// Resume interrupted operation from journal, if any is there.
	COT_JOURNAL_RESUME,
	// Install a package. A parameter is passed, with the path to the .ipk file.
	COT_INSTALL,
	// Remove a package from the system. A parameter is passed with the name of the package.
42 43
	COT_REMOVE,
	// Set a root directory (the parameter is the directory to set to)
44
	COT_ROOT_DIR,
45 46
	// Run without the user confirmation
	COT_BATCH,
47 48
	// Internally used when reexecuting. Informs program that this is restarted instance.
	COT_REEXEC,
49 50
	// Internally used when reexecuting to pass information that we should reboot after updater finishes.
	COT_REBOOT,
51 52
	// Enable dumping state to files in /tmp/updater-state directory
	COT_STATE_LOG,
53 54 55 56
	// Ask for approval of the operations by generating a report and requiring the --approve flag
	COT_ASK_APPROVAL,
	// Approve operation with the given ID (as obtained from the above mentioned report). The ID is in the parameter.
	COT_APPROVE,
57 58 59 60 61 62
	// Syslog level
	COT_SYSLOG_LEVEL,
	// Stderr log level
	COT_STDERR_LEVEL,
	// Name of the syslog
	COT_SYSLOG_NAME,
63 64
	// Put the output into a file
	COT_OUTPUT,
65 66
	// Log tasks into a file
	COT_TASK_LOG,
67 68
	// Exclude this from output
	COT_EXCLUDE,
69 70
	// Path to usign tool
	COT_USIGN,
71 72
	// Don't replan (do whole install at once)
	COT_NO_REPLAN,
73 74 75 76 77
	// Don't immediatelly reboot system
	COT_NO_IMMEDIATE_REBOOT,
	// Run out of root (implies COT_NO_REPLAN and COT_NO_IMMEDIATE_REBOOT)
	// This one is enabled automatically if NO_REPLAN and NO_IMMEDIATE_REBOOT is enabled
	COT_OUT_OF_ROOT,
78 79 80 81
	// Argument isn't option.
	COT_NO_OP,
	// Automatic last dummy value to know size of enum
	COT_LAST
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
};

// A whole operation to be performed, with any needed parameter.
struct cmd_op {
	// What to do.
	enum cmd_op_type type;
	// With what. If the type doesn't expect a parameter, it is set to NULL.
	const char *parameter;
};

/*
 * Parse the command line arguments (or any other string array,
 * as passed) and produce list of requested operations. Note that
 * the operations don't have to correspond one to one with the
 * arguments.
 *
98 99
 * Argument accepts must be COT_LAST terminated array of all allowed operations.
 *
100 101 102 103 104 105
 * The result is allocated on the heap. The parameters are not
 * allocated, they point to the strings passed in argv.
 *
 * The result is always terminated by an operation of type COT_CRASH
 * or COT_EXIT.
 */
106
struct cmd_op *cmd_args_parse(int argc, char *argv[], const enum cmd_op_type accepts[]) __attribute__((nonnull)) __attribute__((returns_nonnull));
107

108 109 110 111 112 113
/*
 * Prints help for accepted arguments
 *
 * Argument accepts is COT_LAST terminated array of all allowed operations.
 */
void cmd_args_help(const enum cmd_op_type accepts[]);
114

115 116 117 118 119
/*
 * Prints message containing updater version
 */
void cmd_args_version(void);

120 121 122 123 124 125 126 127 128
/*
 * Deep-copy the arguments. They can be used in the reexec() function.
 */
void args_backup(int argc, const char **argv);
// Free the backup of arguments.
void arg_backup_clear();
/*
 * Exec the same binary with the same arguments, effectively
 * restarting the whole process.
129 130 131 132 133
 * You can pass additional arguments that will be appended to end of original
 * ones. Arguments args_count is number of arguments to be appended and args is
 * array containing those arguments. You can pass (0, NULL) to append no
 * arguments.
 * This function newer returns so arguments can be allocated even on stack.
134
 */
135
void reexec(int args_count, char *args[]) __attribute__((noreturn));
136

137
#endif