DragonFly On-Line Manual Pages
TLS(2) DragonFly System Calls Manual TLS(2)
NAME
set_tls_area, get_tls_area -- kernel TLS (thread local storage) support
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <sys/tls.h>
int
set_tls_area(int which, struct tls_info *info, size_t infosize);
int
get_tls_area(int which, struct tls_info *info, size_t infosize);
DESCRIPTION
The set_tls_area() system call creates an entry for the TLS facility
which representing thread local storage as specified by the info
structure. A descriptor representing the facility is returned, or -1 if
an error occurred. The facility may be cleared by specifying a NULL
pointer and an infosize of 0. The get_tls_area() system call retrieves
the requested TLS facility. A descriptor representing the facility is
returned, or -1 if an error occurred. If you simply want the descriptor
you may specify a NULL pointer and an infosize of 0.
The returned descriptor and the TLS mechanism is machine-dependent. On
IA32 three global segment descriptors are supported (0, 1, and 2) and
the %gs load value is returned.
The tls_info structure passed to set_tls_area() should first be zerod (to
remain compatible with future extensions) and then initialized.
struct tls_info {
void *base; /* base address of TLS area */
int size; /* size of TLS area in bytes */
};
The actual implementation of the area is machine-dependent. If the
kernel is unable to accommodate the supplied size it may create a larger
area. If the kernel is unable to accommodate the supplied base address
an error will be returned.
RETURN VALUES
A return value of 0 is returned on success, -1 on error.
EXAMPLES
/*
* Pseudo example showing how the TLS system calls work on IA32.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/tls.h>
int X;
static int getdata(int offset);
int
main(int ac, char **av)
{
int i;
int gs;
struct tls_info info;
info.base = &X;
info.size = sizeof(X);
if ((gs = set_tls_area(0, &info, sizeof(info))) < 0) {
perror("setarea");
exit(1);
}
printf("gs = %04x\n", gs);
__asm __volatile("mov %0,%%gs" : : "g" (gs) );
if (get_tls_area(0, &info, sizeof(info)) < 0) {
perror("getarea");
exit(1);
}
printf("%p/%d\n", info.base, info.size);
X = 1;
printf("should be 1: %d\n", getdata(0));
X = 2;
printf("should be 2: %d\n", getdata(0));
printf("this should fault:\n");
fflush(stdout);
getdata(4);
return(0);
}
static int
getdata(int offset)
{
int rv;
__asm __volatile("movl %%gs:(%0),%%eax; movl %%eax,%1" : "+r" (offset) : "m"
(rv) : "ax");
return (rv);
}
ERRORS
[ERANGE] The specified facility index, which, is not supported.
[EINVAL] An invalid parameter has been specified.
[ENOENT] (get_tls_area) The specified facility has not been
initialized with sys_set_tls_area().
SEE ALSO
umtx(2)
HISTORY
The set_tls_area(), and get_tls_area() function calls first appeared in
DragonFly 1.1.
DragonFly 5.5 February 21, 2005 DragonFly 5.5
TLS_READ(3) DragonFly Library Functions Manual TLS_READ(3)
NAME
tls_read, tls_write, tls_handshake, tls_error, tls_close, tls_reset --
use a TLS connection
SYNOPSIS
#include <tls.h>
ssize_t
tls_read(struct tls *ctx, void *buf, size_t buflen);
ssize_t
tls_write(struct tls *ctx, const void *buf, size_t buflen);
int
tls_handshake(struct tls *ctx);
const char *
tls_error(struct tls *ctx);
int
tls_close(struct tls *ctx);
void
tls_reset(struct tls *ctx);
DESCRIPTION
tls_read() reads buflen bytes of data from the socket into buf. It
returns the amount of data read.
tls_write() writes buflen bytes of data from buf to the socket. It
returns the amount of data written.
tls_handshake() explicitly performs the TLS handshake. It is only neces-
sary to call this function if you need to guarantee that the handshake
has completed, as both tls_read() and tls_write() automatically perform
the TLS handshake when necessary.
The tls_error() function may be used to retrieve a string containing more
information about the most recent error relating to a context.
tls_close() closes a connection after use. Only the TLS layer will be
shut down and the caller is responsible for closing the file descriptors,
unless the connection was established using tls_connect(3) or
tls_connect_servername(3). After closing the connection, ctx can be
passed to tls_free(3).
RETURN VALUES
tls_read() and tls_write() return a size on success or -1 on error.
tls_handshake() and tls_close() return 0 on success or -1 on error.
tls_error() returns NULL if no error occurred with ctx during or since
the last call to tls_handshake(), tls_read(), tls_write(), tls_close(),
or tls_reset() involving ctx, or if memory allocation failed while trying
to assemble the string describing the most recent error related to ctx.
The tls_read(), tls_write(), tls_handshake(), and tls_close() functions
have two special return values:
TLS_WANT_POLLIN The underlying read file descriptor needs to be
readable in order to continue.
TLS_WANT_POLLOUT The underlying write file descriptor needs to be
writeable in order to continue.
In the case of blocking file descriptors, the same function call should
be repeated immediately. In the case of non-blocking file descriptors,
the same function call should be repeated when the required condition has
been met.
Callers of these functions cannot rely on the value of the global errno.
To prevent mishandling of error conditions, tls_read(), tls_write(),
tls_handshake(), and tls_close() all explicitly clear errno.
EXAMPLES
The following example demonstrates how to handle TLS writes on a blocking
file descriptor:
...
while (len > 0) {
ssize_t ret;
ret = tls_write(ctx, buf, len);
if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT)
continue;
if (ret < 0)
err(1, "tls_write: %s", tls_error(ctx));
buf += ret;
len -= ret;
}
...
The following example demonstrates how to handle TLS writes on a non-
blocking file descriptor using poll(2):
...
pfd[0].fd = fd;
pfd[0].events = POLLIN|POLLOUT;
while (len > 0) {
nready = poll(pfd, 1, 0);
if (nready == -1)
err(1, "poll");
if ((pfd[0].revents & (POLLERR|POLLNVAL)))
errx(1, "bad fd %d", pfd[0].fd);
if ((pfd[0].revents & (pfd[0].events|POLLHUP))) {
ssize_t ret;
ret = tls_write(ctx, buf, len);
if (ret == TLS_WANT_POLLIN)
pfd[0].events = POLLIN;
else if (ret == TLS_WANT_POLLOUT)
pfd[0].events = POLLOUT;
else if (ret < 0)
err(1, "tls_write: %s", tls_error(ctx));
else {
buf += ret;
len -= ret;
}
}
}
...
SEE ALSO
tls_accept_socket(3), tls_configure(3), tls_conn_version(3),
tls_connect(3), tls_init(3), tls_ocsp_process_response(3)
HISTORY
tls_read(), tls_write(), tls_error(), tls_close(), and tls_reset()
appeared in OpenBSD 5.6 and got their final names in OpenBSD 5.7.
tls_handshake() appeared in OpenBSD 5.9.
AUTHORS
Joel Sing <jsing@openbsd.org> with contributions from
Bob Beck <beck@openbsd.org>
CAVEATS
The function tls_error() returns an internal pointer. It must not be
freed by the application, or a double free error will occur. The pointer
will become invalid when the next error occurs with ctx. Consequently,
if the application may need the message at a later time, it has to copy
the string before calling the next libtls function involving ctx, or a
segmentation fault or read access to unintended data is the likely
result.
DragonFly 5.5 February 20, 2017 DragonFly 5.5