#include "lint.h"
#include "mtlib.h"
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <pthread.h>
#include <thread.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <dlfcn.h>
#include <atomic.h>
#include <md5.h>
#include "pos4obj.h"
#define HASHSTRLEN 32
static char *__pos4obj_name(const char *, const char *);
static void __pos4obj_md5toa(unsigned char *, unsigned char *);
static void __pos4obj_clean(char *);
static char objroot[] = "/tmp/";
static long int name_max = 0;
int
__open_nc(const char *path, int oflag, mode_t mode)
{
int cancel_state;
int val;
struct stat64 statbuf;
if (lstat64(path, &statbuf) == 0) {
if (S_ISLNK(statbuf.st_mode)) {
errno = EINVAL;
return (-1);
}
}
(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
val = open64(path, oflag, mode);
(void) pthread_setcancelstate(cancel_state, NULL);
return (val);
}
int
__close_nc(int fildes)
{
int cancel_state;
int val;
(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
val = close(fildes);
(void) pthread_setcancelstate(cancel_state, NULL);
return (val);
}
typedef void (*md5_calc_t)(unsigned char *, unsigned char *, unsigned int);
static md5_calc_t real_md5_calc = NULL;
static mutex_t md5_lock = DEFAULTMUTEX;
static void
load_md5_calc(void)
{
void *md5_handle = dlopen("libmd.so.1", RTLD_LAZY);
md5_calc_t md5_calc = (md5_handle == NULL)? NULL :
(md5_calc_t)dlsym(md5_handle, "md5_calc");
lmutex_lock(&md5_lock);
if (real_md5_calc == NULL) {
if (md5_calc == NULL)
real_md5_calc = (md5_calc_t)(-1);
else {
real_md5_calc = md5_calc;
md5_handle = NULL;
}
membar_producer();
}
lmutex_unlock(&md5_lock);
if (md5_handle)
(void) dlclose(md5_handle);
}
static char *
__pos4obj_name(const char *path, const char *type)
{
int shortpath = 1;
int olderrno;
size_t len;
char *dfile;
unsigned char hashbuf[HASHSTRLEN + 1];
unsigned char md5_digest[MD5_DIGEST_LENGTH];
if ((strlen(path) - 1) > (name_max - strlen(type)))
shortpath = 0;
if (shortpath) {
len = strlen(objroot) + strlen(type) + strlen(path);
} else {
len = strlen(objroot) + HASHSTRLEN + strlen(type) +
strlen(path) + 3;
}
if ((dfile = malloc(len)) == NULL)
return (NULL);
(void) memset(dfile, 0, len);
(void) strcpy(dfile, objroot);
if (shortpath) {
(void) strcat(dfile, type);
(void) strcat(dfile, path + 1);
return (dfile);
}
if (real_md5_calc == NULL)
load_md5_calc();
if (real_md5_calc == (md5_calc_t)(-1)) {
free(dfile);
return (NULL);
}
real_md5_calc(md5_digest, (unsigned char *)path + 1, strlen(path + 1));
__pos4obj_md5toa(hashbuf, md5_digest);
(void) strcat(dfile, ".");
(void) strcat(dfile, (const char *)hashbuf);
olderrno = errno;
if (mkdir(dfile, S_IRWXU|S_IRWXG|S_IRWXO) == 0) {
if (chmod(dfile, S_IRWXU|S_IRWXG|S_IRWXO) == -1) {
free(dfile);
return (NULL);
}
} else {
if (errno != EEXIST) {
free(dfile);
return (NULL);
}
}
(void) strcat(dfile, "/");
(void) strcat(dfile, type);
if (mkdir(dfile, S_IRWXU|S_IRWXG|S_IRWXO) == 0) {
if (chmod(dfile, S_IRWXU|S_IRWXG|S_IRWXO) == -1) {
free(dfile);
return (NULL);
}
} else {
if (errno != EEXIST) {
free(dfile);
return (NULL);
}
}
errno = olderrno;
(void) strcat(dfile, path);
return (dfile);
}
static void
__pos4obj_md5toa(unsigned char *dest, unsigned char *src)
{
int i;
uint32_t *p;
p = (uint32_t *)src;
for (i = 0; i < (MD5_DIGEST_LENGTH / 4); i++)
(void) snprintf((char *)dest + (i * 8), 9, "%.8x", *p++);
dest[HASHSTRLEN] = '\0';
}
int
__pos4obj_open(const char *name, char *type, int oflag,
mode_t mode, int *crflag)
{
int fd;
char *dfile;
errno = 0;
*crflag = 0;
if ((dfile = __pos4obj_name(name, type)) == NULL) {
return (-1);
}
if (!(oflag & O_CREAT)) {
if ((fd = __open_nc(dfile, oflag, mode)) == -1)
__pos4obj_clean(dfile);
free(dfile);
return (fd);
}
for (;;) {
if ((fd = __open_nc(dfile, (oflag | O_EXCL), mode)) == -1) {
if (errno == EEXIST && !(oflag & O_EXCL)) {
fd = __open_nc(dfile, oflag & ~O_CREAT, mode);
if (fd == -1 && errno == ENOENT)
continue;
break;
}
} else {
*crflag = 1;
}
break;
}
free(dfile);
return (fd);
}
int
__pos4obj_unlink(const char *name, const char *type)
{
int err;
char *dfile;
if ((dfile = __pos4obj_name(name, type)) == NULL) {
return (-1);
}
err = unlink(dfile);
__pos4obj_clean(dfile);
free(dfile);
return (err);
}
int
__pos4obj_lock(const char *name, const char *ltype)
{
char *dfile;
int fd;
int limit = 64;
if ((dfile = __pos4obj_name(name, ltype)) == NULL) {
return (-1);
}
while (limit-- > 0) {
if ((fd = __open_nc(dfile, O_RDWR | O_CREAT | O_EXCL, 0666))
< 0) {
if (errno != EEXIST)
break;
(void) sleep(1);
continue;
}
(void) __close_nc(fd);
free(dfile);
return (1);
}
free(dfile);
return (-1);
}
int
__pos4obj_unlock(const char *path, const char *type)
{
return (__pos4obj_unlink(path, type));
}
static void
__pos4obj_clean(char *path)
{
char *p;
int olderrno;
if (strchr(path + strlen(objroot), '/') == NULL)
return;
olderrno = errno;
if ((p = strrchr(path, '/')) == NULL)
return;
*p = '\0';
(void) rmdir(path);
if ((p = strrchr(path, '/')) == NULL)
return;
*p = '\0';
(void) rmdir(path);
errno = olderrno;
}
int
__pos4obj_check(const char *path)
{
long int i;
if (name_max == 0 || name_max == -1) {
name_max = pathconf(objroot, _PC_NAME_MAX);
if (name_max == -1)
return (-1);
}
if (*path++ != '/') {
errno = EINVAL;
return (-1);
}
for (i = 0; *path != '\0'; i++) {
if (*path++ == '/') {
errno = EINVAL;
return (-1);
}
}
if (i > PATH_MAX || i > name_max) {
errno = ENAMETOOLONG;
return (-1);
}
return (0);
}