Hi Isaac,

Thanks. I have incorporated your comments.
Please find the updated files attached herewith.

Regards,
Vivek


On Thu, Feb 6, 2014 at 12:38 AM, Isaac Dunham <[email protected]> wrote:

> On Wed, Feb 05, 2014 at 06:49:03PM +0530, Vivek Bhagat wrote:
> > Hi List,
> >
> > This is my first post on ToyBox community.
> >
> Welcome!
>
> I'm sorry I can't do inline comments this time, between the mime types and
> the editor I'm using; but I'll try to comment.
>
> And thanks for breaking them out into separate patches.
>
> Anyhow, first there are some smaller details that apply to all of them:
>
> 1. It would be nice if in the future new toys were submitted by
> attaching the source code, rather than a patch (eg, freeramdisk.c,
> openvt.c, and deallocvt.c)
>
> 2. Rob puts submissions into toys/pending first and sets them to "default
> n"
>
> 3. For toybox, the standard indentation is 2 spaces for every level.
>
> 4. I note that the NEWTOY line has more options than TOYFLAG_USR &
> TOYFLAG_BIN:
> All of these should have TOYFLAG_NEEDROOT, and freeramdisk really belongs
> in
> /sbin (TOYFLAG_SBIN)
>
> > Please find the patches attached herewith for adding 3 new commands -
> > 1. freeramdisk - If we unmount or detach the RAM disk based file system
> the
> > Linux Kernel
> >    will not free the allocated memory associated with the RAM device.
> This
> > can be useful if
> >    one wants to mount this device again:  All data will be preserved.
> >    If we need to free the memory back to the Kernel, one can use the
> > command: "toybox freeramdisk <RAM device>".
>
> This one looked pretty good.
>
> >
> > 2. openvt - Successfully opens a new virtual terminal as mentioned with
> -c
> > option
> >         otherwise search and open next available VT.
> >  with -s option it switches to new VT
> > with -s -w option, it switch back successfully to originating VT.
>
> A couple of smaller details...
> This bit:
> >+      vt_fd = open(toybuf, O_RDWR);
> >+      if (vt_fd < 0) perror_exit("failed to open /dev/tty%d", TT.vt_num);
> could be replaced with
> vt_fd = xopen(toybuf, O_RDWR);
>
> Also, full POSIX conformance requires STDIN_FILENO to be 0,
> and toybox relies on this quite heavily.
>
> > 3. deallocvt - Deallocate specified virtual teminal.
> >     if no virtual terminal is specified, it deallocates all unused VT.
> >
>
> Uses find_console_fd() from openvt.c, and does not depend on openvt.
> The options are:
> -add "depends on openvt" to the kconfig entry
> -move find_console_fd() to lib/
> -put deallocvt in openvt.c
>
> > Please find the test log attached here for above 3 commands.
> > Your inputs are welcome.
> >
> > Thanks.
> > Vivek
>
>
> Thank you,
> Isaac Dunham
>
/* freeramdisk.c - Free all memory allocated to ramdisk
 *
 * Copyright 2014 Vivek Kumar Bhagat <[email protected]>
 *
 * No Standard

USE_FREERAMDISK(NEWTOY(freeramdisk, "<1>1", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))

config FREERAMDISK
  bool "freeramdisk"
  default y
  help
    usage: freeramdisk <RAM device>

    Free all memory allocated to specified ramdisk
*/

#include "toys.h"

void freeramdisk_main(void)
{
	int fd;

	fd = xopen(toys.optargs[0], O_RDWR);
	xioctl(fd, BLKFLSBUF, toys.optargs[0]);
	xclose(fd);
}
/* openvt.c - Run a program on a new VT
 *
 * Copyright 2014 Vivek Kumar Bhagat <[email protected]>
 *
 * No Standard

USE_OPENVT(NEWTOY(openvt, "c#<1>63sw", TOYFLAG_BIN|TOYFLAG_NEEDROOT))

config OPENVT
  bool "openvt"
  default y
  help
    usage: openvt [-c N] [-s] [-w] [--] [command [command_options]]

    start a program on a new virtual terminal (VT)

    -c N  Use VT N
    -s    Switch to new VT
    -w    Wait for command to exit
          if -s and -w option used together, switch back
          to originating VT when command completes
*/

