Need FreeBSD wizard for zgv port (long)

Search this archive.

From: Lars Eighner (eighner@io.com)
Date: Thu 12 Oct 2000 - 04:47:19 IST


I am trying to make zgv work with FreeBSD 4.1STABLE.

zgv is an image viewer using sgvalib. 

WARNING:  Serious security issues: blah-blah blah commies
under your bed right now yadda yadda thermonuclear device
blah blah.

I want zgv for a quick image viewer that doesn't have to
fight with a massive bloated gui for resources and the
palette and to use with lynx and similar console programs.

The story so far:

I got the source distribution for zgv-5.1 from
svgalib.org.

I get it to work from a ttyv that root is signed
into, and it is swift, beautiful, wonderful, etc.  But it
is kind of beside the point as the whole idea is to run
it as a regular user.

So basically, I got the trivia out of the way but since
I have no idea what I am doing, I can't solve the
central problem.

Here is where the problem is in the original code:

/* if we're not running directly on a console, try to find a free console
 * and move us to it. Notes old VT so we can switch back to it when finished,
 * if we ran on a different one to start off with.
 *
 * svgalib 1.2.11 and up do something similar (which was based on this,
 * I think), but it doesn't *quite* do what we need (e.g. it changes stdout,
 * which is sensible generally but zgv wants to keep it the same). So, er,
 * that's why this code's still here. :-)
 *
 * NB: *This is run as root*. Think twice before messing with it.
 */
int fixvt()
{
static char vt_filename[128];
struct stat sbuf;
struct vt_stat vts;
int major,minor;
int fd;
int num;

/* see if terminal is a console */
fd=dup(2);
fstat(fd,&sbuf);
major=sbuf.st_rdev>>8;
zgv_vt=minor=sbuf.st_rdev&0xff;
close(fd);
if(major==4 && minor<64)
  return(1);	/* if on a console, already ok */

/* otherwise we need to look for a free VT, redirect std{in,out,err},
 * and switch to it. If there's no free VTs, give up now.
 */

separate_vt=1;

/* still root perms, so this shouldn't be a problem... */
if((fd=open("/dev/console",O_WRONLY))<0) return(0);
ioctl(fd,VT_GETSTATE,&vts);
original_vt=vts.v_active;
ioctl(fd,VT_OPENQRY,&num);
if(num==-1) return(0);	/* no VTs free */

/* now, before we go for it, we test the *current* VT to see if they
 * own it. If so, the user's probably `genuine'.
 * (NB: the kernel now does this, but there's no harm repeating it.)
 */
snprintf(vt_filename,sizeof(vt_filename),"/dev/tty%d",original_vt);
stat(vt_filename,&sbuf);
if(getuid()!=sbuf.st_uid)
  {
  fprintf(stderr,
    "zgv: you must be the owner of the current console to run zgv.\n");
  exit(1);
  }

/* switch to the new VT */
ioctl(fd,VT_ACTIVATE,num);
close(fd);

/* This is incredibly annoying, but the 2.0.x kernel just *will not*
 * work without it. :-(((
 * So, this gives really weird results for `zgv -h' etc., as the parent
 * returns immediately. Redirect stdout if this is a problem.
 */
if(fork()) exit(0);

zgv_vt=num;
sprintf(vt_filename,"/dev/tty%d",num);

setsid();

if(freopen(vt_filename,"r",stdin)==NULL)	return(0);
if(freopen(vt_filename,"w",stderr)==NULL)	return(0);

ioctl(0,VT_WAITACTIVE,num);

/* not needed, but... just in case... */
chown(vt_filename,getuid(),getgid());

/* ok, done it. */
return(1);
}


Here are the changes I have made to it so far: mostly due
to using consio.h instead of vt.h


