IRIX: Implement makesname()
authorChaz Chandler <clc31@inbox.com>
Mon, 14 Jun 2010 04:21:50 +0000 (00:21 -0400)
committerDerrick Brashear <shadow@dementia.org>
Wed, 16 Jun 2010 13:12:42 +0000 (06:12 -0700)
makesname() make a semaphore name for use in <sys/sema.h>-type routines on
IRIX.  It takes as input a pointer to an allocated string buffer (sname) of
size METER_NAMSZ, a string prefix (prefix), and a vnode number (v_number).
When complete, sname is returned, pointing to the beginning of a
NULL-terminated string containing the new name, with a maximum of
(METER_NAMSZ-1) characters plus the NULL.  The name is a concatenation of
the string at 'prefix' and the ASCII representation of the number in
'v_number'.

Note: Due to IRIX's use of uint64 to represent vnumber_t and a maximum
semaphore name length of 15 (METER_NAMSZ-1), this function cannot be
guaranteed to produce a name which uniquely describes a vnode.

makesname() is already called from afs_vcache.c but not (or no longer)
available / implemented elsewhere.

Change-Id: I4331c161b68b39a4c067691c97363b637d13ff15
Change-Id: Ia0a615426dc05e01a98e53e89ec3bae3726cac34
Reviewed-on: http://gerrit.openafs.org/2155
Reviewed-by: Chaz Chandler <clc31@inbox.com>
Tested-by: Chaz Chandler <clc31@inbox.com>
Reviewed-by: Andrew Deason <adeason@sinenomine.net>
Reviewed-by: Derrick Brashear <shadow@dementia.org>

src/afs/IRIX/osi_misc.c

index fb65c48..807e6d5 100644 (file)
@@ -30,6 +30,63 @@ afs_mpservice(void *a)
 {
 }
 
+/*!
+ * make a semaphore name for use in <sys/sema.h>-type routines on
+ * IRIX
+ *
+ * \param[out] sname           will contain the semaphore name and
+ *                             should point to an allocated string
+ *                             buffer of size METER_NAMSZ
+ * \param[in]  prefix          string with which to start the name
+ * \param[in]  v_number        vnode number to complete the name
+ *
+ * \post sname will point to the beginning of a NULL-terminated
+ *       string to be used as a semaphore name with a maximum of
+ *       (METER_NAMSZ-1) characters plus the NULL.  The name is a
+ *       concatenation of the string at 'prefix' and the ASCII
+ *       representation of the number in 'v_number'.  sname is
+ *       returned.
+ *
+ * \note Due to IRIX's use of uint64_t to represent vnumber_t and a
+ *       maximum semaphore name length of 15 (METER_NAMSZ-1), this
+ *       function cannot be guaranteed to produce a name which
+ *       uniquely describes a vnode.
+ *
+ */
+char *
+makesname(char *sname, const char *prefix, vnumber_t v_number)
+{
+    char vnbuf[21]; /* max number of uint64 decimal digits + 1 */
+    size_t prlen, vnlen;
+
+    if (sname) {
+       /*
+        * Note: IRIX doesn't have realloc() available in the
+        * kernel, so the openafs util implementation of snprintf is
+        * not usable.  What follows is intended to reproduce the
+        * behavior of:
+        *      snprintf(sname, METER_NAMSZ, "%s%llu", prefix,
+        *               (unsigned long long)v_number);
+        * Additionally, the kernel only provides a void sprintf(),
+        * making length checking slightly more difficult.
+        */
+       prlen = strlen(prefix);
+       if (prlen > METER_NAMSZ-1)
+           prlen = METER_NAMSZ-1;
+       strncpy(sname, prefix, prlen);
+
+       memset(vnbuf, 0, sizeof(vnbuf));
+       sprintf(vnbuf, "%llu", (unsigned long long)v_number);
+       vnlen = strlen(vnbuf);
+       if (vnlen+prlen > METER_NAMSZ-1)
+           vnlen = METER_NAMSZ-1-prlen;
+       if (vnlen > 0)
+           strncpy(&(sname[prlen]), vnbuf, vnlen);
+       sname[vnlen+prlen] = '\0';
+    }
+    return sname;
+}
+
 #ifdef AFS_SGI_VNODE_GLUE
 #include <sys/invent.h>
 extern mutex_t afs_init_kern_lock;