diff options
-rw-r--r-- | src/libutil/util.c | 33 | ||||
-rw-r--r-- | src/libutil/util.h | 7 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/libutil/util.c b/src/libutil/util.c index 212cb7f1b..19953194c 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -1894,6 +1894,39 @@ rspamd_random_hex (guchar *buf, guint64 len) } } +gint +rspamd_shmem_mkstemp (gchar *pattern) +{ + gint fd = -1; + gchar *nbuf, *xpos; + gsize blen; + + xpos = strchr (pattern, 'X'); + + if (xpos == NULL) { + errno = EINVAL; + return -1; + } + + blen = strlen (pattern); + nbuf = g_malloc (blen + 1); + rspamd_strlcpy (nbuf, pattern, blen + 1); + xpos = nbuf + (xpos - pattern); + + for (;;) { + rspamd_random_hex (xpos, blen - (xpos - nbuf)); + + fd = shm_open (nbuf, O_RDWR | O_EXCL | O_CREAT, 0600); + + if (fd != -1) { + rspamd_strlcpy (pattern, nbuf, blen + 1); + } + } + + g_free (nbuf); + + return fd; +} void rspamd_ptr_array_free_hard (gpointer p) diff --git a/src/libutil/util.h b/src/libutil/util.h index 5c0e82a0d..0c293ccbe 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -412,6 +412,13 @@ guint64 rspamd_hash_seed (void); void rspamd_random_hex (guchar *buf, guint64 len); /** + * Returns + * @param pattern pattern to create (should end with some number of X symbols), modified by this function + * @return + */ +gint rspamd_shmem_mkstemp (gchar *pattern); + +/** * Return jittered time value */ gdouble rspamd_time_jitter (gdouble in, gdouble jitter); |