| 1 | /* |
| 2 | * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany |
| 3 | * Copyright 1993 by David Wexelblat <dwex@goblin.org> |
| 4 | * Copyright 1999 by David Holland <davidh@iquest.net> |
| 5 | * |
| 6 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 7 | * documentation for any purpose is hereby granted without fee, provided that |
| 8 | * the above copyright notice appear in all copies and that both that copyright |
| 9 | * notice and this permission notice appear in supporting documentation, and |
| 10 | * that the names of the copyright holders not be used in advertising or |
| 11 | * publicity pertaining to distribution of the software without specific, |
| 12 | * written prior permission. The copyright holders make no representations |
| 13 | * about the suitability of this software for any purpose. It is provided "as |
| 14 | * is" without express or implied warranty. |
| 15 | * |
| 16 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 17 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND IN NO |
| 18 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 19 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 20 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 21 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 22 | * OF THIS SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #ifdef HAVE_XORG_CONFIG_H |
| 26 | #include <xorg-config.h> |
| 27 | #endif |
| 28 | |
| 29 | #include "xf86.h" |
| 30 | #include "xf86Priv.h" |
| 31 | #include "xf86_OSlib.h" |
| 32 | #ifdef HAVE_SYS_KD_H |
| 33 | #include <sys/kd.h> |
| 34 | #endif |
| 35 | |
| 36 | /* |
| 37 | * Applications see VT number as consecutive integers starting from 1. |
| 38 | * VT number VT device |
| 39 | * ------------------------------------------------------- |
| 40 | * 1 : /dev/vt/0 (Alt + Ctrl + F1) |
| 41 | * 2 : /dev/vt/2 (Alt + Ctrl + F2) |
| 42 | * 3 : /dev/vt/3 (Alt + Ctrl + F3) |
| 43 | * ... ... |
| 44 | */ |
| 45 | #define CONSOLE_VTNO 1 |
| 46 | #define SOL_CONSOLE_DEV "/dev/console" |
| 47 | |
| 48 | static Bool KeepTty = FALSE; |
| 49 | static Bool Protect0 = FALSE; |
| 50 | static Bool UseConsole = FALSE; |
| 51 | |
| 52 | #ifdef HAS_USL_VTS |
| 53 | static int VTnum = -1; |
| 54 | static int xf86StartVT = -1; |
| 55 | static int vtEnabled = 0; |
| 56 | extern void xf86VTAcquire(int); |
| 57 | extern void xf86VTRelease(int); |
| 58 | #endif |
| 59 | |
| 60 | /* Device to open as xf86Info.consoleFd */ |
| 61 | static char consoleDev[PATH_MAX] = "/dev/fb"; |
| 62 | |
| 63 | /* Set by -dev argument on CLI |
| 64 | Used by hw/xfree86/common/xf86AutoConfig.c for VIS_GETIDENTIFIER */ |
| 65 | _X_HIDDEN char xf86SolarisFbDev[PATH_MAX] = "/dev/fb"; |
| 66 | |
| 67 | static void |
| 68 | switch_to(int vt, const char *from) |
| 69 | { |
| 70 | int ret; |
| 71 | |
| 72 | SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_ACTIVATE, vt)); |
| 73 | if (ret != 0) |
| 74 | xf86Msg(X_WARNING, "%s: VT_ACTIVATE failed: %s\n", |
| 75 | from, strerror(errno)); |
| 76 | |
| 77 | SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_WAITACTIVE, vt)); |
| 78 | if (ret != 0) |
| 79 | xf86Msg(X_WARNING, "%s: VT_WAITACTIVE failed: %s\n", |
| 80 | from, strerror(errno)); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | xf86OpenConsole(void) |
| 85 | { |
| 86 | int i; |
| 87 | |
| 88 | #ifdef HAS_USL_VTS |
| 89 | int fd; |
| 90 | struct vt_mode VT; |
| 91 | struct vt_stat vtinfo; |
| 92 | MessageType from = X_PROBED; |
| 93 | #endif |
| 94 | |
| 95 | if (serverGeneration == 1) { |
| 96 | /* Check if we're run with euid==0 */ |
| 97 | if (geteuid() != 0) |
| 98 | FatalError("xf86OpenConsole: Server must be suid root\n"); |
| 99 | |
| 100 | /* Protect page 0 to help find NULL dereferencing */ |
| 101 | /* mprotect() doesn't seem to work */ |
| 102 | if (Protect0) { |
| 103 | int fd = -1; |
| 104 | |
| 105 | if ((fd = open("/dev/zero", O_RDONLY, 0)) < 0) { |
| 106 | xf86Msg(X_WARNING, |
| 107 | "xf86OpenConsole: cannot open /dev/zero (%s)\n", |
| 108 | strerror(errno)); |
| 109 | } |
| 110 | else { |
| 111 | if (mmap(0, 0x1000, PROT_NONE, |
| 112 | MAP_FIXED | MAP_SHARED, fd, 0) == MAP_FAILED) |
| 113 | xf86Msg(X_WARNING, |
| 114 | "xf86OpenConsole: failed to protect page 0 (%s)\n", |
| 115 | strerror(errno)); |
| 116 | |
| 117 | close(fd); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | #ifdef HAS_USL_VTS |
| 122 | |
| 123 | /* |
| 124 | * Setup the virtual terminal manager |
| 125 | */ |
| 126 | if ((fd = open("/dev/vt/0", O_RDWR, 0)) == -1) { |
| 127 | xf86ErrorF("xf86OpenConsole: Cannot open /dev/vt/0 (%s)\n", |
| 128 | strerror(errno)); |
| 129 | vtEnabled = 0; |
| 130 | } |
| 131 | else { |
| 132 | if (ioctl(fd, VT_ENABLED, &vtEnabled) < 0) { |
| 133 | xf86ErrorF("xf86OpenConsole: VT_ENABLED failed (%s)\n", |
| 134 | strerror(errno)); |
| 135 | vtEnabled = 0; |
| 136 | } |
| 137 | } |
| 138 | #endif /* HAS_USL_VTS */ |
| 139 | |
| 140 | if (UseConsole) { |
| 141 | strlcpy(consoleDev, SOL_CONSOLE_DEV, sizeof(consoleDev)); |
| 142 | |
| 143 | #ifdef HAS_USL_VTS |
| 144 | xf86Info.vtno = CONSOLE_VTNO; |
| 145 | |
| 146 | if (vtEnabled == 0) { |
| 147 | xf86StartVT = 0; |
| 148 | } |
| 149 | else { |
| 150 | if (ioctl(fd, VT_GETSTATE, &vtinfo) < 0) |
| 151 | FatalError |
| 152 | ("xf86OpenConsole: Cannot determine current VT\n"); |
| 153 | xf86StartVT = vtinfo.v_active; |
| 154 | } |
| 155 | #endif /* HAS_USL_VTS */ |
| 156 | goto OPENCONSOLE; |
| 157 | } |
| 158 | |
| 159 | #ifdef HAS_USL_VTS |
| 160 | if (vtEnabled == 0) { |
| 161 | /* VT not enabled - kernel too old or Sparc platforms |
| 162 | without visual_io support */ |
| 163 | xf86Msg(from, "VT infrastructure is not available\n"); |
| 164 | |
| 165 | xf86StartVT = 0; |
| 166 | xf86Info.vtno = 0; |
| 167 | strlcpy(consoleDev, xf86SolarisFbDev, sizeof(consoleDev)); |
| 168 | goto OPENCONSOLE; |
| 169 | } |
| 170 | |
| 171 | if (ioctl(fd, VT_GETSTATE, &vtinfo) < 0) |
| 172 | FatalError("xf86OpenConsole: Cannot determine current VT\n"); |
| 173 | |
| 174 | xf86StartVT = vtinfo.v_active; |
| 175 | |
| 176 | if (VTnum != -1) { |
| 177 | xf86Info.vtno = VTnum; |
| 178 | from = X_CMDLINE; |
| 179 | } |
| 180 | else if (xf86Info.ShareVTs) { |
| 181 | xf86Info.vtno = vtinfo.v_active; |
| 182 | from = X_CMDLINE; |
| 183 | } |
| 184 | else { |
| 185 | if ((ioctl(fd, VT_OPENQRY, &xf86Info.vtno) < 0) || |
| 186 | (xf86Info.vtno == -1)) { |
| 187 | FatalError("xf86OpenConsole: Cannot find a free VT\n"); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | xf86Msg(from, "using VT number %d\n\n", xf86Info.vtno); |
| 192 | snprintf(consoleDev, PATH_MAX, "/dev/vt/%d", xf86Info.vtno); |
| 193 | |
| 194 | if (fd != -1) { |
| 195 | close(fd); |
| 196 | } |
| 197 | |
| 198 | #endif /* HAS_USL_VTS */ |
| 199 | |
| 200 | OPENCONSOLE: |
| 201 | if (!KeepTty) |
| 202 | setpgrp(); |
| 203 | |
| 204 | if (((xf86Info.consoleFd = open(consoleDev, O_RDWR | O_NDELAY, 0)) < 0)) |
| 205 | FatalError("xf86OpenConsole: Cannot open %s (%s)\n", |
| 206 | consoleDev, strerror(errno)); |
| 207 | |
| 208 | /* Change ownership of the vt or console */ |
| 209 | chown(consoleDev, getuid(), getgid()); |
| 210 | |
| 211 | #ifdef HAS_USL_VTS |
| 212 | if (xf86Info.ShareVTs) |
| 213 | return; |
| 214 | |
| 215 | if (vtEnabled) { |
| 216 | /* |
| 217 | * Now get the VT |
| 218 | */ |
| 219 | switch_to(xf86Info.vtno, "xf86OpenConsole"); |
| 220 | |
| 221 | #ifdef VT_SET_CONSUSER /* added in snv_139 */ |
| 222 | if (strcmp(display, "0") == 0) |
| 223 | if (ioctl(xf86Info.consoleFd, VT_SET_CONSUSER) != 0) |
| 224 | xf86Msg(X_WARNING, |
| 225 | "xf86OpenConsole: VT_SET_CONSUSER failed\n"); |
| 226 | #endif |
| 227 | |
| 228 | if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) < 0) |
| 229 | FatalError("xf86OpenConsole: VT_GETMODE failed\n"); |
| 230 | |
| 231 | OsSignal(SIGUSR1, xf86VTAcquire); |
| 232 | OsSignal(SIGUSR2, xf86VTRelease); |
| 233 | |
| 234 | VT.mode = VT_PROCESS; |
| 235 | VT.acqsig = SIGUSR1; |
| 236 | VT.relsig = SIGUSR2; |
| 237 | |
| 238 | if (ioctl(xf86Info.consoleFd, VT_SETMODE, &VT) < 0) |
| 239 | FatalError("xf86OpenConsole: VT_SETMODE VT_PROCESS failed\n"); |
| 240 | |
| 241 | if (ioctl(xf86Info.consoleFd, VT_SETDISPINFO, atoi(display)) < 0) |
| 242 | xf86Msg(X_WARNING, "xf86OpenConsole: VT_SETDISPINFO failed\n"); |
| 243 | } |
| 244 | #endif |
| 245 | |
| 246 | #ifdef KDSETMODE |
| 247 | SYSCALL(i = ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS)); |
| 248 | if (i < 0) { |
| 249 | xf86Msg(X_WARNING, |
| 250 | "xf86OpenConsole: KDSETMODE KD_GRAPHICS failed on %s (%s)\n", |
| 251 | consoleDev, strerror(errno)); |
| 252 | } |
| 253 | #endif |
| 254 | } |
| 255 | else { /* serverGeneration != 1 */ |
| 256 | |
| 257 | #ifdef HAS_USL_VTS |
| 258 | if (vtEnabled && !xf86Info.ShareVTs) { |
| 259 | /* |
| 260 | * Now re-get the VT |
| 261 | */ |
| 262 | if (xf86Info.autoVTSwitch) |
| 263 | switch_to(xf86Info.vtno, "xf86OpenConsole"); |
| 264 | |
| 265 | #ifdef VT_SET_CONSUSER /* added in snv_139 */ |
| 266 | if (strcmp(display, "0") == 0) |
| 267 | if (ioctl(xf86Info.consoleFd, VT_SET_CONSUSER) != 0) |
| 268 | xf86Msg(X_WARNING, |
| 269 | "xf86OpenConsole: VT_SET_CONSUSER failed\n"); |
| 270 | #endif |
| 271 | |
| 272 | /* |
| 273 | * If the server doesn't have the VT when the reset occurs, |
| 274 | * this is to make sure we don't continue until the activate |
| 275 | * signal is received. |
| 276 | */ |
| 277 | if (!xf86VTOwner()) |
| 278 | sleep(5); |
| 279 | } |
| 280 | #endif /* HAS_USL_VTS */ |
| 281 | |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void |
| 286 | xf86CloseConsole(void) |
| 287 | { |
| 288 | #ifdef HAS_USL_VTS |
| 289 | struct vt_mode VT; |
| 290 | #endif |
| 291 | |
| 292 | #if !defined(__i386__) && !defined(__i386) && !defined(__x86) |
| 293 | |
| 294 | if (!xf86DoConfigure) { |
| 295 | int fd; |
| 296 | |
| 297 | /* |
| 298 | * Wipe out framebuffer just like the non-SI Xsun server does. This |
| 299 | * could be improved by saving framebuffer contents in |
| 300 | * xf86OpenConsole() above and restoring them here. Also, it's unclear |
| 301 | * at this point whether this should be done for all framebuffers in |
| 302 | * the system, rather than only the console. |
| 303 | */ |
| 304 | if ((fd = open(xf86SolarisFbDev, O_RDWR, 0)) < 0) { |
| 305 | xf86Msg(X_WARNING, |
| 306 | "xf86CloseConsole(): unable to open framebuffer (%s)\n", |
| 307 | strerror(errno)); |
| 308 | } |
| 309 | else { |
| 310 | struct fbgattr fbattr; |
| 311 | |
| 312 | if ((ioctl(fd, FBIOGATTR, &fbattr) < 0) && |
| 313 | (ioctl(fd, FBIOGTYPE, &fbattr.fbtype) < 0)) { |
| 314 | xf86Msg(X_WARNING, |
| 315 | "xf86CloseConsole(): unable to retrieve framebuffer" |
| 316 | " attributes (%s)\n", strerror(errno)); |
| 317 | } |
| 318 | else { |
| 319 | pointer fbdata; |
| 320 | |
| 321 | fbdata = mmap(NULL, fbattr.fbtype.fb_size, |
| 322 | PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 323 | if (fbdata == MAP_FAILED) { |
| 324 | xf86Msg(X_WARNING, |
| 325 | "xf86CloseConsole(): unable to mmap framebuffer" |
| 326 | " (%s)\n", strerror(errno)); |
| 327 | } |
| 328 | else { |
| 329 | memset(fbdata, 0, fbattr.fbtype.fb_size); |
| 330 | munmap(fbdata, fbattr.fbtype.fb_size); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | close(fd); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | #endif |
| 339 | |
| 340 | #ifdef KDSETMODE |
| 341 | /* Reset the display back to text mode */ |
| 342 | SYSCALL(ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT)); |
| 343 | #endif |
| 344 | |
| 345 | #ifdef HAS_USL_VTS |
| 346 | if (vtEnabled) { |
| 347 | if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) != -1) { |
| 348 | VT.mode = VT_AUTO; /* Set default vt handling */ |
| 349 | ioctl(xf86Info.consoleFd, VT_SETMODE, &VT); |
| 350 | } |
| 351 | |
| 352 | /* Activate the VT that X was started on */ |
| 353 | if (xf86Info.autoVTSwitch) |
| 354 | switch_to(xf86StartVT, "xf86CloseConsole"); |
| 355 | } |
| 356 | #endif /* HAS_USL_VTS */ |
| 357 | |
| 358 | close(xf86Info.consoleFd); |
| 359 | } |
| 360 | |
| 361 | int |
| 362 | xf86ProcessArgument(int argc, char **argv, int i) |
| 363 | { |
| 364 | /* |
| 365 | * Keep server from detaching from controlling tty. This is useful when |
| 366 | * debugging, so the server can receive keyboard signals. |
| 367 | */ |
| 368 | if (!strcmp(argv[i], "-keeptty")) { |
| 369 | KeepTty = TRUE; |
| 370 | return 1; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Undocumented flag to protect page 0 from read/write to help catch NULL |
| 375 | * pointer dereferences. This is purely a debugging flag. |
| 376 | */ |
| 377 | if (!strcmp(argv[i], "-protect0")) { |
| 378 | Protect0 = TRUE; |
| 379 | return 1; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Use /dev/console as the console device. |
| 384 | */ |
| 385 | if (!strcmp(argv[i], "-C")) { |
| 386 | UseConsole = TRUE; |
| 387 | return 1; |
| 388 | } |
| 389 | |
| 390 | #ifdef HAS_USL_VTS |
| 391 | |
| 392 | if ((argv[i][0] == 'v') && (argv[i][1] == 't')) { |
| 393 | if (sscanf(argv[i], "vt%d", &VTnum) == 0) { |
| 394 | UseMsg(); |
| 395 | VTnum = -1; |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | return 1; |
| 400 | } |
| 401 | |
| 402 | #endif /* HAS_USL_VTS */ |
| 403 | |
| 404 | if ((i + 1) < argc) { |
| 405 | if (!strcmp(argv[i], "-dev")) { |
| 406 | strlcpy(xf86SolarisFbDev, argv[i + 1], sizeof(xf86SolarisFbDev)); |
| 407 | return 2; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | void |
| 415 | xf86UseMsg(void) |
| 416 | { |
| 417 | #ifdef HAS_USL_VTS |
| 418 | ErrorF("vtX Use the specified VT number\n"); |
| 419 | #endif |
| 420 | ErrorF("-dev <fb> Framebuffer device\n"); |
| 421 | ErrorF("-keeptty Don't detach controlling tty\n"); |
| 422 | ErrorF(" (for debugging only)\n"); |
| 423 | ErrorF("-C Use /dev/console as the console device\n"); |
| 424 | } |