File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / aitio.c
Revision 1.18: download - view: text, annotated - select for diffs - revision graph
Thu Aug 18 09:06:31 2016 UTC (7 years, 9 months ago) by misho
Branches: MAIN
CVS tags: io7_4, IO7_3, HEAD
version 7.3

/*************************************************************************
* (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
*  by Michael Pounov <misho@elwix.org>
*
* $Author: misho $
* $Id: aitio.c,v 1.18 2016/08/18 09:06:31 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004 - 2016
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "global.h"


#pragma GCC visibility push(hidden)

int io_Errno;
char io_Error[STRSIZ];

#pragma GCC visibility pop


// io_GetErrno() Get error code of last operation
int
io_GetErrno()
{
	return io_Errno;
}

// io_GetError() Get error text of last operation
const char *
io_GetError()
{
	return io_Error;
}

// io_SetErr() Set error to variables for internal use!!!
void
io_SetErr(int eno, char *estr, ...)
{
	va_list lst;

	io_Errno = eno;
	memset(io_Error, 0, sizeof io_Error);
	va_start(lst, estr);
	vsnprintf(io_Error, sizeof io_Error, estr, lst);
	va_end(lst);
}


/*
 * ioPromptRead() - Read data from input h[0] with prompt to output h[1]
 *
 * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
 * @csPrompt = Prompt before input, may be NULL
 * @psData = Readed data
 * @dataLen = Length of data
 * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
*/
int
ioPromptRead(int *h, const char *csPrompt, char * __restrict psData, int dataLen)
{
	int ok = 0;
	FILE *inp, *out;
	char szLine[BUFSIZ], *pos;

	if (!psData || !dataLen)
		return -1;

	inp = fdopen(!h ? 0 : h[0], "r");
	if (!inp) {
		LOGERR;
		return -1;
	}
	out = fdopen(!h ? 1 : h[1], "w");
	if (!out) {
		LOGERR;
		return -1;
	}

	while (!ok) {
		if (csPrompt) {
			fprintf(out, "%s", csPrompt);
			fflush(out);
		}

		memset(szLine, 0, BUFSIZ);
		if (!fgets(szLine, BUFSIZ, inp)) {
			clearerr(inp);
#ifdef HAVE_FPURGE
			fpurge(out);
#else
			__fpurge(out);
#endif
			fflush(out);
			return 0;
		}

		if ((pos = strchr(szLine, '\n')))
			*pos = 0;

		strlcpy(psData, szLine, dataLen);
		ok = 1;
	}

	return pos - szLine;
}

/*
 * ioPromptPassword() - Read password from input h[0] with prompt to output h[1]
 *
 * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
 * @csPrompt = Prompt before input, may be NULL
 * @psPass = Readed password
 * @passLen = Length of password
 * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
 * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
*/
int
ioPromptPassword(int *h, const char *csPrompt, char * __restrict psPass, int passLen, int confirm)
{
	int ret, ok = 0;
	FILE *inp, *out;
	char szLine[2][STRSIZ];
#ifndef __linux__
	struct sgttyb tty_state;
#else
	struct termios o;
#endif

	if (!psPass || !passLen)
		return -1;

	inp = fdopen(!h ? 0 : h[0], "r");
	if (!inp) {
		LOGERR;
		return -1;
	}
	out = fdopen(!h ? 1 : h[1], "w");
	if (!out) {
		LOGERR;
		return -1;
	}

#ifndef __linux__
	if (ioctl(fileno(inp), TIOCGETP, &tty_state) == -1) {
		LOGERR;
		return -1;
	} else {
		tty_state.sg_flags &= ~ECHO;
		if (ioctl(fileno(inp), TIOCSETP, &tty_state) == -1) {
			LOGERR;
			return -1;
		}
	}
#else
	if (tcgetattr(fileno(inp), &o) == -1) {
		LOGERR;
		return -1;
	} else {
		o.c_lflag &= ~ECHO;
		if (tcsetattr(fileno(inp), TCSANOW, &o) == -1) {
			LOGERR;
			return -1;
		}
	}
#endif

	while (!ok) {
		switch ((ret = ioPromptRead(h, (!csPrompt || !*csPrompt) ? "Password:" : csPrompt, 
						szLine[0], STRSIZ))) {
			case -1:
				LOGERR;
				ok = -1;
			case 0:
				goto next;
		}
		if (confirm) {
			fprintf(out, "\n");
			fflush(out);

			switch (ioPromptRead(h, "Password confirm:", szLine[1], STRSIZ)) {
				case -1:
					LOGERR;
					ok = -1;
					goto next;
				case 0:
				default:
					if (strcmp(szLine[0], szLine[1])) {
						fprintf(out, "\n\07\07Mismatch - Try again!\n");
						fflush(out);
						continue;
					}
			}
		}

		strlcpy(psPass, szLine[0], passLen);
		ok = ret;
		fprintf(out, "\n");
		fflush(out);
	}

next:
#ifndef __linux__
	tty_state.sg_flags |= ECHO;
	if (ioctl(fileno(inp), TIOCSETP, &tty_state) == -1) {
		LOGERR;
		return -1;
	}
#else
	o.c_lflag |= ECHO;
	if (tcsetattr(fileno(inp), TCSANOW, &o) == -1) {
		LOGERR;
		return -1;
	}
#endif

	return ok;
}