#define FOR_openvt
#include "toys.h"
#include <linux/vt.h>
#include <linux/kd.h>

GLOBALS(
	unsigned long vt_num;
)

int find_console_fd(void)
{
	char *console_name[] = {"/dev/tty", "/dev/tty0", "/dev/console"};
	int i;
	int fd;
	char arg;

	for (i = 0; i < 3; i++) {
		fd = open(console_name[i], O_RDWR);
		if (fd < 0 && errno == EACCES)
			fd = open(console_name[i], O_RDONLY);

		if (fd < 0 && errno == EACCES)
			fd = open(console_name[i], O_WRONLY);

		if (fd >= 0) {
			arg = 0;
			if (0 == ioctl(fd, KDGKBTYPE, &arg))
				return fd;
			else
				close(fd);
		}
	}

	/* check std fd 0, 1 and 2 */
	for (fd = 0; fd < 3; fd++) {
		arg = 0;
		if (0 == ioctl(fd, KDGKBTYPE, &arg))
			return fd;
	}

	return -1;
}

int xvtnum(int fd)
{
	int ret;

	ret = ioctl(fd, VT_OPENQRY, (int *)&TT.vt_num);
	if (ret != 0 || TT.vt_num <= 0) perror_exit("can't find open VT");

	return TT.vt_num;
}

void openvt_main(void)
{
	int fd = -1, vt_fd = -1, pid, ret = 0;
	struct vt_stat vstate;

	if (!(toys.optflags & FLAG_c)) {
		// check if fd 0,1 or 2 is already opened
		for (fd = 0; fd < 3; fd++)
			if (!ioctl(fd, VT_GETSTATE, &vstate)) {
				ret = xvtnum(fd);
				break;
			}

		// find VT number using /dev/console
		if (!ret) {
			fd = xopen("/dev/console", O_RDONLY | O_NONBLOCK);
			xioctl(fd, VT_GETSTATE, &vstate);
			xvtnum(fd);
		}
	}

	sprintf(toybuf, "/dev/tty%lu", TT.vt_num);
	fd = find_console_fd();
	xioctl(fd, VT_GETSTATE, &vstate);

	close(0);	//new vt becomes stdin
	vt_fd = xopen(toybuf, O_RDWR);
	if (toys.optflags & FLAG_s) {
		ioctl(vt_fd, VT_ACTIVATE, TT.vt_num);
		ioctl(vt_fd, VT_WAITACTIVE, TT.vt_num);
	}

	close(1);
	close(2);
	dup2(vt_fd, 1);
	dup2(vt_fd, 2);
	while (vt_fd > 2)
		close(vt_fd--);

	pid = vfork();
	if (pid < 0)	perror_exit("Fork failed");
	else if (!pid) {
		setsid();
		ioctl(vt_fd, TIOCSCTTY, 0);
		xexec(toys.optargs);
	}

	if (toys.optflags & FLAG_w) {
		while (-1 == waitpid(pid, NULL, 0) && errno == EINTR)
			;
		if (toys.optflags & FLAG_s) {
			ioctl(fd, VT_ACTIVATE, vstate.v_active);
			ioctl(fd, VT_WAITACTIVE, vstate.v_active);
			//check why deallocate isn't working here
			xioctl(fd, VT_DISALLOCATE, (void *)(ptrdiff_t)TT.vt_num); 
		}
	}
}
/* deallocvt.c - Deallocate virtual terminal(s)
 *
 * Copyright 2014 Vivek Kumar Bhagat <[email protected]>
 *
 * No Standard.

USE_DEALLOCVT(NEWTOY(deallocvt, ">1", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_NEEDROOT))

config DEALLOCVT
  bool "deallocvt"
	depends on OPENVT
  default y
  help
		usage: deallocvt [N]

		Deallocate unused virtual terminal /dev/ttyN
		default value of N is 0, deallocate all unused consoles
*/

#include "toys.h"
#include <linux/vt.h>

void deallocvt_main(void)
{
	int fd;

	// 0 : deallocate all unused consoles
	int vt_num = 0;

	if (toys.optargs[0])
		vt_num = atolx_range(toys.optargs[0], 1, 63);

	fd = find_console_fd();
	if (fd < 0)	error_exit("can't open console");

	xioctl(fd, VT_DISALLOCATE, (void *)(ptrdiff_t)vt_num);
}
_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to