From 2a777e516d4c4633228583bc44bc030cc1ce02c9 Mon Sep 17 00:00:00 2001
From: Ranjit Ranjan <ranjit.ranjan@ibm.com>
Date: Mon, 12 Feb 2024 10:56:26 -0600
Subject: [PATCH] code-changes-for-aix-support

---
 include/tlog/aix-tlog-util.h       |  87 +++++++
 include/tlog/rc.h                  |   4 +
 lib/tlog/Makefile.am               |   4 +-
 lib/tlog/aix-tlog-util.c           | 369 +++++++++++++++++++++++++++++
 lib/tlog/errs.c                    |   3 +
 lib/tlog/misc.c                    |  12 +-
 lib/tlog/play.c                    |  13 +-
 lib/tlog/play_conf_cmd.c.m4        |   4 +
 lib/tlog/rec.c                     |  65 +++++
 lib/tlog/rec_conf_cmd.c.m4         |   4 +
 lib/tlog/rec_session_conf_cmd.c.m4 |   4 +
 lib/tlog/session.c                 |   7 +
 lib/tlog/syslog_misc.c             |  65 +++++
 lib/tlog/tap.c                     |  12 +-
 src/tlog/Makefile.am               |   6 +-
 src/tlog/tlog-rec.c                |  12 +-
 16 files changed, 657 insertions(+), 14 deletions(-)
 create mode 100644 include/tlog/aix-tlog-util.h
 create mode 100644 lib/tlog/aix-tlog-util.c

diff --git a/include/tlog/aix-tlog-util.h b/include/tlog/aix-tlog-util.h
new file mode 100644
index 0000000..22dc1a1
--- /dev/null
+++ b/include/tlog/aix-tlog-util.h
@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stropts.h>
+#include <unistd.h>
+#include <paths.h>
+#include <termios.h>
+#include <utmpx.h>
+#include <time.h>
+#include <procinfo.h>
+
+
+#ifndef _AIX_TLOG_UTIL_H
+#define _AIX_TLOG_UTIL_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#if defined(HAVE__VSNPRINTF) && !defined(HAVE_VSNPRINTF)
+#undef vsnprintf
+#define vsnprintf _vsnprintf
+#elif !HAVE_VSNPRINTF
+int vsnprintf(char *ret, size_t max, const char *fmt, va_list ap);
+#endif /* !HAVE_VSNPRINTF */
+
+#if !HAVE_ASPRINTF
+int asprintf(char **ret, const char *fmt, ...);
+#endif /* !HAVE_ASPRINTF */
+
+#if !HAVE_VASPRINTF
+int vasprintf(char **ret, const char *fmt, va_list ap);
+#endif /* !HAVE_VASPRINTF */
+
+
+struct option
+{
+        const char *name;
+        int                     has_arg;
+        int                *flag;
+        int                     val;
+};
+#define no_argument 0
+#define required_argument 1
+#define optional_argument 2
+#define BADCH   '?'
+#define BADARG  ':'
+#define EMSG    ""
+/*
+ * getopt_long
+ *      Parse argc/argv argument vector, with long options.
+ *
+ * This implementation does not use optreset.  Instead, we guarantee that
+ * it can be restarted on a new argv array after a previous call returned -1,
+ * if the caller resets optind to 1 before the first call of the new series.
+ * (Internally, this means we must be sure to reset "place" to EMSG before
+ * returning -1.)
+ */
+int
+getopt_long(int argc, char *const argv[],
+                        const char *optstring,
+                        const struct option *longopts, int *longindex);
+
+
+char *strchrnul(const char *s, int c);
+
+pid_t forkpty(int *master, char *name, struct termios *tio, struct winsize *ws);
+
+int getSystemBootTime(time_t *t);
+
+int getProcesStartTime(time_t *procStartTime);
+
+int getProcessSessionId(unsigned int *psessionId);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/include/tlog/rc.h b/include/tlog/rc.h
index 2d9f8ad..56e5eea 100644
--- a/include/tlog/rc.h
+++ b/include/tlog/rc.h
@@ -30,6 +30,10 @@
 
 #include <stdbool.h>
 