int fixvt()
{
static char vt_filename[128];
struct stat sbuf;
int vts;
int major,minor;
int fd;
int num;

/* see if terminal is a console */
fd=dup(2);
fstat(fd,&sbuf);
major=sbuf.st_rdev>>8;
zgv_vt=minor=sbuf.st_rdev&0xff;
close(fd);
if(major==12 && minor<64)
  return(1);	/* if on a console, already ok */

/* otherwise we need to look for a free VT, redirect std{in,out,err},
 * and switch to it. If there's no free VTs, give up now.
 */

separate_vt=1;

/* still root perms, so this shouldn't be a problem... */
if((fd=open("/dev/console",O_WRONLY))<0) return(0);
ioctl(fd,VT_GETACTIVE,&vts);
original_vt=vts;
ioctl(fd,VT_OPENQRY,&num);
if(num==-1) return(0);	/* no VTs free */

/* now, before we go for it, we test the *current* VT to see if they
 * own it. If so, the user's probably `genuine'.
 * (NB: the kernel now does this, but there's no harm repeating it.)
 */
snprintf(vt_filename,sizeof(vt_filename),"/dev/ttyv%x",original_vt);
stat(vt_filename,&sbuf);
if(getuid()!=sbuf.st_uid)
  {
  fprintf(stderr,
    "zgv: you must be the owner of the current console to run zgv.\n");
  exit(1);
  }

/* switch to the new VT */
ioctl(fd,VT_ACTIVATE,num);
close(fd);

/* This is incredibly annoying, but the 2.0.x kernel just *will not*
 * work without it. :-(((
 * So, this gives really weird results for `zgv -h' etc., as the parent
 * returns immediately. Redirect stdout if this is a problem.
 */
if(fork()) exit(0);

zgv_vt=num;
sprintf(vt_filename,"/dev/tty%d",num);

setsid();

if(freopen(vt_filename,"r",stdin)==NULL)	return(0);
if(freopen(vt_filename,"w",stderr)==NULL)	return(0);

ioctl(0,VT_WAITACTIVE,num);

/* not needed, but... just in case... */
chown(vt_filename,getuid(),getgid());

/* ok, done it. */
return(1);
}




