From: Vsevolod Stakhov Date: Fri, 10 Jun 2016 08:48:20 +0000 (+0100) Subject: [Feature] Add function to create temporary shared memory mapping X-Git-Tag: 1.3.0~382 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d910528bbdb056c17ed988e9824bd29ddbc98a40;p=thirdparty%2Frspamd.git [Feature] Add function to create temporary shared memory mapping --- diff --git a/src/libutil/util.c b/src/libutil/util.c index 212cb7f1b4..19953194c2 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 5c0e82a0d4..0c293ccbea 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -411,6 +411,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 */