+#ifdef _AIX
+#include <tlog/aix-tlog-util.h>
+#endif
+
 /** Minimum function return code */
 #define TLOG_RC_MIN  0
 
diff --git a/lib/tlog/Makefile.am b/lib/tlog/Makefile.am
index 8eeba7d..8e706a4 100644
--- a/lib/tlog/Makefile.am
+++ b/lib/tlog/Makefile.am
@@ -143,7 +143,8 @@ libtlog_la_SOURCES = \
     timestr.c                   \
     tty_sink.c                  \
     tty_source.c                \
-    utf8.c
+    utf8.c						\
+	aix-tlog-util.c
 
 if TLOG_JOURNAL_ENABLED
 libtlog_la_SOURCES += \
@@ -156,6 +157,5 @@ libtlog_la_LIBADD = \
     $(JSON_LIBS) \
     $(SYSTEMD_JOURNAL_LIBS) \
     $(LIBCURL) \
-    -lutil \
     -lrt \
     -lm
diff --git a/lib/tlog/aix-tlog-util.c b/lib/tlog/aix-tlog-util.c
new file mode 100644
index 0000000..3ef47b2
--- /dev/null
+++ b/lib/tlog/aix-tlog-util.c
@@ -0,0 +1,369 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stropts.h>
+#include <unistd.h>
+#include <paths.h>
+#include <string.h>
+
+#include "tlog/aix-tlog-util.h"
+
+
+int
+getopt_long(int argc, char *const argv[],
+                        const char *optstring,
+                        const struct option *longopts, int *longindex)
+{
+        static char *place = EMSG;      /* option letter processing */
+        char       *oli;                        /* option letter list index */
+        if (!*place)
+        {                                                       /* update scanning pointer */
+                if (optind >= argc)
+                {
+                        place = EMSG;
+                        return -1;
+                }
+                place = argv[optind];
+                if (place[0] != '-')
+                {
+                        place = EMSG;
+                        return -1;
+                }
+                place++;
+                if (place[0] && place[0] == '-' && place[1] == '\0')
+                {                                               /* found "--" */
+                        ++optind;
+                        place = EMSG;
+                        return -1;
+                }
+                if (place[0] && place[0] == '-' && place[1])
+                {
+                        /* long option */
+                        size_t          namelen;
+                        int                     i;
+                        place++;
+                        namelen = strcspn(place, "=");
+                        for (i = 0; longopts[i].name != NULL; i++)
+                        {
+                                if (strlen(longopts[i].name) == namelen
+                                        && strncmp(place, longopts[i].name, namelen) == 0)
+                                {
+                                        int                     has_arg = longopts[i].has_arg;
+                                        if (has_arg != no_argument)
+                                        {
+                                                if (place[namelen] == '=')
+                                                        optarg = place + namelen + 1;
+                                                else if (optind < argc - 1 &&
+                                                                 has_arg == required_argument)
+                                                {
+                                                        optind++;
+                                                        optarg = argv[optind];
+                                                }
+                                                else
+                                                {
+                                                        if (optstring[0] == ':')
+                                                                return BADARG;
+                                                        if (opterr && has_arg == required_argument)
+                                                                fprintf(stderr,
+                                                                                "%s: option requires an argument -- %s\n",
+                                                                                argv[0], place);
+                                                        place = EMSG;
+                                                        optind++;
+                                                        if (has_arg == required_argument)
+                                                                return BADCH;
+                                                        optarg = NULL;
+                                                }
+                                        }
+                                        else
+                                        {
+                                                optarg = NULL;
+                                                if (place[namelen] != 0)
+                                                {
+                                                        /* XXX error? */
+                                                }
+                                        }
+                                        optind++;
+                                        if (longindex)
+                                                *longindex = i;
+                                        place = EMSG;
+                                        if (longopts[i].flag == NULL)
+                                                return longopts[i].val;
+                                        else
+                                        {
+                                                *longopts[i].flag = longopts[i].val;
+                                                return 0;
+                                        }
+                                }
+                        }
+                        if (opterr && optstring[0] != ':')
+                                fprintf(stderr,
+                                                "%s: illegal option -- %s\n", argv[0], place);
+                        place = EMSG;
+                        optind++;
+                        return BADCH;
+                }
+        }
+        /* short option */
+        optopt = (int) *place++;
+        oli = strchr(optstring, optopt);
+        if (!oli)
+        {
+                if (!*place)
+                        ++optind;
+                if (opterr && *optstring != ':')
+                        fprintf(stderr,
+                                        "%s: illegal option -- %c\n", argv[0], optopt);
+                return BADCH;
+        }
+        if (oli[1] != ':')
+        {                                                       /* don't need argument */
+                optarg = NULL;
+                if (!*place)
+                        ++optind;
+        }
+        else
+        {                                                       /* need an argument */
+                if (*place)                             /* no white space */
+                        optarg = place;
+                else if (argc <= ++optind)
+                {                                               /* no arg */
+                        place = EMSG;
+                        if (*optstring == ':')
+                                return BADARG;
+                        if (opterr)
+                                fprintf(stderr,
+                                                "%s: option requires an argument -- %c\n",
+                                                argv[0], optopt);
+                        return BADCH;
+                }
+                else
+                        /* white space */
+                        optarg = argv[optind];
+                place = EMSG;
+                ++optind;
+        }
+        return optopt;
+}
+
+
+char *strchrnul(const char *s, int c)
+{
+	if(strchr(s,c))
+	{
+		return strchr(s,c);
+	}
+	return s+strlen(s);
+}
+
+
+pid_t forkpty(int *master, char *name, struct termios *tio, struct winsize *ws)
+{
+	int	slave, fd;
+	char   *path;
+	pid_t	pid;
+	struct termios tio2;
+
+	if ((*master = open("/dev/ptc", O_RDWR|O_NOCTTY)) == -1)
+		return -1;
+
+	if ((path = ttyname(*master)) == NULL)
+		goto out;
+	if ((slave = open(path, O_RDWR|O_NOCTTY)) == -1)
+		goto out;
+
+	switch (pid = fork()) {
+	case -1:
+		goto out;
+	case 0:
+		close(*master);
+
+		fd = open(_PATH_TTY, O_RDWR|O_NOCTTY);
+		if (fd >= 0) {
+			ioctl(fd, TIOCNOTTY, NULL);
+			close(fd);
+		}
+
+		setsid();
+
+		fd = open(_PATH_TTY, O_RDWR|O_NOCTTY);
+		if (fd >= 0)
+			return -1;
+
+		fd = open(path, O_RDWR);
+		if (fd < 0)
+			return -1;
+		close(fd);
+
+		fd = open("/dev/tty", O_WRONLY);
+		if (fd < 0)
+			return -1;
+		close(fd);
+
+		if (tcgetattr(slave, &tio2) != 0)
+			return -1;
+		if (tio != NULL)
+			memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
+		tio2.c_cc[VERASE] = '\177';
+		if (tcsetattr(slave, TCSAFLUSH, &tio2) == -1)
+			return -1;
+		if (ioctl(slave, TIOCSWINSZ, ws) == -1)
+			return -1;
+
+		dup2(slave, 0);
+		dup2(slave, 1);
+		dup2(slave, 2);
+		if (slave > 2)
+			close(slave);
+		return 0;
+	}
+
+	close(slave);
+	return pid;
+
+out:
+	if (*master != -1)
+		close(*master);
+	if (slave != -1)
+		close(slave);
+	return -1;
+}
+
+
+#if defined(HAVE__VSNPRINTF) && !defined(HAVE_VSNPRINTF)
+#undef HAVE_VSNPRINTF
+#undef vsnprintf
+#define HAVE_VSNPRINTF 1
+#define vsnprintf _vsnprintf
+#endif
+
+#ifndef _PATH_DEVNULL
+#define _PATH_DEVNULL "/dev/null"
+#endif
+
+#define CHUNKSIZE 512
+int
+vasprintf(char **ret, const char *fmt, va_list ap)
+{
+#if HAVE_VSNPRINTF
+        int chunks;
+        size_t buflen;
+        char *buf;
+        int len;
+
+        chunks = ((strlen(fmt) + 1) / CHUNKSIZE) + 1;
+        buflen = chunks * CHUNKSIZE;
+        for (;;) {
+                if ((buf = malloc(buflen)) == NULL) {
+                        *ret = NULL;
+                        return -1;
+                }
+                len = vsnprintf(buf, buflen, fmt, ap);
+                if (len >= 0 && (size_t) len < (buflen - 1)) {
+                        break;
+                }
+                free(buf);
+                buflen = (++chunks) * CHUNKSIZE;
+                /*
+                 * len >= 0 are required for vsnprintf implementation that
+                 * return -1 of buffer insufficient
+                 */
+                if (len >= 0 && (size_t) len >= buflen) {
+                        buflen = len + 1;
+                }
+        }
+        *ret = buf;
+        return len;
+#else /* HAVE_VSNPRINTF */
+#ifdef _REENTRANT
+        FILE *fp;
+#else /* !_REENTRANT */
+        static FILE *fp = NULL;
+#endif /* !_REENTRANT */
+        int len;
+        char *buf;
+
+        *ret = NULL;
+
+#ifdef _REENTRANT
+
+# ifdef WIN32
+#  error Win32 do not have /dev/null, should use vsnprintf version
+# endif
+
+        if ((fp = fopen(_PATH_DEVNULL, "w")) == NULL)
+                return -1;
+#else /* !_REENTRANT */
+        if ((fp == NULL) && ((fp = fopen(_PATH_DEVNULL, "w")) == NULL))
+                return -1;
+#endif /* !_REENTRANT */
+
+        len = vfprintf(fp, fmt, ap);
+
+#ifdef _REENTRANT
+        if (fclose(fp) != 0)
+                return -1;
+#endif /* _REENTRANT */
+
+        if (len < 0)
+                return len;
+        if ((buf = malloc(len + 1)) == NULL)
+                return -1;
+        if (vsprintf(buf, fmt, ap) != len)
+                return -1;
+        *ret = buf;
+        return len;
+#endif /* HAVE_VSNPRINTF */
+}
+
+int
+asprintf(char **ret, const char *fmt, ...)
+{
+        int len;
+        va_list ap;
+
+        va_start(ap, fmt);
+        len = vasprintf(ret, fmt, ap);
+        va_end(ap);
+        return len;
+}
+
+
+
+int getSystemBootTime(time_t *t)
+{
+    struct utmpx * ent;
+    while ( ( ent = getutxent ( ) ) ) {
+        if ( !strcmp ( "system boot", ent->ut_line ) ) {
+            *t = ent->ut_tv.tv_sec;
+            return 0;
+        }
+    }
+    return -1;
+}
+
+int getProcesStartTime(time_t *procStartTime)
+{
+    struct procentry64 psinfo;
+    pid_t pid = getpid();
+    if (getprocs64(&psinfo, sizeof(struct procentry64), NULL, sizeof(struct fdsinfo64) , &pid, 1) > 0) {
+    *procStartTime = psinfo.pi_start;
+    return 0;
+    }
+    return -1;
+}
+
+
+int getProcessSessionId(unsigned int  *psessionId)
+{
+    struct procentry64 psinfo;
+    pid_t pid = getpid();
+    if (getprocs64(&psinfo, sizeof(struct procentry64), NULL, sizeof(struct fdsinfo64) , &pid, 1) > 0) {
+    *psessionId = psinfo.pi_sid;
+    return 0;
+    }
+    return -1;
+}
+
diff --git a/lib/tlog/errs.c b/lib/tlog/errs.c
index 6f02eca..ab86a90 100644
--- a/lib/tlog/errs.c
+++ b/lib/tlog/errs.c
@@ -26,6 +26,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+#ifdef _AIX
+#include <tlog/aix-tlog-util.h>
+#endif
 
 const struct tlog_errs tlog_errs_nomem = {
     .next = NULL,
diff --git a/lib/tlog/misc.c b/lib/tlog/misc.c
index 6f37a41..e634d1e 100644
--- a/lib/tlog/misc.c
+++ b/lib/tlog/misc.c
@@ -136,13 +136,21 @@ tlog_exec(struct tlog_errs **perrs, unsigned int opts,
         gid_t gid = getgid();
 
         /* Drop the possibly-privileged EGID permanently */
-        if (setresgid(gid, gid, gid) < 0) {
+#ifdef _AIX
+		if (setregid(gid, gid) < 0) {
+#else
+		if (setresgid(gid, gid, gid) < 0) {
+#endif
             grc = TLOG_GRC_ERRNO;
             TLOG_ERRS_RAISECF(grc, "Failed dropping EGID");
         }
 
         /* Drop the possibly-privileged EUID permanently */
-        if (setresuid(uid, uid, uid) < 0) {
+#ifdef _AIX
+		if (setreuid(uid, uid) < 0) {
+#else
+		if (setresuid(uid, uid, uid) < 0) {
+#endif
             grc = TLOG_GRC_ERRNO;
             TLOG_ERRS_RAISECF(grc, "Failed dropping EUID");
         }
diff --git a/lib/tlog/play.c b/lib/tlog/play.c
index 085ef80..6f438a3 100644
--- a/lib/tlog/play.c
+++ b/lib/tlog/play.c
@@ -669,8 +669,13 @@ tlog_play_init(struct tlog_errs **perrs,
         grc = TLOG_GRC_ERRNO;
         TLOG_ERRS_RAISECS(grc, "Failed getting stdin flags");
     }
+#ifdef _AIX
     if (fcntl(STDIN_FILENO, F_SETFL,
-              tlog_play_stdin_flags | O_ASYNC | O_NONBLOCK) < 0) {
+			tlog_play_stdin_flags | O_NONBLOCK) < 0) {
+#else
+    if (fcntl(STDIN_FILENO, F_SETFL,
+			tlog_play_stdin_flags | O_ASYNC | O_NONBLOCK) < 0) {
+#endif
         tlog_play_stdin_flags = -1;
         grc = TLOG_GRC_ERRNO;
         TLOG_ERRS_RAISECS(grc, "Failed setting stdin flags");
@@ -1124,7 +1129,11 @@ tlog_play_run(struct tlog_errs **perrs, int *psignal)
          */
         /* Wait for I/O on the terminal */
         do {
-            rc = ppoll(std_pollfds, TLOG_ARRAY_SIZE(std_pollfds), NULL, NULL);
+#ifdef _AIX
+            rc = poll(std_pollfds, TLOG_ARRAY_SIZE(std_pollfds), 0);
+#else
+			rc = ppoll(std_pollfds, TLOG_ARRAY_SIZE(std_pollfds), NULL, NULL);
+#endif
         } while (rc == 0);
         if (rc < 0) {
             if (errno == EINTR) {
diff --git a/lib/tlog/play_conf_cmd.c.m4 b/lib/tlog/play_conf_cmd.c.m4
index 007cbaa..5479a82 100644
--- a/lib/tlog/play_conf_cmd.c.m4
+++ b/lib/tlog/play_conf_cmd.c.m4
@@ -34,7 +34,11 @@ m4_generated_warning(` * ')m4_dnl
 #include <libgen.h>
 #include <stdio.h>
 #include <unistd.h>
+#ifdef _AIX
+#include <tlog/aix-tlog-util.h>
+#else
 #include <getopt.h>
+#endif
 #include <string.h>
 #include <inttypes.h>
 #include <assert.h>
diff --git a/lib/tlog/rec.c b/lib/tlog/rec.c
index 1d4031f..d4fedb7 100644
--- a/lib/tlog/rec.c
+++ b/lib/tlog/rec.c
@@ -151,6 +151,71 @@ tlog_rec_get_id(struct tlog_errs **perrs, char **pid)
     FILE *file = NULL;
     unsigned long long int starttime;
 
+#ifdef _AIX
+    time_t systemBootTime;
+    time_t procStartTime;
+	if(getSystemBootTime(&systemBootTime) < 0) {
+		grc = TLOG_RC_FAILURE;
+        TLOG_ERRS_RAISECF(grc, "getSystemBootTime Failed");
+	}
+	if(getProcesStartTime(&procStartTime) < 0) {
+		grc = TLOG_RC_FAILURE;
+        TLOG_ERRS_RAISECF(grc, "getProcesStartTime Failed");
+	}
+
+	/*
+     * Append systemBootTime
+     */
+    rc = snprintf(p, l, "%llx", systemBootTime);
+    if (rc >= l) {
+        grc = TLOG_RC_FAILURE;
+        tlog_errs_pushc(perrs, grc);
+        tlog_errs_pushs(perrs, "Generated recording ID is too long");
+    }
+    p += rc;
+    l -= rc;
+
+	/*
+     * Append separating dash and PID
+     */
+    rc = snprintf(p, l, "-%x", (unsigned int)getpid());
+    if (rc >= l) {
+        grc = TLOG_RC_FAILURE;
+        tlog_errs_pushc(perrs, grc);
+        tlog_errs_pushs(perrs, "Generated recording ID is too long");
+    }
+    p += rc;
+    l -= rc;
+
+
+    /* Append a separating dash and the start time */
+    rc = snprintf(p, l, "-%llx", procStartTime-systemBootTime);
+    if (rc >= l) {
+        grc = TLOG_RC_FAILURE;
+        tlog_errs_pushc(perrs, grc);
+        tlog_errs_pushs(perrs, "Generated recording ID is too long");
+    }
+	p+=rc;
+
+   
+    /*
+     * Output, if requested.
+     */
+    if (pid != NULL) {
+        *pid = strndup(buf, p - buf);
+        if (*pid == NULL) {
+            grc = TLOG_GRC_ERRNO;
+            tlog_errs_pushc(perrs, grc);
+            tlog_errs_pushs(perrs, "Failed allocating recording ID");
+        }
+    }
+
+    /* Success! */
+    grc = TLOG_RC_OK;
+	return grc;	
+#endif
+
+
     /*
      * Append boot ID
      */
diff --git a/lib/tlog/rec_conf_cmd.c.m4 b/lib/tlog/rec_conf_cmd.c.m4
index 544e4e5..9811057 100644
--- a/lib/tlog/rec_conf_cmd.c.m4
+++ b/lib/tlog/rec_conf_cmd.c.m4
@@ -34,7 +34,11 @@ m4_generated_warning(` * ')m4_dnl
 #include <libgen.h>
 #include <stdio.h>
 #include <unistd.h>
+#ifdef _AIX
+#include <tlog/aix-tlog-util.h>
+#else
 #include <getopt.h>
+#endif
 #include <string.h>
 #include <inttypes.h>
 #include <assert.h>
diff --git a/lib/tlog/rec_session_conf_cmd.c.m4 b/lib/tlog/rec_session_conf_cmd.c.m4
index 7a1e0f4..d6e7633 100644
--- a/lib/tlog/rec_session_conf_cmd.c.m4
+++ b/lib/tlog/rec_session_conf_cmd.c.m4
@@ -34,7 +34,11 @@ m4_generated_warning(` * ')m4_dnl
 #include <libgen.h>
 #include <stdio.h>
 #include <unistd.h>
+#ifdef _AIX
+#include <tlog/aix-tlog-util.h>
+#else
 #include <getopt.h>
+#endif
 #include <string.h>
 #include <inttypes.h>
 #include <assert.h>
diff --git a/lib/tlog/session.c b/lib/tlog/session.c
index 0c672fd..56946cb 100644
--- a/lib/tlog/session.c
+++ b/lib/tlog/session.c
@@ -38,6 +38,13 @@ tlog_session_get_id(unsigned int *pid)
 
     assert(pid != NULL);
 
+#ifdef _AIX
+    if(getProcessSessionId(pid) < 0){
+		return TLOG_RC_FAILURE;
+	}
+    return TLOG_RC_OK;
+#endif
+
     file = fopen("/proc/self/sessionid", "r");
     if (file == NULL) {
         return TLOG_RC_FAILURE;
diff --git a/lib/tlog/syslog_misc.c b/lib/tlog/syslog_misc.c
index ad5940e..fb81ef3 100644
--- a/lib/tlog/syslog_misc.c
+++ b/lib/tlog/syslog_misc.c
@@ -25,6 +25,71 @@
 #define SYSLOG_NAMES
 #include <syslog.h>
 
+#ifdef _AIX
+
+#define	LOG_FTP		(11<<3)	/* ftp daemon */
+
+#define	LOG_PRI(p)	((p) & LOG_PRIMASK)
+#define	LOG_MAKEPRI(fac, pri)	((fac) | (pri))
+
+#ifdef SYSLOG_NAMES
+#define	INTERNAL_NOPRI	0x10	/* the "no priority" priority */
+				/* mark "facility" */
+#define	INTERNAL_MARK	LOG_MAKEPRI(LOG_NFACILITIES << 3, 0)
+
+typedef struct _code {
+	char	*c_name;
+	int	c_val;
+} CODE;
+
+CODE prioritynames[] =
+  {
+    { "alert", LOG_ALERT },
+    { "crit", LOG_CRIT },
+    { "debug", LOG_DEBUG },
+    { "emerg", LOG_EMERG },
+    { "err", LOG_ERR },
+    { "error", LOG_ERR },		/* DEPRECATED */
+    { "info", LOG_INFO },
+    { "none", INTERNAL_NOPRI },		/* INTERNAL */
+    { "notice", LOG_NOTICE },
+    { "panic", LOG_EMERG },		/* DEPRECATED */
+    { "warn", LOG_WARNING },		/* DEPRECATED */
+    { "warning", LOG_WARNING },
+    { NULL, -1 }
+  };
+
+CODE facilitynames[] =
+  {
+    { "auth", LOG_AUTH },
+    { "authpriv", LOG_AUTHPRIV },
+    { "cron", LOG_CRON },
+    { "daemon", LOG_DAEMON },
+    { "ftp", LOG_FTP },
+    { "kern", LOG_KERN },
+    { "lpr", LOG_LPR },
+    { "mail", LOG_MAIL },
+    { "mark", INTERNAL_MARK },		/* INTERNAL */
+    { "news", LOG_NEWS },
+    { "security", LOG_AUTH },		/* DEPRECATED */
+    { "syslog", LOG_SYSLOG },
+    { "user", LOG_USER },
+    { "uucp", LOG_UUCP },
+    { "local0", LOG_LOCAL0 },
+    { "local1", LOG_LOCAL1 },
+    { "local2", LOG_LOCAL2 },
+    { "local3", LOG_LOCAL3 },
+    { "local4", LOG_LOCAL4 },
+    { "local5", LOG_LOCAL5 },
+    { "local6", LOG_LOCAL6 },
+    { "local7", LOG_LOCAL7 },
+    { NULL, -1 }
+  };
+#endif
+
+#endif
+
+
 /**
  * Find a code value in a syslog code list and return its name.
  *
diff --git a/lib/tlog/tap.c b/lib/tlog/tap.c
index 5b8e1e3..fdb9129 100644
--- a/lib/tlog/tap.c
+++ b/lib/tlog/tap.c
@@ -25,7 +25,9 @@
 #include <tlog/tty_source.h>
 #include <tlog/tty_sink.h>
 #include <tlog/misc.h>
+#ifndef _AIX
 #include <pty.h>
+#endif
 #include <sys/mman.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
@@ -179,8 +181,13 @@ tlog_tap_setup(struct tlog_errs **perrs,
     }
 
     /* Allocate privilege-escalation semaphore */
+#ifdef _AIX
+    sem = mmap(NULL, sizeof(*sem), PROT_READ | PROT_WRITE,
+               MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+#else
     sem = mmap(NULL, sizeof(*sem), PROT_READ | PROT_WRITE,
                MAP_SHARED | MAP_ANONYMOUS, 0, 0);
+#endif
     if (sem == MAP_FAILED) {
         grc = TLOG_GRC_ERRNO;
         TLOG_ERRS_RAISECS(grc,
@@ -206,7 +213,7 @@ tlog_tap_setup(struct tlog_errs **perrs,
         }
 
         /* Fork a child connected via a PTY */
-        tap.pid = forkpty(&master_fd, NULL, &tap.termios_orig, &winsize);
+		tap.pid = forkpty(&master_fd, NULL, &tap.termios_orig, &winsize);
         if (tap.pid < 0) {
             grc = TLOG_GRC_ERRNO;
             TLOG_ERRS_RAISECS(grc,
@@ -258,6 +265,7 @@ tlog_tap_setup(struct tlog_errs **perrs,
         _exit(127);
     }
 
+
     /* Escalate the possibly-privileged GID */
     if (setegid(egid) < 0) {
         grc = TLOG_GRC_ERRNO;
@@ -277,6 +285,7 @@ tlog_tap_setup(struct tlog_errs **perrs,
                           "the privileges are escalated");
     }
 
+	sleep(3);
     /* Create the TTY source */
     grc = tlog_tty_source_create(&tap.source, in_fd, tap.out_fd,
                                  tap.tty_fd, 4096, clock_id);
@@ -321,7 +330,6 @@ cleanup:
         munmap(sem, sizeof(*sem));
     }
     tlog_tap_teardown(perrs, &tap, NULL);
-
     return grc;
 }
 
diff --git a/src/tlog/Makefile.am b/src/tlog/Makefile.am
index 74d6de4..ca79943 100644
--- a/src/tlog/Makefile.am
+++ b/src/tlog/Makefile.am
@@ -118,8 +118,7 @@ tlog_rec_LDADD = \
     ../../lib/tlog/libtlog.la   \
     $(JSON_LIBS)                \
     $(PTHREAD_LIBS)             \
-    -lrt                        \
-    -lutil
+    -lrt
 
 tlog_rec_session_SOURCES = \
     tlog-rec-session.c
@@ -129,8 +128,7 @@ tlog_rec_session_LDADD = \
     ../../lib/tlog/libtlog.la   \
     $(JSON_LIBS)                \
     $(PTHREAD_LIBS)             \
-    -lrt                        \
-    -lutil
+    -lrt
 
 tlog_play_SOURCES = \
     tlog-play.c
diff --git a/src/tlog/tlog-rec.c b/src/tlog/tlog-rec.c
index 3c80986..b0cdbf3 100644
--- a/src/tlog/tlog-rec.c
+++ b/src/tlog/tlog-rec.c
@@ -64,12 +64,20 @@ main(int argc, char **argv)
 
     /* Lock any elevated privileges we received */
     egid = getegid();
-    if (setresgid(egid, egid, egid) < 0) {
+#ifdef _AIX
+    if (setregid(egid, egid) < 0) {
+#else
+	if (setresgid(egid, egid, egid) < 0) {
+#endif
         grc = TLOG_GRC_ERRNO;
         TLOG_ERRS_RAISECF(grc, "Failed locking GID");
     }
     euid = geteuid();
-    if (setresuid(euid, euid, euid) < 0) {
+#ifdef _AIX
+    if (setreuid(euid, euid) < 0) {
+#else
+	if (setresuid(euid, euid, euid) < 0){
+#endif
         grc = TLOG_GRC_ERRNO;
         TLOG_ERRS_RAISECF(grc, "Failed locking UID");
     }
-- 
2.41.0

