Skip to content

Commit

Permalink
use plasma project code attributes for portability
Browse files Browse the repository at this point in the history
  • Loading branch information
gstrauss committed Nov 30, 2013
1 parent 9b2576d commit 1e301ab
Show file tree
Hide file tree
Showing 25 changed files with 311 additions and 182 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "plasma"]
path = plasma
url = https://github.com/gstrauss/plasma.git
20 changes: 20 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plasma/ git submodule: https://github.com/gstrauss/plasma.git
(see plasma/CREDITS for additional credits)

/* Copyright (c) 2013, Glue Logic LLC. All rights reserved. code()gluelogic.com
*
* This file is part of plasma.
*
* plasma 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.1 of the License, or
* (at your option) any later version.
*
* plasma 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 plasma. If not, see <http://www.gnu.org/licenses/>.
*/
14 changes: 11 additions & 3 deletions bsock/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ $(bsock_sobjs): CFLAGS+=-fpic
bsock_preload.o: CFLAGS+=-fpic

PTHREAD_FLAGS?=-pthread -D_THREAD_SAFE
%.o: CFLAGS+=-std=c99 -D_XOPEN_SOURCE=600 $(PTHREAD_FLAGS) -DNDEBUG -I.
%.o: CFLAGS+=-std=c99 -D_XOPEN_SOURCE=600 $(PTHREAD_FLAGS) -DNDEBUG -I. -I..
%.o: %.c
$(CC) -o $@ $(CFLAGS) -c $<

Expand Down Expand Up @@ -135,7 +135,7 @@ bsock.t: bsock.t.o libbsock.so
$(CC) -o $@ $(RPATH) $(LDFLAGS) $^

# (Note: not currently installing libbsock.so with any version suffix)
.PHONY: install install-suid install-headers install-doc
.PHONY: install install-suid install-headers install-headers-plasma install-doc
BSOCK_MODE=0550
install-suid: BSOCK_MODE=4550
install-suid: install ;
Expand All @@ -150,9 +150,14 @@ install: bsock libbsock.so libbsock_preload.so
(/bin/touch $(BSOCK_CONFIG) && \
/bin/chmod 0644 $(BSOCK_CONFIG))
install-headers: bsock_addrinfo.h bsock_bind.h bsock_unix.h \
bsock_daemon.h bsock_syslog.h
bsock_daemon.h bsock_syslog.h | install-headers-plasma
/bin/mkdir -p -m 0755 $(PREFIX)/include/bsock
/usr/bin/install -m 0444 -p $^ $(PREFIX)/include/bsock/
install-headers-plasma: ../plasma/plasma_attr.h \
../plasma/plasma_feature.h \
../plasma/plasma_stdtypes.h
/bin/mkdir -p -m 0755 $(PREFIX)/include/bsock/plasma
/usr/bin/install -m 0444 -p $^ $(PREFIX)/include/bsock/plasma/
install-doc: CHANGELOG COPYING FAQ INSTALL NOTES README
/bin/mkdir -p -m 0755 $(BSOCK_DOC_DIR)
/usr/bin/install -m 0444 -p $^ $(BSOCK_DOC_DIR)
Expand All @@ -173,3 +178,6 @@ bsock_daemon.o: bsock_daemon.h bsock_syslog.h bsock_unix.h
bsock_resvaddr.o: bsock_addrinfo.h bsock_resvaddr.h bsock_syslog.h
bsock_unix.o: bsock_unix.h
bsock_syslog.o: bsock_syslog.h
%.o: ../plasma/plasma_attr.h \
../plasma/plasma_feature.h \
../plasma/plasma_stdtypes.h
57 changes: 39 additions & 18 deletions bsock/bsock.m.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <plasma/plasma_attr.h>
#include <plasma/plasma_stdtypes.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
Expand All @@ -37,7 +40,6 @@
#include <netdb.h>
#include <sched.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -112,19 +114,21 @@
/* nointr_close() - make effort to avoid leaking open file descriptors */
static int
nointr_close (const int fd)
{ int r; do { r = close(fd); } while (r != 0 && errno == EINTR); return r; }
{ int r; retry_eintr_do_while(r = close(fd), r != 0); return r; }

static int __attribute__((noinline))
__attribute_noinline__
static int
retry_poll_fd (const int fd, const short events, const int timeout)
{
struct pollfd pfd = { .fd = fd, .events = events, .revents = 0 };
int n; /*EINTR results in retrying poll with same timeout again and again*/
do { n = poll(&pfd, 1, timeout); } while (-1 == n && errno == EINTR);
retry_eintr_do_while(n = poll(&pfd, 1, timeout), -1 == n);
if (0 == n) errno = ETIME; /* specific for bsock; not generic */
return n;
}

