]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/test_uuidd.c
Merge branch 'setterm' of https://github.com/jwilk-forks/util-linux
[thirdparty/util-linux.git] / misc-utils / test_uuidd.c
index 78399b91f00124e53f6f80d2a85467146692c281..3f38fa4306c861433eaa0ac7fd6e39dcd8d13d72 100644 (file)
  *
  *     uuidd --debug --no-fork --no-pid --socket /run/uuidd/request
  *
- * if the $localstatedir (as defined by build-system) is /run. If you want
+ * if the $runstatedir (as defined by build-system) is /run. If you want
  * to overwrite the built-in default then use:
  *
- *     make uuidd uuidgen localstatedir=/var
+ *     make uuidd uuidgen runstatedir=/var/run
  */
-#include <error.h>
 #include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "c.h"
 #include "xalloc.h"
 #include "strutils.h"
+#include "nls.h"
 
 #define LOG(level,args) if (loglev >= level) { fprintf args; }
 
-size_t nprocesses = 4;
-size_t nthreads = 4;
-size_t nobjects = 4096;
-size_t loglev = 1;
+static size_t nprocesses = 4;
+static size_t nthreads = 4;
+static size_t nobjects = 4096;
+static size_t loglev = 1;
 
 struct processentry {
        pid_t           pid;
@@ -73,17 +73,17 @@ static int shmem_id;
 static object_t *objects;
 
 
-static void __attribute__((__noreturn__)) usage(FILE *out)
+static void __attribute__((__noreturn__)) usage(void)
 {
-       fprintf(out, "\n %s [options]\n", program_invocation_short_name);
+       printf("\n %s [options]\n", program_invocation_short_name);
 
-       fprintf(out, "  -p <num>     number of of nprocesses (default:%zu)\n", nprocesses);
-       fprintf(out, "  -t <num>     number of nthreads (default:%zu)\n", nthreads);
-       fprintf(out, "  -o <num>     number of nobjects (default:%zu)\n", nobjects);
-       fprintf(out, "  -l <level>   log level (default:%zu)\n", loglev);
-       fprintf(out, "  -h           display help\n");
+       printf("  -p <num>     number of nprocesses (default:%zu)\n", nprocesses);
+       printf("  -t <num>     number of nthreads (default:%zu)\n", nthreads);
+       printf("  -o <num>     number of nobjects (default:%zu)\n", nobjects);
+       printf("  -l <level>   log level (default:%zu)\n", loglev);
+       printf("  -h           display help\n");
 
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       exit(EXIT_SUCCESS);
 }
 
 static void allocate_segment(int *id, void **address, size_t number, size_t size)
@@ -143,9 +143,9 @@ static void *create_uuids(thread_t *th)
                object_uuid_create(obj);
                obj->tid = th->tid;
                obj->pid = th->proc->pid;
-               obj->idx = th->index + i;;
+               obj->idx = th->index + i;
        }
-       return 0;
+       return NULL;
 }
 
 static void *thread_body(void *arg)
@@ -161,14 +161,15 @@ static void create_nthreads(process_t *proc, size_t index)
        size_t i, ncreated = 0;
        int rc;
 
-       threads = (thread_t *) xcalloc(nthreads, sizeof(thread_t));
+       threads = xcalloc(nthreads, sizeof(thread_t));
 
        for (i = 0; i < nthreads; i++) {
                thread_t *th = &threads[i];
 
                rc = pthread_attr_init(&th->thread_attr);
                if (rc) {
-                       error(0, rc, "%d: pthread_attr_init failed", proc->pid);
+                       errno = rc;
+                       warn("%d: pthread_attr_init failed", proc->pid);
                        break;
                }
 
@@ -177,18 +178,19 @@ static void create_nthreads(process_t *proc, size_t index)
                rc = pthread_create(&th->tid, &th->thread_attr, &thread_body, th);
 
                if (rc) {
-                       error(0, rc, "%d: pthread_create failed", proc->pid);
+                       errno = rc;
+                       warn("%d: pthread_create failed", proc->pid);
                        break;
                }
 
-               LOG(2, (stderr, "%d: started thread [tid=%d,index=%zu]\n",
-                    proc->pid, (int) th->tid, th->index));
+               LOG(2, (stderr, "%d: started thread [tid=%jd,index=%zu]\n",
+                    proc->pid, (intmax_t) th->tid, th->index));
                index += nobjects;
                ncreated++;
        }
 
        if (ncreated != nthreads)
-               fprintf(stderr, "%d: %zu threads not craeted and ~%zu objects will be ignored\n",
+               fprintf(stderr, "%d: %zu threads not created and ~%zu objects will be ignored\n",
                                proc->pid, nthreads - ncreated,
                                (nthreads - ncreated) * nobjects);
 
@@ -196,11 +198,13 @@ static void create_nthreads(process_t *proc, size_t index)
                thread_t *th = &threads[i];
 
                rc = pthread_join(th->tid, (void *) &th->retval);
-               if (rc)
-                       error(EXIT_FAILURE, rc, "pthread_join failed");
+               if (rc) {
+                       errno = rc;
+                       err(EXIT_FAILURE, "pthread_join failed");
+               }
 
-               LOG(2, (stderr, "%d: thread exited [tid=%d,return=%d]\n",
-                    proc->pid, (int) th->tid, th->retval));
+               LOG(2, (stderr, "%d: thread exited [tid=%jd,return=%d]\n",
+                    proc->pid, (intmax_t) th->tid, th->retval));
        }
 }
 
@@ -209,7 +213,7 @@ static void create_nprocesses(void)
        process_t *process;
        size_t i;
 
-       process = (process_t *) xcalloc(nprocesses, sizeof(process_t));
+       process = xcalloc(nprocesses, sizeof(process_t));
 
        for (i = 0; i < nprocesses; i++) {
                process_t *proc = &process[i];
@@ -243,7 +247,7 @@ static void create_nprocesses(void)
 
 static void object_dump(size_t idx, object_t *obj)
 {
-       char uuid_string[37], *p;
+       char uuid_string[UUID_STR_LEN], *p;
 
        p = uuid_string;
        object_uuid_to_string(obj, &p);
@@ -252,10 +256,12 @@ static void object_dump(size_t idx, object_t *obj)
        fprintf(stderr, "  uuid:    <%s>\n", p);
        fprintf(stderr, "  idx:     %zu\n", obj->idx);
        fprintf(stderr, "  process: %d\n", (int) obj->pid);
-       fprintf(stderr, "  thread:  %d\n", (int) obj->tid);
+       fprintf(stderr, "  thread:  %jd\n", (intmax_t) obj->tid);
        fprintf(stderr, "}\n");
 }
 
+#define MSG_TRY_HELP "Try '-h' for help."
+
 int main(int argc, char *argv[])
 {
        size_t i, nfailed = 0, nignored = 0;
@@ -276,16 +282,16 @@ int main(int argc, char *argv[])
                        loglev = strtou32_or_err(optarg, "invalid log level argument");
                        break;
                case 'h':
-                       usage(stdout);
+                       usage();
                        break;
                default:
-                       usage(stderr);
-                       break;
+                       fprintf(stderr, MSG_TRY_HELP);
+                       exit(EXIT_FAILURE);
                }
        }
 
        if (optind != argc)
-               usage(stderr);
+               errx(EXIT_FAILURE, "bad usage\n" MSG_TRY_HELP);
 
        if (loglev == 1)
                fprintf(stderr, "requested: %zu processes, %zu threads, %zu objects per thread (%zu objects = %zu bytes)\n",