X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=common-src%2Futil.h;h=8c7ea449a2866a39ba57d4e105bab3fa4a9211e9;hb=b116e9366c7b2ea2c2eb53b0a13df4090e176235;hp=3a9efe7fcc880eadf00d4f710cb3914fecd4948d;hpb=fb2bd066c2f8b34addafe48d62550e3033a59431;p=debian%2Famanda diff --git a/common-src/util.h b/common-src/util.h index 3a9efe7..8c7ea44 100644 --- a/common-src/util.h +++ b/common-src/util.h @@ -44,25 +44,96 @@ /* internal types and variables */ +/* Function to get the GQuark for errors, + * with error codes specified by AmUtilError + * + * @return The GQuark that's used for errors + */ +GQuark am_util_error_quark(void); + +/* Error codes that may be returned by these functions */ +typedef enum { + AM_UTIL_ERROR_HEXDECODEINVAL, +} AmUtilError; -ssize_t fullread(int, void *, size_t); -ssize_t fullwrite(int, const void *, size_t); int connect_portrange(sockaddr_union *, in_port_t, in_port_t, char *, sockaddr_union *, int); int bind_portrange(int, sockaddr_union *, in_port_t, in_port_t, char *); +/* just like an accept() call, but periodically calling PROLONG(PROLONG_DATA) and + * returning -1 with errno set to 0 if PROLONG returns false. Note that the socket + * need not be configured as non-blocking. + * + * Other arguments are just like for accept(2). + */ +int interruptible_accept(int sock, struct sockaddr *addr, socklen_t *addrlen, + gboolean (*prolong)(gpointer data), gpointer prolong_data); + +ssize_t full_writev(int, struct iovec *, int); + char * construct_datestamp(time_t *t); char * construct_timestamp(time_t *t); -/*@only@*//*@null@*/char *quote_string(const char *str); +/* quote_string only adds "" if they're required; quote_string_always + * always adds "" around the string */ +#define quote_string(str) quote_string_maybe((str), 0) +#define quote_string_always(str) quote_string_maybe((str), 1) + +/*@only@*//*@null@*/char *quote_string_maybe(const char *str, gboolean always); /*@only@*//*@null@*/char *unquote_string(const char *str); -int needs_quotes(const char * str); + +/* Split a string into space-delimited words, obeying quoting as created by + * quote_string. To keep compatibility with the old split(), this has the + * characteristic that multiple consecutive spaces are not collapsed into + * a single space: "x y" parses as [ "x", "", "y", NULL ]. The strings are + * unquoted before they are returned, unlike split(). An empty string is + * split into [ "", NULL ]. + * + * Returns a NULL-terminated array of strings, which should be freed with + * g_strfreev. + */ +gchar ** split_quoted_strings(const gchar *string); + +/* Like strtok_r, but consider a quoted string to be a single token. Caller + * must begin parsing with strtok_r first, then pass the saveptr to this function. + * + * Returns NULL on unparseable strings (e.g., unterminated quotes, bad escapes) + */ +char * strquotedstr(char **saveptr); char * sanitize_string(const char *str); + +/* Encode a string using URI-style hexadecimal encoding. + * Non-alphanumeric characters will be replaced with "%xx" + * where "xx" is the two-digit hexadecimal representation of the character. + * + * @param str The string to encode + * + * @return The encoded string. An empty string will be returned for NULL. + */ +char * hexencode_string(const char *str); + +/* Decode a string using URI-style hexadecimal encoding. + * + * @param str The string to decode + * @param err return location for a GError + * + * @return The decoded string. An empty string will be returned for NULL + * or if an error occurs. + */ +char * hexdecode_string(const char *str, GError **err); + int copy_file(char *dst, char *src, char **errmsg); +/* These two functions handle "braced alternates", which is a syntax borrowed, + * partially, from shells. See perl/Amanda/Util.pod for a full description of + * the syntax they support. + */ +GPtrArray * expand_braced_alternates(char * source); +char * collapse_braced_alternates(GPtrArray *source); + /* * validate_email return 0 if the following characters are present * * ( ) < > [ ] , ; : ! $ \ / " @@ -103,11 +174,6 @@ void free_new_argv(int new_argc, char **new_argv); * strings, instead of segfaulting. */ int compare_possibly_null_strings(const char * a, const char * b); -/* Does g_thread_init(), along with anything else that should be done - * before/after thread setup. It's OK to call this function more than once. - * Returns TRUE if threads are supported. */ -gboolean amanda_thread_init(void); - /* Given a hostname, call getaddrinfo to resolve it. Optionally get the * entire set of results (if res is not NULL) and the canonical name of * the host (if canonname is not NULL). The canonical name might @@ -118,10 +184,10 @@ gboolean amanda_thread_init(void); * not NULL. * * @param hostname: the hostname to start with + * @param socktype: the socket type (SOCK_DGRAM or SOCK_STREAM) * @param res: (result) if not NULL, the results from getaddrinfo() - * @param canonname: (result) if not NULL, the canonical name of the host - * @returns: newly allocated canonical hostname, or NULL if no - * canonical hostname was available. + * @param canonname: (result) if not NULL, the newly-allocated canonical name of the host + * @returns: 0 on success, otherwise a getaddrinfo result (for use with gai_strerror) */ int resolve_hostname(const char *hostname, int socktype, struct addrinfo **res, char **canonname); @@ -155,6 +221,9 @@ char *_str_exit_status(char *subject, amwait_t status); * @param who: one of the RUNNING_AS_* constants, below. */ typedef enum { + /* doesn't matter */ + RUNNING_AS_ANY, + /* userid is 0 */ RUNNING_AS_ROOT, @@ -170,7 +239,7 @@ typedef enum { RUNNING_AS_CLIENT_LOGIN, RUNNING_AS_USER_MASK = (1 << 8) - 1, - /* '&' this on to only check the uid, not the euid; use this for programs + /* '|' this on to only check the uid, not the euid; use this for programs * that will call become_root() */ RUNNING_AS_UID_ONLY = 1 << 8 } running_as_flags; @@ -181,8 +250,8 @@ void check_running_as(running_as_flags who); * need to be root for certain operations. Does nothing if SINGLE_USERID is * defined. * - * @param need_root: if true, try to assume root priviledges; otherwise, drop - * priviledges. + * @param need_root: if 1, try to assume root priviledges; otherwise, drop + * priviledges. If -1, drop them irreversibly. * @returns: true if the priviledge change succeeded */ int set_root_privs(int need_root); @@ -199,6 +268,24 @@ int become_root(void); * Process parameters */ +/* The 'context' of a process gives a general description of how it is + * used. This affects log output, among other things. + */ +typedef enum { + /* default context (logging to stderr, etc. -- not pretty) */ + CONTEXT_DEFAULT = 0, + + /* user-interfacing command-line utility like amadmin */ + CONTEXT_CMDLINE, + + /* daemon like amandad or sendbackup */ + CONTEXT_DAEMON, + + /* a utility used from shell scripts, and thus probably invoked + * quite often */ + CONTEXT_SCRIPTUTIL, +} pcontext_t; + /* Set the name of the process. The parameter is copied, and remains * the responsibility of the caller on return. This value is used in log * messages and other output throughout Amanda. @@ -214,6 +301,33 @@ void set_pname(char *pname); */ char *get_pname(void); +/* Set the type of the process. The parameter is copied, and remains + * the responsibility of the caller on return. This value dictates the + * directory in which debug logs are stored. + * + * @param pname: the new process type + */ +void set_ptype(char *ptype); + +/* Get the current process name; the result is in a static buffer, and + * should *not* be free()d by the caller. + * + * @returns: process name + */ +char *get_ptype(void); + +/* Set the process's context + * + * @param context: the new context + */ +void set_pcontext(pcontext_t context); + +/* Get the process's context + * + * @returns: the context + */ +pcontext_t get_pcontext(void); + /* * Readline support * @@ -242,4 +356,29 @@ void add_history(const char *line); #endif +char *base64_decode_alloc_string(char *); + +/* Inform the OpenBSD pthread library about the high-numbered file descriptors + * that an amandad service inherits. This won't be necessary once the new + * threading library is availble (OpenBSD 5.0?), but won't hurt anyway. See the + * thread "Backup issues with OpenBSD 4.5 machines" from September 2009. */ +#ifdef __OpenBSD__ +void openbsd_fd_inform(void); +#else +#define openbsd_fd_inform() +#endif + +/* Add all properties to an ARGV + * + * @param argvchild: Pointer to the ARGV. + * @param proplist: The property list + */ +void property_add_to_argv(GPtrArray *argv_ptr, GHashTable *proplist); + +/* Print the argv_ptr with g_debug() + * + * @param argv_ptr: GPtrArray of an array to print. + */ +void debug_executing(GPtrArray *argv_ptr); + #endif /* UTIL_H */