static void __attribute__((nonnull))
__attribute_nonnull__
static void
bsock_cleanup_close (void * const arg)
{
const int fd = *(int *)arg;
Expand Down Expand Up @@ -158,7 +162,8 @@ bsock_thread_table_init (void)
bsock_thread_elts[BSOCK_THREAD_MAX-1].next = NULL;
}

static struct bsock_client_st * __attribute__((nonnull))
__attribute_nonnull__
static struct bsock_client_st *
bsock_thread_table_query (const struct bsock_client_st * const c)
{
const uid_t uid = c->uid;
Expand All @@ -169,7 +174,8 @@ bsock_thread_table_query (const struct bsock_client_st * const c)
return t;
}

static struct bsock_client_st * __attribute__((nonnull))
__attribute_nonnull__
static struct bsock_client_st *
bsock_thread_table_add (struct bsock_client_st * const c)
{
/* (not checking for multiple-add of same uid (do not do that)) */
Expand All @@ -184,7 +190,8 @@ bsock_thread_table_add (struct bsock_client_st * const c)
return (*next = t);
}

static void __attribute__((nonnull))
__attribute_nonnull__
static void
bsock_thread_table_remove (struct bsock_client_st * const c)
{
/* (removes only first uid found if multiple (should not happen)) */
Expand All @@ -203,7 +210,8 @@ bsock_thread_table_remove (struct bsock_client_st * const c)
}
}

static int __attribute__((nonnull))
__attribute_nonnull__
static int
bsock_client_handler (struct bsock_client_st * const restrict c,
struct addrinfo * const restrict ai,
int * const restrict fd)
Expand Down Expand Up @@ -309,7 +317,8 @@ uint32_to_str(uint32_t u, char * const buf)
return (int)(out - buf);
}

static void __attribute__((noinline))
__attribute_noinline__
static void
bsock_infostr (char * restrict infobuf,
const int fd, const uid_t uid, const gid_t gid)
{
Expand Down Expand Up @@ -339,7 +348,8 @@ bsock_infostr (char * restrict infobuf,
infobuf[0] = '\0';
}

static void __attribute__((nonnull))
__attribute_nonnull__
static void
bsock_cleanup_client (void * const arg)
{
struct bsock_client_st * const c = (struct bsock_client_st *)arg;
Expand All @@ -352,7 +362,8 @@ bsock_cleanup_client (void * const arg)
}
}

static void * __attribute__((nonnull))
__attribute_nonnull__
static void *
bsock_client_thread (void * const arg)
{
struct bsock_client_st c; /* copy so that not referencing hash entry */
Expand Down Expand Up @@ -405,7 +416,9 @@ bsock_client_thread (void * const arg)
return NULL; /* end of thread; identical to pthread_exit() */
}

static void __attribute_cold__
__attribute_cold__
__attribute_noinline__
static void
bsock_sigaction_sighup (void)
{
/* efficiency: keep databases open (advantageous for nss_mcdb module) */
Expand All @@ -423,7 +436,8 @@ bsock_sigaction_sighup (void)
endservent();
}

static void __attribute__((nonnull))
__attribute_nonnull__
static void
bsock_sigaction (sigset_t * const restrict sigs, const int signo)
{
switch (signo) {
Expand All @@ -441,7 +455,9 @@ bsock_sigaction (sigset_t * const restrict sigs, const int signo)
}
}

static void __attribute__((nonnull)) __attribute__((noreturn))
__attribute_nonnull__
__attribute_noreturn__
static void
bsock_sigwait (void * const arg)
{
sigset_t * const sigs = (sigset_t *)arg;
Expand Down Expand Up @@ -473,7 +489,9 @@ bsock_thread_signals (void)
bsock_syslog(errnum, LOG_ERR, "pthread_create");
}

static void __attribute__((noinline)) __attribute_cold__
__attribute_cold__
__attribute_noinline__
static void
bsock_client_send_errno (const int fd, int errnum)
{
/* one-shot response; send buffer should be empty and should not block */
Expand Down Expand Up @@ -553,8 +571,10 @@ bsock_thread_event_loop (const int sfd, pthread_attr_t * const restrict attr)
}

/* one-shot mode; handle single request and exit */
__attribute_cold__
__attribute_noinline__
__attribute_nonnull__
static int
__attribute__((nonnull)) __attribute__((noinline)) __attribute_cold__
bsock_client_once (const int argc, char ** const restrict argv)
{
struct bsock_client_st m;
Expand Down Expand Up @@ -646,7 +666,8 @@ bsock_client_once (const int argc, char ** const restrict argv)
return rc;
}