/*
 * ioMkDir() - Function for racursive directory creation and validation
 *
 * @csDir = Full directory path
 * @mode = Mode for directory creation if missing dir
 * return: -1 error, 0 directory path exist, >0 created missing dirs
*/
int
ioMkDir(const char *csDir, int mode)
{
	char *str, *s, *pbrk, szOld[MAXPATHLEN] = { 0 };
	register int cx = -1;

	if (!csDir)
		return cx;

	str = e_strdup(csDir);
	if (!str) {
		LOGERR;
		return cx;
	}

	getcwd(szOld, MAXPATHLEN);
	if (*str == '/')
		chdir("/");

	for (cx = 0, s = strtok_r(str, "/", &pbrk); s; s = strtok_r(NULL, "/", &pbrk)) {
		if (mkdir(s, mode) == -1) {
			if (errno != EEXIST) {
				LOGERR;
				cx = -1;
				goto end;
			}
		} else
			cx++;

		if (chdir(s) == -1) {
			LOGERR;
			cx = -1;
			goto end;
		}
	}
end:
	chdir(szOld);
	e_free(str);
	return cx;
}

#ifndef __linux__
static int
watchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp))
{
	glob_t g[2] = {{ 0 }, { 0 }};
	int d, kq, n = 0;
	register int j, i;
	struct kevent req, chg;
	char wrk[MAXPATHLEN * 2], str[MAXPATHLEN] = { 0 };

	if (!csDir || !callback)
		return 0;

	strlcpy(str, csDir, MAXPATHLEN);
	strlcat(str, "/*", MAXPATHLEN);

	d = open(csDir, O_RDONLY);
	if (d == -1) {
		LOGERR;
		return -1;
	}

	kq = kqueue();
	if (kq == -1) {
		LOGERR;
		close(d);
		return -1;
	}

	EV_SET(&req, d, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, 0);

	if ((n = glob(str, GLOB_NOCHECK, NULL, &g[0]))) {
		LOGERR;
		close(kq);
		close(d);
		return -1;
	} /*else
		ioDEBUG(3, "Start files %d in %s\n", g[0].gl_matchc, str);*/

	while (kevent(kq, &req, 1, &chg, 1, NULL) > 0) {
		/*ioDEBUG(1, "Event:: req=0x%x -> chg=0x%x data=%x\n", req.fflags, chg.fflags, chg.data);*/

		if (!glob(str, GLOB_NOCHECK, NULL, &g[1])) {
			/*ioDEBUG(3, "Diffs %d <> %d\n", g[0].gl_matchc, g[1].gl_matchc);*/

			if (g[0].gl_matchc != g[1].gl_matchc) {
				/* find new items */
				for (j = 0; j < g[1].gl_matchc; j++) {
					for (i = 0; i < g[0].gl_matchc; i++)
						if (!strcmp(g[0].gl_pathv[i], g[1].gl_pathv[j]))
							break;
					if (i == g[0].gl_matchc) {
						if (callback(g[1].gl_pathv[j], 1) < 0)
							break;
						else
							n++;
					}
				}
				/* find del items */
				for (j = 0; j < g[0].gl_matchc; j++) {
					for (i = 0; i < g[1].gl_matchc; i++)
						if (!strcmp(g[1].gl_pathv[i], g[0].gl_pathv[j]))
							break;
					if (i == g[1].gl_matchc) {
						if (callback(g[0].gl_pathv[j], -1) < 0)
							break;
						else
							n++;
					}
				}
			} else {
				/* find chg from items */
				for (j = 0; j < g[0].gl_matchc; j++) {
					for (i = 0; i < g[1].gl_matchc; i++)
						if (!strcmp(g[1].gl_pathv[i], g[0].gl_pathv[j]))
							break;
					if (i == g[1].gl_matchc) {
						strlcpy(wrk, g[0].gl_pathv[j], sizeof wrk);
						strlcat(wrk, ":", sizeof wrk);
					}
				}
				/* find chg to items */
				for (j = 0; j < g[1].gl_matchc; j++) {
					for (i = 0; i < g[0].gl_matchc; i++)
						if (!strcmp(g[0].gl_pathv[i], g[1].gl_pathv[j]))
							break;
					if (i == g[0].gl_matchc) {
						strlcat(wrk, g[1].gl_pathv[j], sizeof wrk);
						if (callback(wrk, 0) < 0)
							break;
						else
							n++;
					}
				}
			}

			globfree(&g[0]);
			g[0] = g[1];
		}
	}

	globfree(&g[0]);
	close(kq);
	close(d);
	return n;
}
#else
static int
watchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp))
{
	int d, in, rlen, n = 0;
	register int i = 0;
	struct inotify_event *evt;
	char buf[BUFSIZ * (sizeof(struct inotify_event) + 16)];

	if (!csDir || !callback)
		return 0;

	in = inotify_init();
	if (in == -1) {
		LOGERR;
		return -1;
	}

	d = inotify_add_watch(in, csDir, IN_CREATE | IN_DELETE | IN_MOVE);

	while ((rlen = read(in, buf, sizeof buf)) > 0) {
		if (i >= rlen)
			break;
		else
			evt = (struct inotify_event*) &buf[i];

		if (evt->len) {
			if (evt->mask & IN_CREATE) {
				if (callback(evt->name, 1) < 0)
					break;
				else
					n++;
			} else if (evt->mask & IN_DELETE) {
				if (callback(evt->name, -1) < 0)
					break;
				else
					n++;
			} else if (evt->mask & IN_MOVE) {
				if (callback(evt->name, 0) < 0)
					break;
				else
					n++;
			}
		}

		i += sizeof (struct inotify_event) + evt->len;
	}

	inotify_rm_watch(in, d);
	close(in);
	return n;
}
#endif

