File: errors.c

package info (click to toggle)
libnbd 1.22.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,636 kB
  • sloc: ansic: 53,855; ml: 12,311; sh: 8,499; python: 4,595; makefile: 2,902; perl: 165; cpp: 24
file content (168 lines) | stat: -rw-r--r-- 4,536 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* NBD client library in userspace
 * Copyright Red Hat
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include <pthread.h>

#include "internal.h"

struct last_error {
  const char *context;         /* Current function context. */
  char *error;                 /* Error message string. */
  int errnum;                  /* errno value (0 if not available). */
};

/* Thread-local storage of the last error. */
static pthread_key_t errors_key;

static void free_errors_key (void *vp) LIBNBD_ATTRIBUTE_NONNULL (1);

/* Create the thread-local key when the library is loaded. */
static void errors_key_create (void) __attribute__ ((constructor));

static void
errors_key_create (void)
{
  int err;

  err = pthread_key_create (&errors_key, free_errors_key);
  if (err != 0) {
    fprintf (stderr, "%s: %s: %s\n", "libnbd", "pthread_key_create",
             strerror (err));
    abort ();
  }
}

/* Destroy the thread-local key when the library is unloaded. */
static void errors_key_destroy (void) __attribute__ ((destructor));

static void
errors_key_destroy (void)
{
  struct last_error *last_error = pthread_getspecific (errors_key);

  /* "No destructor functions shall be invoked by
   * pthread_key_delete().  Any destructor function that may have been
   * associated with key shall no longer be called upon thread exit."
   */
  if (last_error != NULL) {
    free (last_error->error);
    free (last_error);
  }

  /* We could do this, but that causes a race condition described here:
   * https://listman.redhat.com/archives/libguestfs/2023-March/031002.html
   */
  //pthread_key_delete (errors_key);
}

/* This is called when a thread exits, to free the thread-local data
 * for that thread.  Note that vp != NULL, guaranteed by pthread.
 */
static void
free_errors_key (void *vp)
{
  struct last_error *last_error = vp;

  free (last_error->error);
  free (last_error);
}

static struct last_error *
allocate_last_error_on_demand (void)
{
  struct last_error *last_error = pthread_getspecific (errors_key);

  if (!last_error) {
    last_error = calloc (1, sizeof *last_error);
    if (last_error) {
      int err = pthread_setspecific (errors_key, last_error);
      if (err != 0) {
        /* This is not supposed to happen (XXX). */
        fprintf (stderr, "%s: %s: %s\n", "libnbd", "pthread_setspecific",
                 strerror (err));
      }
    }
  }
  return last_error;
}

/* Called on entry to any API function that can call an error function
 * (see generator "may_set_error") to reset the error context.  The
 * 'context' parameter is the name of the function.
 */
void
nbd_internal_set_error_context (const char *context)
{
  struct last_error *last_error = allocate_last_error_on_demand ();

  if (!last_error)
    return;
  last_error->context = context;
}

void
nbd_internal_set_last_error (int errnum, char *error)
{
  struct last_error *last_error = allocate_last_error_on_demand ();

  if (!last_error) {
    /* At least we shouldn't lose the error. */
    perror ("nbd_internal_set_last_error: calloc");
    fprintf (stderr, "nbd_internal_set_last_error: lost error: %s (%d)\n",
             error, errnum);
    return;
  }

  free (last_error->error);
  last_error->error = error;
  last_error->errnum = errnum;
}

const char *
nbd_internal_get_error_context (void)
{
  struct last_error *last_error = allocate_last_error_on_demand ();

  return last_error ? last_error->context : NULL;
}

const char *
nbd_get_error (void)
{
  struct last_error *last_error = pthread_getspecific (errors_key);

  if (!last_error)
    return NULL;
  return last_error->error;
}

int
nbd_get_errno (void)
{
  struct last_error *last_error = pthread_getspecific (errors_key);

  if (!last_error)
    return 0;
  return last_error->errnum;
}