int __attribute__((nonnull))
__attribute_nonnull__
int
main (int argc, char *argv[])
{
int sfd, opt, daemon = false, supervised = false;
Expand Down
5 changes: 4 additions & 1 deletion bsock/bsock.t.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <plasma/plasma_attr.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Expand All @@ -35,7 +37,8 @@
#include <bsock_addrinfo.h>
#include <bsock_bind.h>

int __attribute__((nonnull))
__attribute_nonnull__
int
main (int argc, char *argv[])
{
int nfd;
Expand Down
30 changes: 20 additions & 10 deletions bsock/bsock_addrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ extern int getprotobynumber_r(int proto, struct protoent *protoptr,
* Similarly, simple string parsing routines like strtok() are used, even though
* less string traversals could be achieved through additional coding */

static int __attribute__((nonnull))
__attribute_nonnull__
static int
bsock_addrinfo_family_from_str (const char * const restrict family)
{
/* list of protocol families below is not complete */
Expand Down Expand Up @@ -167,7 +168,8 @@ bsock_addrinfo_family_to_str (const int family)
}
}

static int __attribute__((nonnull))
__attribute_nonnull__
static int
bsock_addrinfo_socktype_from_str (const char * const restrict socktype)
{
if ( 0 == memcmp_constr(socktype, "SOCK_STREAM"))
Expand Down Expand Up @@ -218,7 +220,8 @@ bsock_addrinfo_socktype_to_str (const int socktype)
}
}

static int __attribute__((nonnull))
__attribute_nonnull__
static int
bsock_addrinfo_protocol_from_str (const char * const restrict protocol)
{
#ifdef _AIX
Expand Down Expand Up @@ -269,7 +272,8 @@ bsock_addrinfo_protocol_from_str (const char * const restrict protocol)
}

#ifdef HAVE_GETPROTOBYNUMBER_R
static char * __attribute__((nonnull))
__attribute_nonnull__
static char *
bsock_addrinfo_protocol_to_str (const int proto,
char * const buf, const size_t bufsz)
{
Expand Down Expand Up @@ -304,7 +308,8 @@ bsock_addrinfo_protocol_to_str (const int proto)
bsock_addrinfo_protocol_to_str(proto)
#endif

bool __attribute__((nonnull))
__attribute_nonnull__
bool
bsock_addrinfo_from_strs(struct addrinfo * const restrict ai,
const struct bsock_addrinfo_strs *
const restrict aistr)
Expand Down Expand Up @@ -373,7 +378,8 @@ bsock_addrinfo_from_strs(struct addrinfo * const restrict ai,
}
}

bool __attribute__((nonnull))
__attribute_nonnull__
bool
bsock_addrinfo_to_strs(const struct addrinfo * const restrict ai,
struct bsock_addrinfo_strs * const aistr,
char * const restrict buf, const size_t bufsz)
Expand Down Expand Up @@ -415,7 +421,8 @@ bsock_addrinfo_to_strs(const struct addrinfo * const restrict ai,
return false;
}

bool __attribute__((nonnull))
__attribute_nonnull__
bool
bsock_addrinfo_split_str(struct bsock_addrinfo_strs * const aistr,
char * const restrict str)
{
Expand All @@ -428,7 +435,8 @@ bsock_addrinfo_split_str(struct bsock_addrinfo_strs * const aistr,
) || (errno = EINVAL, false);
}

bool __attribute__((nonnull (2,3)))
__attribute_nonnull_x__((2,3))
bool
bsock_addrinfo_recv_ex (const int fd,
struct addrinfo * const restrict ai,
int * const restrict rfd,
Expand Down Expand Up @@ -499,7 +507,8 @@ bsock_addrinfo_recv_ex (const int fd,
}

#if 0 /* see #define bsock_addrinfo_recv(fd, ai, rfd) in bsock_addrinfo.h */
bool __attribute__((nonnull))
__attribute_nonnull__
bool
bsock_addrinfo_recv (const int fd,
struct addrinfo * const restrict ai,
int * const restrict rfd)
Expand All @@ -508,7 +517,8 @@ bsock_addrinfo_recv (const int fd,
}
#endif

bool __attribute__((nonnull))
__attribute_nonnull__
bool
bsock_addrinfo_send (const int fd,
const struct addrinfo * const restrict ai, const int sfd)
{
Expand Down
Loading

0 comments on commit 1e301ab

Please sign in to comment.