Known dependencies: svgalib, jpeglib, pnglib, tifflib --
uses gawk as is, but of course it could be awk (just for
man page), gmake, needs texinfo (but this comes with the
FreeBSD distribution, and of course gcc and the usual
build libraries.



zgv/Makefile

line 31: delete src/install-info from src:

This install-info is meant to take the place of texinfo
3.12. Since texinfo is installed with the system in 
FreeBSD (and since I can't get this install-info to
work, and since it has nothing to do with the basic
purpose of zvg) we pretty much have no use for it.

lines 38 & 39:

Comment out the make for src/install-info as above.

zgv/config.mk

line 16: chance PREFIX from /usr to /usr/local

This is a /usr/local sort of program.

line 22: comment out SHARE_INFIX.

man/ and info/ attach directly to /usr/local and not to
/usr/local/share.

line 35: uncomment INFO_DIR_UPDATE=no

We are not using the package's version of install info.

line 48: note you can have pcd support if you install
libpcd.  I didn't want it.

line 58: note - uses rgb.txt from X.  Can be commented out
but then there is no support for xpm named colors.

line 69: Americans and others not using A4 paper will have
to change this if the manual is going to be printed.


zgv/doc/Makefile

line 29: could use awk instead of gawk to avoid having to
install gawk if you don't have it.

lines 32 & 34-36:  This is all very confusing.  Basically,
comment out these lines since we are not using the package
version of install-info.

lines 42-50: comment out for above reason.

line 51: a convenient place to add 
<TAB>install-info zgv $(INFODIR)/dir

lines 52 & 53: comment out some more of the package
install-info stuff

zgv/src/Makefile

line 19 & 20 : add continue slash and add this line to
CFLAGS: -idirafter /usr/local/include -idirafter /usr/local
-L /usr/local/lib

There is bound to be a more elegant way of doing this, but
this lets gmake find the right headers and libraries -
mainly the jpeg, tiff, png, sgva libraries' stuff

line 30: delete (the package) install-info from the all
target list.

As above.  Texinfo is part of the FreeBSD distribution and
we don't need a replacement.

lines 51-52: remove rule for install info

line 55: there is no group root, so change -g root to -g
wheel.

line 72: comment out dependency rule for install-info

zgv/src/readnbkey.c

line 19: include sys/kbio.h instead of linux/kd.h

line 48 & line 52-62: comment out.

This is a bad hack to deal with a bad hack.  For certain
historic and mnemonic reason best known to the author, he
wishes to emmulate the old behavior of keyboards with only
10 F keys so that Shift-F1 = F11 and Shift-F2 = F12.  He is
testing here to see if you do indeed already have an old 10
Fkey keyboard so as not to emulate it if you already have
it.  I don't understand his method here and I can't make it
work, so I have hacked it to report there is a new keyboard
unconditionally.  This breaks it, of course, if you do have
an old keyboard.  It think the odds are very much against
your having an old keyboard attached to a virtual console,
but if you do, tough.

zgv/src/zgv.c

line 36: include sys/consio.h instead of sys/vt.h

line 3111: change struct vt_stat vts to int vts (per
consio.h)

line 3122: change major==4 to major==12 (?) 

I think the major mode of virtual consoles is 12.

line 3133: VT_GETSTATE changed to VT_GETACTIVE (?)

consio doesn't know from VT_GETSTATE.

line 3134: change vts.v_active to vts since vts is an int,
per line 3111 (?)

line 3142: /dev/tty%d to /dev/ttyv%x (?)

I think what we want to do is with ttyvs.


Here are the diffs.  The original distribution is in share/

diff -C 2 -r share/zgv-5.1/Makefile zgv-5.1/Makefile
*** share/zgv-5.1/Makefile	Thu Jun  1 09:48:58 2000
--- zgv-5.1/Makefile	Wed Oct 11 17:25:57 2000
***************
*** 29,33 ****
  all: src man infowarn
  
! src: zgv src/install-info
  
  # We try this the whole time, as the dependancies are a bit
--- 29,33 ----
  all: src man infowarn
  
! src: zgv
  
  # We try this the whole time, as the dependancies are a bit
***************
*** 36,41 ****
  	$(MAKE) -C src zgv
  
! src/install-info: src/install-info.c
! 	$(MAKE) -C src install-info
  
  man: doc/zgv.1
--- 36,41 ----
  	$(MAKE) -C src zgv
  
! #src/install-info: src/install-info.c
! #	$(MAKE) -C src install-info
  
  man: doc/zgv.1
diff -C 2 -r share/zgv-5.1/config.mk zgv-5.1/config.mk
*** share/zgv-5.1/config.mk	Thu Jun  1 09:53:23 2000
--- zgv-5.1/config.mk	Wed Oct 11 20:00:07 2000
***************
*** 14,18 ****
  # Usually it will be simpler to just set PREFIX.
  #
! PREFIX=/usr
  
  # comment this out to use traditional info/man locations, e.g.
--- 14,18 ----
  # Usually it will be simpler to just set PREFIX.
  #
! PREFIX=/usr/local
  
  # comment this out to use traditional info/man locations, e.g.
***************
*** 20,24 ****
  # /usr/share/info and /usr/share/man/man1.
  #
! SHARE_INFIX=/share
  
  BINDIR=$(PREFIX)/bin
--- 20,24 ----
  # /usr/share/info and /usr/share/man/man1.
  #
! #SHARE_INFIX=/share
  
  BINDIR=$(PREFIX)/bin
***************
*** 33,37 ****
  # you) you should uncomment this to prevent `make install' doing that.
  #
! #INFO_DIR_UPDATE=no
  
  
--- 33,37 ----
  # you) you should uncomment this to prevent `make install' doing that.
  #
! INFO_DIR_UPDATE=no
  
  
diff -C 2 -r share/zgv-5.1/doc/Makefile zgv-5.1/doc/Makefile
*** share/zgv-5.1/doc/Makefile	Thu Jun  1 09:52:43 2000
--- zgv-5.1/doc/Makefile	Wed Oct 11 20:19:33 2000
***************
*** 30,38 ****
  
  # I think this one's getting just a tiny bit confusing :-}
! ifeq ($(INFO_DIR_UPDATE),no)
  install:
! else
! install: ../src/install-info
! endif
  	install -m 444 zgv.1 $(MANDIR)
  	install -m 444 zgv zgv-? $(INFODIR)
--- 30,38 ----
  
  # I think this one's getting just a tiny bit confusing :-}
! #ifeq ($(INFO_DIR_UPDATE),no)
  install:
! #else
! #install: ../src/install-info
! #endif
  	install -m 444 zgv.1 $(MANDIR)
  	install -m 444 zgv zgv-? $(INFODIR)
***************
*** 40,55 ****
  # Info always uses a dir file in preference to a dir.gz, so we don't use
  # dir.gz unless it's the only game in town.
! ifneq ($(INFO_DIR_UPDATE),no)
! 	if [ -f $(INFODIR)/dir.gz -a ! -f $(INFODIR)/dir ]; then \
! 	  gzip -d $(INFODIR)/dir.gz; \
! 	  ../src/install-info zgv $(INFODIR)/dir; \
! 	  gzip $(INFODIR)/dir; \
! 	else \
! 	  ../src/install-info zgv $(INFODIR)/dir; \
! 	fi
! endif
! 
! ../src/install-info: ../src/install-info.c
! 	make -C ../src install-info
  
  # can't easily fix dir :-/, but do remove the files.
--- 40,55 ----
  # Info always uses a dir file in preference to a dir.gz, so we don't use
  # dir.gz unless it's the only game in town.
! #ifneq ($(INFO_DIR_UPDATE),no)
! #	if [ -f $(INFODIR)/dir.gz -a ! -f $(INFODIR)/dir ]; then \
! #	  gzip -d $(INFODIR)/dir.gz; \
! #	  ../src/install-info zgv $(INFODIR)/dir; \
! #	  gzip $(INFODIR)/dir; \
! #	else \
! #	  ../src/install-info zgv $(INFODIR)/dir; \
! #	fi
! #endif
! 	install-info $(INFODIR)/zgv $(INFODIR)/dir
! #../src/install-info: ../src/install-info.c
! #	make -C ../src install-info
  
  # can't easily fix dir :-/, but do remove the files.
Only in share/zgv-5.1/doc: zgv
Only in share/zgv-5.1/doc: zgv-1
Only in share/zgv-5.1/doc: zgv-2
Only in share/zgv-5.1/doc: zgv-3
Only in share/zgv-5.1/doc: zgv-4
diff -C 2 -r share/zgv-5.1/src/Makefile zgv-5.1/src/Makefile
*** share/zgv-5.1/src/Makefile	Thu Jun  1 09:51:08 2000
--- zgv-5.1/src/Makefile	Wed Oct 11 18:34:18 2000
***************
*** 17,22 ****
  CFLAGS = $(WARNOPTS) $(PCDDEF) \
  	-DRGB_DB_FILE=\"$(RGB_DB)\" \
! 	-O2 -fomit-frame-pointer -finline-functions
! 
  ZGV_LIBS = -lvgagl -lvga -ljpeg -lpng -lz -lm
  ifeq ($(PCDDEF),-DPCD_SUPPORT)
--- 17,22 ----
  CFLAGS = $(WARNOPTS) $(PCDDEF) \
  	-DRGB_DB_FILE=\"$(RGB_DB)\" \
! 	-O2 -fomit-frame-pointer -finline-functions \
! 	-idirafter /usr/local/include -idirafter /usr/local -L /usr/local/lib
  ZGV_LIBS = -lvgagl -lvga -ljpeg -lpng -lz -lm
  ifeq ($(PCDDEF),-DPCD_SUPPORT)
***************
*** 28,32 ****
  # (It would also be weird to have it made by the `doc' Makefile, IMHO.)
  
! all: zgv install-info
  
  ZGV_OBJS = zgv.o vgadisp.o font.o 3deffects.o mousecur.o \
--- 28,32 ----
  # (It would also be weird to have it made by the `doc' Makefile, IMHO.)
  
! all: zgv 
  
  ZGV_OBJS = zgv.o vgadisp.o font.o 3deffects.o mousecur.o \
***************
*** 49,57 ****
  	$(CC) $(CFLAGS) -o bdf2h bdf2h.o
  
! install-info: install-info.c
! 	$(CC) $(INFODIRDEF) -o install-info install-info.c
  
  install: all
! 	install -m 4511 -o root -g root -s zgv $(BINDIR)
  
  uninstall:
--- 49,57 ----
  	$(CC) $(CFLAGS) -o bdf2h bdf2h.o
  
! #install-info: install-info.c
! #	$(CC) $(INFODIRDEF) -o install-info install-info.c
  
  install: all
! 	install -m 4511 -o root -g wheel -s zgv $(BINDIR)
  
  uninstall:
***************
*** 70,74 ****
  helppage.o: helppage.c 3deffects.h font.h readnbkey.h rc_config.h \
   rcfile.h mousecur.h zgv.h
! install-info.o: install-info.c
  magic.o: magic.c magic.h
  mousecur.o: mousecur.c rc_config.h rcfile.h zgv.h
--- 70,74 ----
  helppage.o: helppage.c 3deffects.h font.h readnbkey.h rc_config.h \
   rcfile.h mousecur.h zgv.h
! #install-info.o: install-info.c
  magic.o: magic.c magic.h
  mousecur.o: mousecur.c rc_config.h rcfile.h zgv.h
diff -C 2 -r share/zgv-5.1/src/readnbkey.c zgv-5.1/src/readnbkey.c
*** share/zgv-5.1/src/readnbkey.c	Thu Mar  9 22:02:03 2000
--- zgv-5.1/src/readnbkey.c	Wed Oct 11 18:47:36 2000
***************
*** 17,21 ****
  #include <vgamouse.h>
  #include <vgakeyboard.h>	/* for SCANCODE_{F1,F11} */
! #include <linux/kd.h>
  #include "zgv.h"
  #include "readnbkey.h"
--- 17,21 ----
  #include <vgamouse.h>
  #include <vgakeyboard.h>	/* for SCANCODE_{F1,F11} */
! #include <sys/kbio.h>       /* #include <linux/kd.h> */
  #include "zgv.h"
  #include "readnbkey.h"
***************
*** 46,55 ****
  int is_logical_keymap(int ttyfd)
  {
! struct kbentry ent1,ent2;
  
  /* this is horrible, but I really do need the scancodes to check this :-(
   * as the mapping is done at that level.
   */
! ent1.kb_table=K_NORMTAB;
  ent1.kb_index=SCANCODE_F11;
  ent2.kb_table=K_SHIFTTAB;
--- 46,55 ----
  int is_logical_keymap(int ttyfd)
  {
! /* struct kbentry ent1,ent2; */
  
  /* this is horrible, but I really do need the scancodes to check this :-(
   * as the mapping is done at that level.
   */
! /* ent1.kb_table=K_NORMTAB;
  ent1.kb_index=SCANCODE_F11;
  ent2.kb_table=K_SHIFTTAB;
***************
*** 60,64 ****
  
  if(ent1.kb_value==ent2.kb_value)
!   return(0);
  
  return(1);
--- 60,64 ----
  
  if(ent1.kb_value==ent2.kb_value)
!   return(0); */
  
  return(1);
diff -C 2 -r share/zgv-5.1/src/zgv.c zgv-5.1/src/zgv.c
*** share/zgv-5.1/src/zgv.c	Thu Apr 13 11:02:18 2000
--- zgv-5.1/src/zgv.c	Wed Oct 11 19:14:14 2000
***************
*** 34,38 ****
  #include <sys/file.h>
  #include <sys/ioctl.h>
! #include <sys/vt.h>
  #include <errno.h>
  #include <vga.h>
--- 34,38 ----
  #include <sys/file.h>
  #include <sys/ioctl.h>
! #include <sys/consio.h>  /* #include <sys/vt.h> */
  #include <errno.h>
  #include <vga.h>
***************
*** 3109,3113 ****
  static char vt_filename[128];
  struct stat sbuf;
! struct vt_stat vts;
  int major,minor;
  int fd;
--- 3109,3113 ----
  static char vt_filename[128];
  struct stat sbuf;
! int vts;
  int major,minor;
  int fd;
***************
*** 3120,3124 ****
  zgv_vt=minor=sbuf.st_rdev&0xff;
  close(fd);
! if(major==4 && minor<64)
    return(1);	/* if on a console, already ok */
  
--- 3120,3124 ----
  zgv_vt=minor=sbuf.st_rdev&0xff;
  close(fd);
! if(major==12 && minor<64)
    return(1);	/* if on a console, already ok */
  
***************
*** 3131,3136 ****
  /* still root perms, so this shouldn't be a problem... */
  if((fd=open("/dev/console",O_WRONLY))<0) return(0);
! ioctl(fd,VT_GETSTATE,&vts);
! original_vt=vts.v_active;
  ioctl(fd,VT_OPENQRY,&num);
  if(num==-1) return(0);	/* no VTs free */
--- 3131,3136 ----
  /* still root perms, so this shouldn't be a problem... */
  if((fd=open("/dev/console",O_WRONLY))<0) return(0);
! ioctl(fd,VT_GETACTIVE,&vts);
! original_vt=vts;
  ioctl(fd,VT_OPENQRY,&num);
  if(num==-1) return(0);	/* no VTs free */
***************
*** 3140,3144 ****
   * (NB: the kernel now does this, but there's no harm repeating it.)
   */
! snprintf(vt_filename,sizeof(vt_filename),"/dev/tty%d",original_vt);
  stat(vt_filename,&sbuf);
  if(getuid()!=sbuf.st_uid)
--- 3140,3144 ----
   * (NB: the kernel now does this, but there's no harm repeating it.)
   */
! snprintf(vt_filename,sizeof(vt_filename),"/dev/ttyv%x",original_vt);
  stat(vt_filename,&sbuf);
  if(getuid()!=sbuf.st_uid)


-- 
Lars Eighner
eighner@io.com
http://www.io.com/~eighner/index.html


------------------------------------------------------------------
Unsubscribe:  To:   listbot@svgalib.org
              Body: unsubscribe linux-svgalib


This archive was generated by hypermail 2.1.4 : Wed 21 Jan 2004 - 22:10:23 IST