13 #include "gcc-compat.h"
17 __noreturn
void clean_exit(int status
);
20 * Write a message to a dynamically allocated string.
22 * \param fmt Usual format string.
23 * \param p Result pointer.
26 #define VSPRINTF(fmt, p) \
30 p = dss_malloc(size); \
33 /* Try to print in the allocated space. */ \
35 n = vsnprintf(p, size, fmt, ap); \
37 /* If that worked, return the string. */ \
38 if (n > -1 && n < size) \
40 /* Else try again with more space. */ \
41 if (n > -1) /* glibc 2.1 */ \
42 size = n + 1; /* precisely what is needed */ \
43 else /* glibc 2.0 */ \
44 size *= 2; /* twice the old size */ \
45 p = dss_realloc(p, size); \
50 * dss' version of realloc().
52 * \param p Pointer to the memory block, may be \p NULL.
53 * \param size The desired new size.
55 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
56 * i.e. there is no need to check the return value in the caller.
58 * \return A pointer to the newly allocated memory, which is suitably aligned
59 * for any kind of variable and may be different from \a p.
63 __must_check __malloc
void *dss_realloc(void *p
, size_t size
)
66 * No need to check for NULL pointers: If p is NULL, the call
67 * to realloc is equivalent to malloc(size)
70 if (!(p
= realloc(p
, size
))) {
71 DSS_EMERG_LOG("realloc failed (size = %zu), aborting\n",
73 clean_exit(EXIT_FAILURE
);
79 * dss' version of malloc().
81 * \param size The desired new size.
83 * A wrapper for malloc(3) which exits on errors.
85 * \return A pointer to the allocated memory, which is suitably aligned for any
90 __must_check __malloc
void *dss_malloc(size_t size
)
93 void *p
= malloc(size
);
96 DSS_EMERG_LOG("malloc failed (size = %zu), aborting\n",
98 clean_exit(EXIT_FAILURE
);
104 * dss' version of calloc().
106 * \param size The desired new size.
108 * A wrapper for calloc(3) which exits on errors.
110 * \return A pointer to the allocated and zeroed-out memory, which is suitably
111 * aligned for any kind of variable.
115 __must_check __malloc
void *dss_calloc(size_t size
)
117 void *ret
= dss_malloc(size
);
119 memset(ret
, 0, size
);
124 * dss' version of strdup().
126 * \param s The string to be duplicated.
128 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
129 * there is no need to check the return value in the caller.
131 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
132 * an pointer to an empty string is returned.
137 __must_check __malloc
char *dss_strdup(const char *s
)
141 if ((ret
= strdup(s
? s
: "")))
143 DSS_EMERG_LOG("strdup failed, aborting\n");
144 clean_exit(EXIT_FAILURE
);
148 * Allocate a sufficiently large string and print into it.
150 * \param fmt A usual format string.
152 * Produce output according to \p fmt. No artificial bound on the length of the
153 * resulting string is imposed.
155 * \return This function either returns a pointer to a string that must be
156 * freed by the caller or aborts without returning.
160 __must_check __printf_1_2 __malloc
char *make_message(const char *fmt
, ...)
168 __printf_1_2
void make_err_msg(const char* fmt
,...)
171 VSPRINTF(fmt
, dss_error_txt
);
175 * Get the home directory of the current user.
177 * \return A dynammically allocated string that must be freed by the caller. If
178 * the home directory could not be found, this function returns "/tmp".
180 __must_check __malloc
char *get_homedir(void)
182 struct passwd
*pw
= getpwuid(getuid());
183 return dss_strdup(pw
? pw
->pw_dir
: "/tmp");
186 /** \cond LLONG_MAX and LLONG_LIN might not be defined. */
188 #define LLONG_MAX (1 << (sizeof(long) - 1))
191 #define LLONG_MIN (-LLONG_MAX - 1LL)
196 * Convert a string to a 64-bit signed integer value.
198 * \param str The string to be converted.
199 * \param value Result pointer.
203 * \sa strtol(3), atoi(3).
205 int dss_atoi64(const char *str
, int64_t *value
)
210 errno
= 0; /* To distinguish success/failure after call */
211 tmp
= strtoll(str
, &endptr
, 10);
212 if (errno
== ERANGE
&& (tmp
== LLONG_MAX
|| tmp
== LLONG_MIN
)) {
213 make_err_msg("%s", str
);
214 return -E_ATOI_OVERFLOW
;
216 if (errno
!= 0 && tmp
== 0) { /* other error */
217 make_err_msg("%s", str
);
221 make_err_msg("%s", str
);
222 return -E_ATOI_NO_DIGITS
;
224 if (*endptr
!= '\0') { /* Further characters after number */
225 make_err_msg("%s", str
);
226 return -E_ATOI_JUNK_AT_END
;
233 * Get the logname of the current user.
235 * \return A dynammically allocated string that must be freed by the caller. On
236 * errors, the string "unknown user" is returned, i.e. this function never
241 __must_check __malloc
char *dss_logname(void)
243 struct passwd
*pw
= getpwuid(getuid());
244 return dss_strdup(pw
? pw
->pw_name
: "unknown_user");