From: Zbigniew Jędrzejewski-Szmek Date: Wed, 31 Mar 2021 09:46:31 +0000 (+0200) Subject: man: split the description of sd_bus_error_set() X-Git-Tag: v249-rc1~490^2~11 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=af46237ea1ba0d500ec2c8fea2e8501484491f60;p=thirdparty%2Fsystemd.git man: split the description of sd_bus_error_set() It was one giant all of text in pseudo-random order. Let's split it into paragraphs talk about one subject each. And unfortunately, the description of what happens when the error is not set was not correct. In general, various functions treat 0/NULL as not-an-error, and return 0. --- diff --git a/man/sd_bus_error.xml b/man/sd_bus_error.xml index bfc5caf4e24..bb8015de5ee 100644 --- a/man/sd_bus_error.xml +++ b/man/sd_bus_error.xml @@ -147,55 +147,54 @@ Description - The sd_bus_error structure carries - information about a D-Bus error condition. The functions described - below may be used to set and query fields in this structure. The - name field contains a short identifier - of an error. It should follow the rules for error names described - in the D-Bus specification, subsection Valid - Names. A number of common, standardized error names are - described in - sd-bus-errors3, - but additional domain-specific errors may be defined by - applications. The message field usually - contains a human-readable string describing the details, but might - be NULL. An unset sd_bus_error structure - should have both fields initialized to NULL. Set an error - structure to SD_BUS_ERROR_NULL in order to - reset both fields to NULL. When no longer necessary, resources - held by the sd_bus_error structure should - be destroyed with sd_bus_error_free(). - - sd_bus_error_set() sets an error - structure to the specified name and message strings. The strings - will be copied into internal, newly allocated memory. It is - essential to free the error structure again when it is not - required anymore (see above). The function will return an - errno-like negative value (see The sd_bus_error structure carries information about a D-Bus error + condition, or lack thereof. The functions described below may be used to set and query fields in this + structure. + + The name field contains a short identifier of an error. It + should follow the rules for error names described in the D-Bus specification, subsection Valid + Names. A number of common, standardized error names are described in + sd-bus-errors3, but + additional domain-specific errors may be defined by applications. + + The message field usually contains a human-readable string + describing the details, but might be NULL. + + An unset sd_bus_error structure should have both fields initialized to + NULL, and signifies lack of an error, i.e. success. Assign + SD_BUS_ERROR_NULL to the structure in order to initialize both fields to + NULL. When no longer necessary, resources held by the + sd_bus_error structure should be destroyed with + sd_bus_error_free(). + + sd_bus_error_set() sets an error structure to the specified name and message + strings. The strings will be copied into internal, newly allocated memory. It is essential to free the + contents again when they are not required anymore (see above). Do not use this call on error structures + that have already been set. If you intend to reuse an error structure, free the old data stored in it + with sd_bus_error_free() first. + + sd_bus_error_set() will return an errno-like value (see + errno3) - determined from the specified error name. Various well-known - D-Bus errors are converted to well-known errno - counterparts, and the other ones to -EIO. See - sd-bus-errors3 - for a list of well-known error names. Additional error mappings - may be defined with - sd_bus_error_add_map3. If - e is NULL, no error structure is initialized, - but the error is still converted into an - errno-style error. If - name is NULL, it is - assumed that no error occurred, and 0 is returned. This means that - this function may be conveniently used in a - return statement. If - message is NULL, no message is set. This - call can fail if no memory may be allocated for the name and - message strings, in which case an - SD_BUS_ERROR_NO_MEMORY error might be set - instead and -ENOMEM be returned. Do not use this call on error - structures that are already initialized. If you intend to reuse an - error structure, free the old data stored in it with - sd_bus_error_free() first. + determined from the specified error name name. If name is + NULL, it is assumed that no error occurred, and 0 is returned. + If name is nonnull, a negative value is always returned. If + e is NULL, no error structure is initialized, but + name is still converted into an errno-style value. + + Various well-known D-Bus errors are converted to well-known errno counterparts, + and the other ones to -EIO. See + sd-bus-errors3 for a + list of well-known error names. Additional error mappings may be defined with + sd_bus_error_add_map3. + + + sd_bus_error_set() is designed to be conveniently used in a + return statement. If message is NULL, no + message is set. This call can fail if no memory may be allocated for the name and message strings, in + which case an SD_BUS_ERROR_NO_MEMORY error will be set instead and + -ENOMEM returned. sd_bus_error_setf() is similar to sd_bus_error_set(), but takes a message field. sd_bus_error_set_const() is similar to - sd_bus_error_set(), but the string parameters - are not copied internally, and must hence remain constant and - valid for the lifetime of e. Use this call - to avoid memory allocations when setting error structures. Since - this call does not allocate memory, it will not fail with an - out-of-memory condition as - sd_bus_error_set() can, as described - above. Alternatively, the - SD_BUS_ERROR_MAKE_CONST() macro may be used - to generate a literal, constant bus error structure - on-the-fly. - - sd_bus_error_set_errno() will set - name from an - errno-like value that is converted to a D-Bus + sd_bus_error_set(), but the string parameters are not copied internally, and must + hence remain constant and valid for the lifetime of e. Use this call to avoid + memory allocations when setting error structures. Since this call does not allocate memory, it will not + fail with an out-of-memory condition as sd_bus_error_set() may, as described + above. Alternatively, the SD_BUS_ERROR_MAKE_CONST() macro may be used to generate a + literal, constant bus error structure on-the-fly. + + sd_bus_error_set_errno() will immediately return 0 if the + specified error parameter error is 0. Otherwise, it will set + name from an errno-like value that is converted to a D-Bus error. strerror_r3 - will be used to set message. Well-known - D-Bus error names will be used for name - if applicable, otherwise a name in the - System.Error. namespace will be generated. The - sign of the specified error number is ignored. The absolute value - is used implicitly. The call always returns a negative value, for - convenient usage in return statements. This - call might fail due to lack of memory, in which case an - SD_BUS_ERROR_NO_MEMORY error is set instead, - and -ENOMEM is returned. + project='die-net'>strerror_r3 will + be used to set message. Well-known D-Bus error names will be used for + name if applicable, otherwise a name in the System.Error. + namespace will be generated. The sign of the specified error number is ignored and the absolute value is + used implicitly. If the specified error error is non-zero, the call always returns + a negative value, for convenient usage in return statements. This call might fail + due to lack of memory, in which case an SD_BUS_ERROR_NO_MEMORY error is set instead, + and -ENOMEM is returned. sd_bus_error_set_errnof() is similar to sd_bus_error_set_errno(), but in addition to @@ -246,28 +237,26 @@ project='man-pages'>va_arg3 parameter list. - sd_bus_error_get_errno() converts the - name field of an error structure to an - errno-like (positive) value using the same - rules as sd_bus_error_set(). If - e is NULL, 0 will be - returned. - - sd_bus_error_copy() will initialize - dst using the values in - e. If the strings in - e were set using - sd_bus_error_set_const(), they will be shared. - Otherwise, they will be copied. Returns a converted - errno-like, negative error code. - - sd_bus_error_move() is similar to sd_bus_error_copy(), but will - move any error information from e into dst, resetting the - former. This function cannot fail, as no new memory is allocated. Note that if e is not set - (or NULL) dst is initializated to - SD_BUS_ERROR_NULL. Moreover, if dst is NULL no - operation is executed on it and resources held by e are freed and reset. Returns a - converted errno-like, negative error code. + sd_bus_error_get_errno() converts the name field of + an error structure to an errno-like (positive) value using the same rules as + sd_bus_error_set(). If e is NULL, + 0 will be returned. + + sd_bus_error_copy() will initialize dst using the + values in e, if e has been set with an error value before. + Otherwise, it will return immediately. If the strings in e were set using + sd_bus_error_set_const(), they will be shared. Otherwise, they will be + copied. Returns a converted errno-like, negative error code or 0. + Before this call, dst must be unset, i.e. either freshly initialized with + NULL or reset using sd_bus_error_free(). + + sd_bus_error_move() is similar to sd_bus_error_copy(), + but will move any error information from e into dst, + resetting the former. This function cannot fail, as no new memory is allocated. Note that if + e is not set, dst is initializated to + SD_BUS_ERROR_NULL. Moreover, if dst is + NULL no operation is executed on it and resources held by e + are freed and reset. Returns a converted errno-like, non-positive error value. sd_bus_error_is_set() will return a non-zero value if e is @@ -300,25 +289,23 @@ Return Value - The functions sd_bus_error_set(), - sd_bus_error_setf(), and - sd_bus_error_set_const(), when successful, - return the negative errno value corresponding to the - name parameter. The functions - sd_bus_error_set_errno(), - sd_bus_error_set_errnof() and - sd_bus_error_set_errnofv(), when successful, - return the negative value of the error - parameter. If an error occurs, one of the negative error values - listed below will be returned. + The functions sd_bus_error_set(), sd_bus_error_setf(), + and sd_bus_error_set_const() always return 0 when the specified + error value is NULL, and a negative errno-like value corresponding to the + name parameter otherwise. The functions + sd_bus_error_set_errno(), sd_bus_error_set_errnof() and + sd_bus_error_set_errnofv(), return 0 when the specified error + value is 0, and a a negative errno-like value corresponding to the + error parameter otherwise. If an error occurs internally, one of the negative + error values listed below will be returned. sd_bus_error_get_errno() returns false when e is NULL, and a positive errno value mapped from e->name otherwise. - sd_bus_error_copy() and sd_bus_error_move() return 0 or a positive - integer on success, and a negative error value converted from the error name otherwise. + sd_bus_error_copy() and sd_bus_error_move() return a + negative error value converted from the source error, and zero if the error has not been set. sd_bus_error_is_set() returns a non-zero value when e and the diff --git a/src/libsystemd/sd-bus/bus-error.c b/src/libsystemd/sd-bus/bus-error.c index df292fe3d59..7747600b837 100644 --- a/src/libsystemd/sd-bus/bus-error.c +++ b/src/libsystemd/sd-bus/bus-error.c @@ -465,7 +465,7 @@ _public_ int sd_bus_error_set_errno(sd_bus_error *e, int error) { if (!e) return -error; if (error == 0) - return -error; + return 0; assert_return(!bus_error_is_dirty(e), -EINVAL);