/*
 * ioWatchDirLoop() - Function for watching changes in directory and fire callback
 *
 * @csDir = Full directory path
 * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
 * return: -1 error, !=-1 ok, number of total signaled events
*/
int
ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp))
{
	return watchDirLoop(csDir, callback);
}

/*
 * ioCreatePIDFile() - Create PID file
 *
 * @csName = PID filename
 * @ifExists = !=0 if filename exists return error
 * return: -1 error or 0 ok
 */
int
ioCreatePIDFile(const char *csName, int ifExists)
{
	int fd;
	char str[STRSIZ] = { 0 };

	if (!csName)
		return -1;

	fd = open(csName, O_WRONLY | O_CREAT | (ifExists ? O_EXCL : 0), 0644);
	if (fd == -1) {
		LOGERR;
		return -1;
	}
	snprintf(str, sizeof str, "%d", getpid());
	write(fd, str, strlen(str));
	close(fd);
	return 0;
}


/*
 * ioSendFile() - AITNET sendfile() userland implementation, not dependant from OS
 *
 * @s = socket
 * @csFile = file for send
 * @sendLen = bytes to send, if 0 send all data
 * @offset = start file offset
 * @sndbuf = SO_SNDBUF value, if 0 use default
 * return: 0 error, >0 ok, sended bytes
 */
