[1] | 1 | /** @file
|
---|
| 2 | *
|
---|
| 3 | * VirtualBox Timesync using temporary Backdoor
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[1] | 26 | */
|
---|
| 27 |
|
---|
| 28 | #include <unistd.h>
|
---|
| 29 | #include <asm/io.h>
|
---|
| 30 | #include <sys/time.h>
|
---|
| 31 | #include <time.h>
|
---|
| 32 |
|
---|
| 33 | void usage()
|
---|
| 34 | {
|
---|
| 35 | printf("TimesyncBackdoor [-interval <seconds>]"
|
---|
| 36 | " [-daemonize]"
|
---|
| 37 | "\n");
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | int main(int argc, char *argv[])
|
---|
| 41 | {
|
---|
| 42 | int secInterval = 10;
|
---|
| 43 | int fDaemonize = 0;
|
---|
| 44 | int i;
|
---|
| 45 |
|
---|
| 46 | for (i = 1; i < argc; i++)
|
---|
| 47 | {
|
---|
| 48 | if (strcmp(argv[i], "-interval") == 0)
|
---|
| 49 | {
|
---|
| 50 | if (argc <= i)
|
---|
| 51 | {
|
---|
| 52 | usage();
|
---|
| 53 | return 1;
|
---|
| 54 | }
|
---|
| 55 | secInterval = atoi(argv[i + 1]);
|
---|
| 56 | i++;
|
---|
| 57 | }
|
---|
| 58 | else if (strcmp(argv[i], "-daemonize") == 0)
|
---|
| 59 | {
|
---|
| 60 | fDaemonize = 1;
|
---|
| 61 | }
|
---|
| 62 | else
|
---|
| 63 | {
|
---|
| 64 | usage();
|
---|
| 65 | return 1;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /* get port IO permission */
|
---|
| 70 | if (iopl(3))
|
---|
| 71 | {
|
---|
| 72 | printf("Error: could not set IOPL to 3!\n");
|
---|
| 73 | return 1;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | printf("VirtualBox timesync tool. Sync interval: %d seconds.\n", secInterval);
|
---|
| 77 |
|
---|
| 78 | if (fDaemonize)
|
---|
| 79 | daemon(1, 0);
|
---|
| 80 |
|
---|
| 81 | do
|
---|
| 82 | {
|
---|
| 83 | unsigned long long time;
|
---|
| 84 | /* get the high 32bit, this _must_ be done first */
|
---|
| 85 | outl(0, 0x505);
|
---|
| 86 | time = (unsigned long long)inl(0x505) << 32;
|
---|
| 87 | /* get the low 32bit */
|
---|
| 88 | outl(1, 0x505);
|
---|
| 89 | time |= inl(0x505);
|
---|
| 90 |
|
---|
| 91 | /* set the system time */
|
---|
| 92 | struct timeval tv;
|
---|
| 93 | tv.tv_sec = time / (unsigned long long)1000;
|
---|
| 94 | tv.tv_usec = (time % (unsigned long long)1000) * 1000;
|
---|
| 95 | settimeofday(&tv, NULL);
|
---|
| 96 |
|
---|
[26505] | 97 | /* wait for the next run */
|
---|
[1] | 98 | sleep(secInterval);
|
---|
| 99 |
|
---|
| 100 | } while (1);
|
---|
| 101 |
|
---|
| 102 | return 0;
|
---|
| 103 | }
|
---|