1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
/*
* Zapping (TV viewer for the Gnome Desktop)
*
* Copyright (C) 2000 Iñaki García Etxebarria
* Copyright (C) 2003 Michael H. Schimek
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
This is an auxiliary suid program to prepare a kernel video capture
device for DMA overlay onto video memory. When you find any security
flaws here, please report at zapping-misc@lists.sourceforge.net.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/stat.h>
#ifdef HAVE_GETOPT_LONG
# include <getopt.h>
#endif
#include <assert.h>
#include <X11/Xlib.h>
#include "common/intl-priv.h"
#include "zapping_setup_fb.h"
static const char * zsfb_version = "zapping_setup_fb 0.13";
static const char * default_device_name = "/dev/video0";
#ifndef HAVE_PROGRAM_INVOCATION_NAME
char * program_invocation_name;
char * program_invocation_short_name;
#endif
unsigned int uid;
unsigned int euid;
#define VERBOSITY_CHILD_PROCESS -1
#define VERBOSITY_MIN 0
#define VERBOSITY_MAX 3
int verbosity = 1;
/* Legacy verbosity value, used in libtv/screen.c. */
int debug_msg = 0;
/* Log V4L/V4L2 driver responses. */
FILE * device_log_fp = NULL;
void
message (int level,
const char * template,
...)
{
if (VERBOSITY_CHILD_PROCESS == verbosity)
return;
assert (level >= VERBOSITY_MIN
&& level <= VERBOSITY_MAX);
if (verbosity >= level)
{
va_list ap;
va_start (ap, template);
vfprintf (stderr, template, ap);
va_end (ap);
}
}
void
error_message (const char * file,
unsigned int line,
const char * template,
...)
{
va_list ap;
va_start (ap, template);
if (VERBOSITY_CHILD_PROCESS == verbosity)
{
vfprintf (stderr, template, ap);
}
else if (verbosity > 0)
{
fprintf (stderr, "%s:%s:%u: ",
program_invocation_short_name, file, line);
vfprintf (stderr, template, ap);
fputc ('\n', stderr);
}
va_end (ap);
}
#include "common/device.c" /* generic device access routines */
#ifndef major
# define major(dev) ((unsigned int)(((dev) >> 8) & 0xff))
#endif
zsfb_status
device_open_safer (int * fd,
const char * device_name,
int device_fd,
unsigned int major_number,
unsigned int flags)
{
struct stat st;
assert (NULL != fd);
if (NULL != device_name)
{
if (strchr (device_name, '.'))
{
errmsg (_("Device name %s is unsafe, contains dots."),
device_name);
errno = EINVAL;
return ZSFB_BAD_DEVICE_NAME;
}
if (strncmp (device_name, "/dev/", 5))
{
errmsg (_("Device name %s is unsafe, "
"does not begin with /dev/."),
device_name);
errno = EINVAL;
return ZSFB_BAD_DEVICE_NAME;
}
message (/* verbosity */ 1,
"Opening device %s.\n", device_name);
flags |= O_NOCTTY;
flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
*fd = device_open (device_log_fp, device_name, flags, 0600);
if (-1 == *fd)
{
int saved_errno = errno;
/* TRANSLATORS: File name, error message. */
errmsg (_("Cannot open device %s. %s."),
device_name, strerror (saved_errno));
errno = saved_errno;
return ZSFB_OPEN_ERROR;
}
if (-1 == fstat (*fd, &st))
{
int saved_errno = errno;
device_close (device_log_fp, *fd);
*fd = -1;
/* TRANSLATORS: File name, error message. */
errmsg (_("Cannot identify %s. %s."),
device_name, strerror (saved_errno));
errno = saved_errno;
return ZSFB_UNKNOWN_DEVICE;
}
if (!S_ISCHR (st.st_mode))
{
device_close (device_log_fp, *fd);
*fd = -1;
errmsg (_("%s is not a device."),
device_name);
errno = ENODEV;
return ZSFB_UNKNOWN_DEVICE;
}
if (0 != major_number)
{
if (major_number != major (st.st_rdev))
{
device_close (device_log_fp, *fd);
*fd = -1;
errmsg (_("%s is not a video device."),
device_name);
errno = ENODEV;
return ZSFB_UNKNOWN_DEVICE;
}
}
}
else if (-1 != device_fd)
{
if (-1 == fstat (device_fd, &st))
{
int saved_errno = errno;
errmsg (_("Cannot identify file descriptor %d. %s."),
device_fd, strerror (saved_errno));
errno = saved_errno;
if (EBADF == saved_errno)
return ZSFB_BAD_DEVICE_FD;
else
return ZSFB_UNKNOWN_DEVICE;
}
if (!S_ISCHR (st.st_mode))
{
errmsg (_("File descriptor %d is not a device."),
device_fd);
errno = ENODEV;
return ZSFB_UNKNOWN_DEVICE;
}
if (0 != major_number)
{
if (major_number != major (st.st_rdev))
{
errmsg (_("File descriptor %d is not a video device."),
device_fd);
errno = ENODEV;
return ZSFB_UNKNOWN_DEVICE;
}
}
message (/* verbosity */ 1,
"Using device by file descriptor %d.\n",
device_fd);
*fd = device_fd;
}
else
{
errmsg (_("Internal error at %s:%u."),
__FILE__, __LINE__);
errno = 0;
return ZSFB_BUG;
}
return ZSFB_SUCCESS;
}
void
drop_root_privileges (void)
{
if (ROOT_UID == euid && ROOT_UID != uid)
{
message (/* verbosity */ 2,
"Dropping root privileges\n");
if (-1 == seteuid (uid))
{
errmsg (_("Cannot drop root privileges despite "
"running with UID %d, EUID %d."),
uid, euid);
exit (ZSFB_BUG);
}
}
else if (ROOT_UID == uid)
{
if (0) /* cannot distinguish between root and consolehelper */
message (/* verbosity */ 1,
"You should not run %s as root,\n"
"better use consolehelper, sudo, su or set the "
"SUID flag with chmod +s.\n",
program_invocation_name);
}
}
zsfb_status
restore_root_privileges (void)
{
if (ROOT_UID == euid && ROOT_UID != uid)
{
message (/* verbosity */ 2,
"Restoring root privileges\n");
if (-1 == seteuid (euid))
{
int saved_errno = errno;
errmsg (_("Cannot restore root privileges despite "
"running with UID %d, EUID %d."),
uid, euid);
errno = saved_errno;
return ZSFB_NO_PERMISSION;
}
}
return ZSFB_SUCCESS;
}
static const char
short_options [] = "b:cd:f:hqvD:S:V";
#ifdef HAVE_GETOPT_LONG
static const struct option
long_options [] =
{
{ "bpp", required_argument, 0, 'b' },
{ "child", no_argument, 0, 'c' },
{ "device", required_argument, 0, 'd' },
{ "fd", required_argument, 0, 'f' },
{ "help", no_argument, 0, 'h' },
{ "quiet", no_argument, 0, 'q' },
{ "usage", no_argument, 0, 'h' },
{ "verbose", no_argument, 0, 'v' },
{ "display", required_argument, 0, 'D' },
{ "screen", required_argument, 0, 'S' },
{ "version", no_argument, 0, 'V' },
{ 0, 0, 0, 0 }
};
#else
# define getopt_long(ac, av, s, l, i) getopt (ac, av, s)
#endif
static void
usage (FILE * fp)
{
if (VERBOSITY_CHILD_PROCESS == verbosity)
return;
fprintf (fp,
"Usage: %s [OPTIONS]\n"
"Available options:\n"
" -b, --bpp x Color depth hint, bits per pixel on "
" display in question (24 or 32)\n"
" -c, --child Return localized error messages in UTF-8\n"
" encoding to parent process on stderr\n"
" -d, --device name The video device to open, default %s\n"
" -f, --fd number Access video device by file descriptor\n"
" -h, --help, --usage Print this message\n"
" -q, --quiet Decrement verbosity level\n"
" -v, --verbose Increment verbosity level\n"
" -D, --display name The X display to use\n"
" -S, --screen number The X screen to use (Xinerama)\n"
" -V, --version Print the program version and exit\n"
"",
program_invocation_name,
default_device_name);
}
int
main (int argc,
char ** argv)
{
zsfb_status status;
const char *device_name;
int device_fd;
const char *display_name;
int screen_number;
int bpp_arg;
tv_screen *screens;
tv_screen *xs;
status = ZSFB_BUG;
#ifndef HAVE_PROGRAM_INVOCATION_NAME
program_invocation_name = argv[0];
program_invocation_short_name = argv[0];
#endif
/* Make sure fds 0 1 2 are open, otherwise we might end up sending
error messages to the device file. */
{
int flags;
int fd;
for (fd = 0; fd <= 2; ++fd)
if (-1 == fcntl (fd, F_GETFL, &flags))
{
errmsg (_("No standard input, output or error file."));
exit (ZSFB_BUG);
}
}
/* Drop root privileges until we need them. */
uid = getuid ();
euid = geteuid ();
drop_root_privileges ();
/* Parse arguments. */
if (0)
{
unsigned int i;
for (i = 0; i < (unsigned int) argc; ++i)
fprintf (stderr, "argv[%u]='%s'\n", i, argv[i]);
}
device_name = default_device_name;
device_fd = -1;
display_name = getenv ("DISPLAY");
screen_number = -1; /* use default screen of display */
bpp_arg = -1; /* unknown bpp */
assert (verbosity >= VERBOSITY_MIN
&& verbosity <= VERBOSITY_MAX);
for (;;)
{
int c;
c = getopt_long (argc, argv, short_options, long_options, NULL);
if (-1 == c)
break;
switch (c)
{
case 'b':
bpp_arg = strtol (optarg, NULL, 0);
switch (bpp_arg)
{
case 24:
case 32:
break;
default:
errmsg (_("Invalid bpp argument %d. Expected 24 or 32."),
bpp_arg);
status = ZSFB_INVALID_BPP;
goto failure;
}
break;
case 'c':
#ifdef ENABLE_NLS
bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
#endif
verbosity = VERBOSITY_CHILD_PROCESS;
break;
case 'd':
device_name = strdup (optarg);
device_fd = -1;
break;
case 'D':
display_name = strdup (optarg);
break;
case 'f':
device_name = NULL;
device_fd = strtol (optarg, NULL, 0);
if (device_fd <= 2)
{
errmsg (_("Invalid device file descriptor %d."),
device_fd);
exit (ZSFB_BAD_DEVICE_FD);
}
break;
case 'h':
usage (stdout);
exit (ZSFB_SUCCESS);
case 'q':
if (verbosity > VERBOSITY_MIN)
--verbosity;
break;
case 'S':
screen_number = strtol (optarg, NULL, 0);
break;
case 'v':
if (verbosity < VERBOSITY_MAX)
++verbosity;
break;
case 'V':
printf ("%s\n", zsfb_version);
exit (ZSFB_SUCCESS);
default:
/* getopt(_long) prints option name when unknown or arg missing. */
usage (stderr);
goto failure;
}
}
if (verbosity >= 2)
debug_msg = 1;
if (verbosity >= 3)
device_log_fp = stderr;
message (/* verbosity */ 1,
"(C) 2000-2005 Iñaki G. Etxebarria, Michael H. Schimek.\n"
"This program is freely redistributable under the terms\n"
"of the GNU General Public License.\n\n");
message (/* verbosity */ 1,
"Using video device '%s', display '%s', screen %d.\n",
device_name, display_name, screen_number);
message (/* verbosity */ 1,
"Querying frame buffer parameters from X server.\n");
screens = tv_screen_list_new (display_name, bpp_arg);
if (NULL == screens)
{
errmsg (_("No screens found."));
status = ZSFB_NO_SCREEN;
goto failure;
}
for (xs = screens; xs; xs = xs->next)
{
message (/* verbosity */ 2,
"Screen %d:\n"
" position %u, %u - %u, %u\n"
" frame buffer address 0x%lx\n"
" frame buffer size %ux%u pixels, 0x%lx bytes\n"
" bytes per line %lu bytes\n"
" pixfmt %s\n",
xs->screen_number,
xs->x,
xs->y,
xs->x + xs->width,
xs->y + xs->height,
xs->target.base,
xs->target.format.width,
xs->target.format.height,
xs->target.format.size,
xs->target.format.bytes_per_line[0],
tv_pixfmt_name (xs->target.format.pixel_format->pixfmt));
}
if (-1 == screen_number)
{
Display *display;
display = XOpenDisplay (display_name);
if (NULL == display)
{
errmsg (_("Cannot open display %s."),
display_name);
status = ZSFB_NO_SCREEN;
goto failure;
}
screen_number = XDefaultScreen (display);
XCloseDisplay (display);
}
for (xs = screens; xs; xs = xs->next)
if (xs->screen_number == screen_number)
break;
if (NULL == xs)
{
errmsg (_("Screen %d not found."),
screen_number);
status = ZSFB_NO_SCREEN;
goto failure;
}
if (!tv_screen_is_target (xs))
{
errmsg (_("DMA is not possible on screen %d."),
xs->screen_number);
status = ZSFB_OVERLAY_IMPOSSIBLE;
goto failure;
}
status = setup_v4l25 (device_name, device_fd, &xs->target);
if (ZSFB_UNKNOWN_DEVICE == status) /* not Linux 2.6 V4L2 */
{
status = setup_v4l2 (device_name, device_fd, &xs->target);
if (ZSFB_UNKNOWN_DEVICE == status) /* not V4L2 0.20 */
{
status = setup_v4l (device_name, device_fd, &xs->target);
if (ZSFB_UNKNOWN_DEVICE == status) /* not V4L */
{
if (NULL != device_name)
errmsg (_("%s is not a V4L or V4L2 device."),
device_name);
else if (-1 != device_fd)
errmsg (_("File descriptor %d is not a V4L or V4L2 device."),
device_fd);
else
errmsg (_("Cannot identify the video capture device."));
goto failure;
}
}
}
if (ZSFB_SUCCESS != status)
goto failure;
message (/* verbosity */ 1,
"Setup completed.\n");
exit (ZSFB_SUCCESS);
failure:
message (/* verbosity */ 1,
"Setup failed. Try %s -v or -vv for more details.\n",
program_invocation_name);
exit (status);
return EXIT_FAILURE;
}
/*
Local Variables:
coding: utf-8
End:
*/
|