size_t
ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset, int sndbuf)
{
	void *addr;
	int fd;
	size_t len = 0;
	register size_t off = 0;

	if (!csFile)
		return 0;

	if (sndbuf)
		if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof sndbuf) == -1) {
			LOGERR;
			return 0;
		}

	fd = open(csFile, O_RDONLY);
	if (fd == -1) {
		LOGERR;
		return 0;
	}
	if (!sendLen) {
		sendLen = lseek(fd, 0, SEEK_END);
		if (sendLen == -1) {
			LOGERR;
			close(fd);
			return 0;
		}
	}
	addr = mmap(NULL, sendLen, PROT_READ, MAP_SHARED, fd, offset);
	if (addr == MAP_FAILED) {
		LOGERR;
		close(fd);
		return 0;
	} else
		close(fd);

	while (off < sendLen && (len = write(s, addr + off, sendLen - off)) != -1)
		off += len;
	if (len == -1) {
		LOGERR;
		munmap(addr, sendLen);
		return 0;
	} else
		len = off;

	if (len != sendLen) {
		io_SetErr(ECANCELED, "Different sizes - request %u bytes, actually sended %u bytes\n", 
				sendLen, len);
		len ^= len;
	}

	munmap(addr, sendLen);
	return len;
}

/*
 * ioRecvFile() - Receive file from socket, fastest (zero-copy) way
 *
 * @s = socket
 * @csFile = file for receive
 * @recvLen = receive bytes
 * @over = overwrite file if exists with mode like 0644
 * @rcvbuf = SO_RCVBUF value, if 0 use default
 * return: 0 error, >0 ok, received bytes
 */
size_t
ioRecvFile(int s, const char *csFile, size_t recvLen, int over, int rcvbuf)
{
	void *addr;
	int fd;
	size_t len = 0;
	register size_t off = 0;
	struct pollfd pfd = { s, POLLIN | POLLPRI, 0 };

	if (!csFile || !recvLen)
		return 0;
	if (!over && !access(csFile, F_OK))
		return 0;

	if (rcvbuf)
		if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof rcvbuf) == -1) {
			LOGERR;
			return 0;
		}

	fd = open(csFile, O_WRONLY | O_CREAT | O_TRUNC, over);
	if (fd == -1) {
		LOGERR;
		unlink(csFile);
		return 0;
	}
	if (ftruncate(fd, recvLen) == -1) {
		LOGERR;
		close(fd);
		unlink(csFile);
		return 0;
	}
	addr = mmap(NULL, recvLen, PROT_WRITE, MAP_SHARED, fd, 0);
	if (addr == MAP_FAILED) {
		LOGERR;
		close(fd);
		unlink(csFile);
		return 0;
	} else
		close(fd);

	while (off < recvLen && poll(&pfd, 1, RECV_TIMEOUT) != -1)
		while (off < recvLen && (len = read(s, addr + off, recvLen - off)) != -1)
			off += len;
	if (len == -1) {
		LOGERR;
		munmap(addr, recvLen);
		unlink(csFile);
		return 0;
	} else
		len = off;

	if (len != recvLen)
		io_SetErr(EAGAIN, "Different sizes - request %u bytes, actually received %u bytes\n", 
				recvLen, len);

	munmap(addr, recvLen);
	return len;
}

/*
 * ioRealFileName() - Get real file name
 *
 * @fname = filename
 * return: =NULL error or !=NULL real filename, should be free with e_free()
 */
char *
ioRealFileName(const char *fname)
{
	char *str = NULL;
	struct stat sb;

	if (!fname)
		return NULL;

	str = e_malloc(MAXPATHLEN);
	if (!str) {
		io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
		return NULL;
	} else
		memset(str, 0, MAXPATHLEN);
	if (readlink(fname, str, MAXPATHLEN) == -1) {
		if (stat(fname, &sb) == -1) {
			LOGERR;
			e_free(str);
			return NULL;
		} else
			strlcpy(str, fname, MAXPATHLEN);
	}

	return str;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>