#include "includes.h"
int sys_usleep(long usecs)
{
#ifndef HAVE_USLEEP
struct timeval tval;
#endif
if(usecs < 0 || usecs > 1000000) {
errno = EINVAL;
return -1;
}
#if HAVE_USLEEP
usleep(usecs);
return 0;
#else
tval.tv_sec = 0;
tval.tv_usec = usecs/1000;
select(0,NULL,NULL,NULL,&tval);
return 0;
#endif
}
int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
{
int ret;
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
ret = stat64(fname, sbuf);
#else
ret = stat(fname, sbuf);
#endif
if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
return ret;
}
int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
{
int ret;
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
ret = fstat64(fd, sbuf);
#else
ret = fstat(fd, sbuf);
#endif
if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
return ret;
}
int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
{
int ret;
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
ret = lstat64(fname, sbuf);
#else
ret = lstat(fname, sbuf);
#endif
if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
return ret;
}
int sys_ftruncate(int fd, SMB_OFF_T offset)
{
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
return ftruncate64(fd, offset);
#else
return ftruncate(fd, offset);
#endif
}
SMB_OFF_T sys_lseek(int fd, SMB_OFF_T offset, int whence)
{
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
return lseek64(fd, offset, whence);
#else
return lseek(fd, offset, whence);
#endif
}
int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
{
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
return fseek64(fp, offset, whence);
#elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
return fseeko64(fp, offset, whence);
#else
return fseek(fp, offset, whence);
#endif
}
SMB_OFF_T sys_ftell(FILE *fp)
{
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
return (SMB_OFF_T)ftell64(fp);
#elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
return (SMB_OFF_T)ftello64(fp);
#else
return (SMB_OFF_T)ftell(fp);
#endif
}
int sys_creat(const char *path, mode_t mode)
{
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CREAT64)
return creat64(path, mode);
#else
return sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
#endif
}
int sys_open(const char *path, int oflag, mode_t mode)
{
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
return open64(path, oflag, mode);
#else
return open(path, oflag, mode);
#endif
}
FILE *sys_fopen(const char *path, const char *type)
{
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FOPEN64)
return fopen64(path, type);
#else
return fopen(path, type);
#endif
}
SMB_STRUCT_DIRENT *sys_readdir(DIR *dirp)
{
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
return readdir64(dirp);
#else
return readdir(dirp);
#endif
}
int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
{
#if defined(HAVE_MKNOD) || defined(HAVE_MKNOD64)
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_MKNOD64) && defined(HAVE_DEV64_T)
return mknod64(path, mode, dev);
#else
return mknod(path, mode, dev);
#endif
#else
errno = ENOSYS;
return -1;
#endif
}
char *sys_realpath(const char *path, char *resolved_path)
{
#if defined(HAVE_REALPATH)
return realpath(path, resolved_path);
#else
errno = EINVAL;
return NULL;
#endif
}
int sys_waitpid(pid_t pid,int *status,int options)
{
#ifdef HAVE_WAITPID
return waitpid(pid,status,options);
#else
return wait4(pid, status, options, NULL);
#endif
}
char *sys_getwd(char *s)
{
char *wd;
#ifdef HAVE_GETCWD
wd = (char *)getcwd(s, sizeof (pstring));
#else
wd = (char *)getwd(s);
#endif
return wd;
}
int sys_symlink(const char *oldpath, const char *newpath)
{
#ifndef HAVE_SYMLINK
errno = ENOSYS;
return -1;
#else
return symlink(oldpath, newpath);
#endif
}
int sys_readlink(const char *path, char *buf, size_t bufsiz)
{
#ifndef HAVE_READLINK
errno = ENOSYS;
return -1;
#else
return readlink(path, buf, bufsiz);
#endif
}
int sys_link(const char *oldpath, const char *newpath)
{
#ifndef HAVE_LINK
errno = ENOSYS;
return -1;
#else
return link(oldpath, newpath);
#endif
}
int sys_chown(const char *fname,uid_t uid,gid_t gid)
{
#ifndef HAVE_CHOWN
static int done;
if (!done) {
DEBUG(1,("WARNING: no chown!\n"));
done=1;
}
#else
return(chown(fname,uid,gid));
#endif
}
int sys_chroot(const char *dname)
{
#ifndef HAVE_CHROOT
static int done;
if (!done) {
DEBUG(1,("WARNING: no chroot!\n"));
done=1;
}
errno = ENOSYS;
return -1;
#else
return(chroot(dname));
#endif
}
struct hostent *sys_gethostbyname(const char *name)
{
#ifdef REDUCE_ROOT_DNS_LOOKUPS
char query[256], hostname[256];
char *domain;
if (strchr(name, '.'))
return(gethostbyname(name));
gethostname(hostname, sizeof(hostname) - 1);
hostname[sizeof(hostname) - 1] = 0;
if ((domain = strchr(hostname, '.')) == NULL)
return(gethostbyname(name));
if((strlen(name) + strlen(domain)) >= sizeof(query))
return(gethostbyname(name));
slprintf(query, sizeof(query)-1, "%s%s", name, domain);
return(gethostbyname(query));
#else
return(gethostbyname(name));
#endif
}
#if defined(HAVE_IRIX_SPECIFIC_CAPABILITIES)
static BOOL set_process_capability( uint32 cap_flag, BOOL enable )
{
if(cap_flag == KERNEL_OPLOCK_CAPABILITY) {
cap_t cap = cap_get_proc();
if (cap == NULL) {
DEBUG(0,("set_process_capability: cap_get_proc failed. Error was %s\n",
strerror(errno)));
return False;
}
if(enable)
cap->cap_effective |= CAP_NETWORK_MGT;
else
cap->cap_effective &= ~CAP_NETWORK_MGT;
if (cap_set_proc(cap) == -1) {
DEBUG(0,("set_process_capability: cap_set_proc failed. Error was %s\n",
strerror(errno)));
cap_free(cap);
return False;
}
cap_free(cap);
DEBUG(10,("set_process_capability: Set KERNEL_OPLOCK_CAPABILITY.\n"));
}
return True;
}
static BOOL set_inherited_process_capability( uint32 cap_flag, BOOL enable )
{
if(cap_flag == KERNEL_OPLOCK_CAPABILITY) {
cap_t cap = cap_get_proc();
if (cap == NULL) {
DEBUG(0,("set_inherited_process_capability: cap_get_proc failed. Error was %s\n",
strerror(errno)));
return False;
}
if(enable)
cap->cap_inheritable |= CAP_NETWORK_MGT;
else
cap->cap_inheritable &= ~CAP_NETWORK_MGT;
if (cap_set_proc(cap) == -1) {
DEBUG(0,("set_inherited_process_capability: cap_set_proc failed. Error was %s\n",
strerror(errno)));
cap_free(cap);
return False;
}
cap_free(cap);
DEBUG(10,("set_inherited_process_capability: Set KERNEL_OPLOCK_CAPABILITY.\n"));
}
return True;
}
#endif
void oplock_set_capability(BOOL this_process, BOOL inherit)
{
#if HAVE_KERNEL_OPLOCKS_IRIX
set_process_capability(KERNEL_OPLOCK_CAPABILITY,this_process);
set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY,inherit);
#endif
}
long sys_random(void)
{
#if defined(HAVE_RANDOM)
return (long)random();
#elif defined(HAVE_RAND)
return (long)rand();
#else
DEBUG(0,("Error - no random function available !\n"));
exit(1);
#endif
}
void sys_srandom(unsigned int seed)
{
#if defined(HAVE_SRANDOM)
srandom(seed);
#elif defined(HAVE_SRAND)
srand(seed);
#else
DEBUG(0,("Error - no srandom function available !\n"));
exit(1);
#endif
}
int groups_max(void)
{
#if defined(SYSCONF_SC_NGROUPS_MAX)
int ret = sysconf(_SC_NGROUPS_MAX);
return (ret == -1) ? NGROUPS_MAX : ret;
#else
return NGROUPS_MAX;
#endif
}
int sys_getgroups(int setlen, gid_t *gidset)
{
#if !defined(HAVE_BROKEN_GETGROUPS)
return getgroups(setlen, gidset);
#else
GID_T gid;
GID_T *group_list;
int i, ngroups;
if(setlen == 0) {
return getgroups(setlen, &gid);
}
if(setlen < 0) {
errno = EINVAL;
return -1;
}
if (setlen == 0)
setlen = groups_max();
if((group_list = (GID_T *)malloc(setlen * sizeof(GID_T))) == NULL) {
DEBUG(0,("sys_getgroups: Malloc fail.\n"));
return -1;
}
if((ngroups = getgroups(setlen, group_list)) < 0) {
int saved_errno = errno;
SAFE_FREE(group_list);
errno = saved_errno;
return -1;
}
for(i = 0; i < ngroups; i++)
gidset[i] = (gid_t)group_list[i];
SAFE_FREE(group_list);
return ngroups;
#endif
}
#ifdef HAVE_SETGROUPS
int sys_setgroups(int setlen, gid_t *gidset)
{
#if !defined(HAVE_BROKEN_GETGROUPS)
return setgroups(setlen, gidset);
#else
GID_T *group_list;
int i ;
if (setlen == 0)
return 0 ;
if (setlen < 0 || setlen > groups_max()) {
errno = EINVAL;
return -1;
}
if((group_list = (GID_T *)malloc(setlen * sizeof(GID_T))) == NULL) {
DEBUG(0,("sys_setgroups: Malloc fail.\n"));
return -1;
}
for(i = 0; i < setlen; i++)
group_list[i] = (GID_T) gidset[i];
if(setgroups(setlen, group_list) != 0) {
int saved_errno = errno;
SAFE_FREE(group_list);
errno = saved_errno;
return -1;
}
SAFE_FREE(group_list);
return 0 ;
#endif
}
#endif
struct saved_pw {
fstring pw_name;
fstring pw_passwd;
fstring pw_gecos;
pstring pw_dir;
pstring pw_shell;
struct passwd pass;
};
static struct saved_pw pw_mod;
static struct saved_pw pw_cache;
static int num_lookups;
#ifndef PW_RET_CACHE_MAX_LOOKUPS
#define PW_RET_CACHE_MAX_LOOKUPS 100
#endif
static void copy_pwent(struct saved_pw *dst, struct passwd *pass)
{
memcpy((char *)&dst->pass, pass, sizeof(struct passwd));
fstrcpy(dst->pw_name, pass->pw_name);
dst->pass.pw_name = dst->pw_name;
fstrcpy(dst->pw_passwd, pass->pw_passwd);
dst->pass.pw_passwd = dst->pw_passwd;
fstrcpy(dst->pw_gecos, pass->pw_gecos);
dst->pass.pw_gecos = dst->pw_gecos;
pstrcpy(dst->pw_dir, pass->pw_dir);
dst->pass.pw_dir = dst->pw_dir;
pstrcpy(dst->pw_shell, pass->pw_shell);
dst->pass.pw_shell = dst->pw_shell;
}
static struct passwd *setup_pwret(struct passwd *pass)
{
if (pass == NULL) {
memset(&pw_cache, '\0', sizeof(struct saved_pw));
memset(&pw_mod, '\0', sizeof(struct saved_pw));
num_lookups = 0;
return NULL;
}
copy_pwent( &pw_mod, pass);
if (pass != &pw_cache.pass) {
copy_pwent( &pw_cache, pass);
num_lookups = 1;
} else {
num_lookups++;
num_lookups = (num_lookups % PW_RET_CACHE_MAX_LOOKUPS);
}
return &pw_mod.pass;
}
void sys_setpwent(void)
{
setup_pwret(NULL);
setpwent();
}
struct passwd *sys_getpwent(void)
{
return setup_pwret(getpwent());
}
void sys_endpwent(void)
{
setup_pwret(NULL);
endpwent();
}
struct passwd *sys_getpwnam(const char *name)
{
if (!name || !name[0])
return NULL;
if (num_lookups && pw_cache.pass.pw_name && !strcmp(name, pw_cache.pass.pw_name)) {
return setup_pwret(&pw_cache.pass);
}
return setup_pwret(getpwnam(name));
}
struct passwd *sys_getpwuid(uid_t uid)
{
if (num_lookups && pw_cache.pass.pw_name && (uid == pw_cache.pass.pw_uid)) {
return setup_pwret(&pw_cache.pass);
}
return setup_pwret(getpwuid(uid));
}
#if 0
int wsys_stat(const smb_ucs2_t *wfname,SMB_STRUCT_STAT *sbuf)
{
pstring fname;
return sys_stat(unicode_to_unix(fname,wfname,sizeof(fname)), sbuf);
}
int wsys_lstat(const smb_ucs2_t *wfname,SMB_STRUCT_STAT *sbuf)
{
pstring fname;
return sys_lstat(unicode_to_unix(fname,wfname,sizeof(fname)), sbuf);
}
int wsys_creat(const smb_ucs2_t *wfname, mode_t mode)
{
pstring fname;
return sys_creat(unicode_to_unix(fname,wfname,sizeof(fname)), mode);
}
int wsys_open(const smb_ucs2_t *wfname, int oflag, mode_t mode)
{
pstring fname;
return sys_open(unicode_to_unix(fname,wfname,sizeof(fname)), oflag, mode);
}
FILE *wsys_fopen(const smb_ucs2_t *wfname, const char *type)
{
pstring fname;
return sys_fopen(unicode_to_unix(fname,wfname,sizeof(fname)), type);
}
DIR *wsys_opendir(const smb_ucs2_t *wfname)
{
pstring fname;
return opendir(unicode_to_unix(fname,wfname,sizeof(fname)));
}
SMB_STRUCT_WDIRENT *wsys_readdir(DIR *dirp)
{
static SMB_STRUCT_WDIRENT retval;
SMB_STRUCT_DIRENT *dirval = sys_readdir(dirp);
if(!dirval)
return NULL;
unix_to_unicode(retval.d_name,dirval->d_name,sizeof(retval.d_name));
return &retval;
}
smb_ucs2_t *wsys_getwd(smb_ucs2_t *s)
{
pstring fname;
char *p = sys_getwd(fname);
if(!p)
return NULL;
return unix_to_unicode(s, p, sizeof(wpstring));
}
int wsys_chown(const smb_ucs2_t *wfname, uid_t uid, gid_t gid)
{
pstring fname;
return chown(unicode_to_unix(fname,wfname,sizeof(fname)), uid, gid);
}
int wsys_chroot(const smb_ucs2_t *wfname)
{
pstring fname;
return chroot(unicode_to_unix(fname,wfname,sizeof(fname)));
}
SMB_STRUCT_WPASSWD *wsys_getpwnam(const smb_ucs2_t *wname)
{
static SMB_STRUCT_WPASSWD retval;
fstring name;
struct passwd *pwret = sys_getpwnam(unicode_to_unix(name,wname,sizeof(name)));
if(!pwret)
return NULL;
unix_to_unicode(retval.pw_name, pwret->pw_name, sizeof(retval.pw_name));
retval.pw_passwd = pwret->pw_passwd;
retval.pw_uid = pwret->pw_uid;
retval.pw_gid = pwret->pw_gid;
unix_to_unicode(retval.pw_gecos, pwret->pw_gecos, sizeof(retval.pw_gecos));
unix_to_unicode(retval.pw_dir, pwret->pw_dir, sizeof(retval.pw_dir));
unix_to_unicode(retval.pw_shell, pwret->pw_shell, sizeof(retval.pw_shell));
return &retval;
}
SMB_STRUCT_WPASSWD *wsys_getpwuid(uid_t uid)
{
static SMB_STRUCT_WPASSWD retval;
struct passwd *pwret = sys_getpwuid(uid);
if(!pwret)
return NULL;
unix_to_unicode(retval.pw_name, pwret->pw_name, sizeof(retval.pw_name));
retval.pw_passwd = pwret->pw_passwd;
retval.pw_uid = pwret->pw_uid;
retval.pw_gid = pwret->pw_gid;
unix_to_unicode(retval.pw_gecos, pwret->pw_gecos, sizeof(retval.pw_gecos));
unix_to_unicode(retval.pw_dir, pwret->pw_dir, sizeof(retval.pw_dir));
unix_to_unicode(retval.pw_shell, pwret->pw_shell, sizeof(retval.pw_shell));
return &retval;
}
#endif
static char **extract_args(const char *command)
{
static pstring trunc_cmd;
char *ptr;
int argcl;
char **argl = NULL;
int i;
pstrcpy(trunc_cmd, command);
if(!(ptr = strtok(trunc_cmd, " \t"))) {
errno = EINVAL;
return NULL;
}
for( argcl = 1; ptr; ptr = strtok(NULL, " \t"))
argcl++;
if((argl = (char **)malloc((argcl + 1) * sizeof(char *))) == NULL)
return NULL;
pstrcpy(trunc_cmd, command);
ptr = strtok(trunc_cmd, " \t");
i = 0;
argl[i++] = ptr;
while((ptr = strtok(NULL, " \t")) != NULL)
argl[i++] = ptr;
argl[i++] = NULL;
return argl;
}
static pid_t mypid = (pid_t)-1;
pid_t sys_fork(void)
{
pid_t forkret = fork();
if (forkret == (pid_t)0)
mypid = (pid_t) -1;
return forkret;
}
pid_t sys_getpid(void)
{
if (mypid == (pid_t)-1)
mypid = getpid();
return mypid;
}
typedef struct _popen_list
{
int fd;
pid_t child_pid;
struct _popen_list *next;
} popen_list;
static popen_list *popen_chain;
int sys_popen(const char *command)
{
int parent_end, child_end;
int pipe_fds[2];
popen_list *entry = NULL;
char **argl = NULL;
if (pipe(pipe_fds) < 0)
return -1;
parent_end = pipe_fds[0];
child_end = pipe_fds[1];
if (!*command) {
errno = EINVAL;
goto err_exit;
}
if((entry = (popen_list *)malloc(sizeof(popen_list))) == NULL)
goto err_exit;
ZERO_STRUCTP(entry);
if(!(argl = extract_args(command)))
goto err_exit;
entry->child_pid = sys_fork();
if (entry->child_pid == -1) {
goto err_exit;
}
if (entry->child_pid == 0) {
int child_std_end = STDOUT_FILENO;
popen_list *p;
close(parent_end);
if (child_end != child_std_end) {
dup2 (child_end, child_std_end);
close (child_end);
}
for (p = popen_chain; p; p = p->next)
close(p->fd);
execv(argl[0], argl);
_exit (127);
}
close (child_end);
SAFE_FREE(argl);
entry->next = popen_chain;
popen_chain = entry;
entry->fd = parent_end;
return entry->fd;
err_exit:
SAFE_FREE(entry);
SAFE_FREE(argl);
close(pipe_fds[0]);
close(pipe_fds[1]);
return -1;
}
int sys_pclose(int fd)
{
int wstatus;
popen_list **ptr = &popen_chain;
popen_list *entry = NULL;
pid_t wait_pid;
int status = -1;
for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
if ((*ptr)->fd == fd) {
entry = *ptr;
*ptr = (*ptr)->next;
status = 0;
break;
}
}
if (status < 0 || close(entry->fd) < 0)
return -1;
do {
wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
} while (wait_pid == -1 && errno == EINTR);
SAFE_FREE(entry);
if (wait_pid == -1)
return -1;
return wstatus;
}
void *sys_dlopen(const char *name, int flags)
{
#ifdef HAVE_LIBDL
return dlopen(name, flags);
#else
return NULL;
#endif
}
void *sys_dlsym(void *handle, char *symbol)
{
#ifdef HAVE_LIBDL
return dlsym(handle, symbol);
#else
return NULL;
#endif
}
int sys_dlclose (void *handle)
{
#ifdef HAVE_LIBDL
return dlclose(handle);
#else
return 0;
#endif
}
const char *sys_dlerror(void)
{
#ifdef HAVE_LIBDL
return dlerror();
#else
return NULL;
#endif
}