VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/daemon/vboxadd_timesync.cpp@ 494

Last change on this file since 494 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1/** @file
2 *
3 * VirtualBox timesync daemon for Linux
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <getopt.h>
27#include <errno.h>
28#include <sys/fcntl.h>
29#include <sys/ioctl.h>
30#include <sys/time.h>
31#include <time.h>
32#include <VBox/VBoxGuest.h>
33#include <VBox/err.h>
34
35static void usage()
36{
37 printf("vboxadd-timesync [--interval <seconds>]\n"
38 " [--daemonize]"
39 "\n");
40}
41
42static void safe_sleep(time_t seconds)
43{
44 struct timespec requestedSleepTime;
45
46 requestedSleepTime.tv_sec = seconds;
47 requestedSleepTime.tv_nsec = 0;
48
49 for (;;)
50 {
51 int err;
52 struct timespec remainingSleepTime;
53
54 err = nanosleep(&requestedSleepTime, &remainingSleepTime);
55 if (err)
56 {
57 /* if the sleep was interrupted, remember remaining sleep time */
58 if (errno == EINTR)
59 {
60 requestedSleepTime = remainingSleepTime;
61 }
62 else
63 {
64 /* not good... */
65 }
66 }
67 else
68 {
69 /* nanosleep completed and took at least the requested time */
70 break;
71 }
72 }
73}
74
75
76int main(int argc, char *argv[])
77{
78 const struct option options[] =
79 {
80 { "interval", required_argument, NULL, 'i' },
81 { "daemonize", no_argument, NULL, 'd' },
82 { "help", no_argument, NULL, 'h' },
83 { NULL, 0, NULL, 0 }
84 };
85 int secInterval = 10;
86 int fDaemonize = 0;
87 int c;
88
89 /* command line parsing */
90 for (;;)
91 {
92 c = getopt_long(argc, argv, "", options, NULL);
93 if (c == -1)
94 break;
95 switch (c)
96 {
97 case 'i':
98 {
99 secInterval = atoi(optarg);
100 break;
101 }
102
103 case 'd':
104 {
105 fDaemonize = 1;
106 break;
107 }
108
109 case 'h':
110 {
111 usage();
112 return 0;
113 }
114
115 case ':':
116 case '?':
117 {
118 /* unrecognized option (?) or parameter missing (:) */
119 return 1;
120 }
121
122 default:
123 break;
124 }
125 }
126
127 /* open the driver */
128 int fd = open(VBOXGUEST_DEVICE_NAME, O_RDWR, 0);
129 if (fd < 0)
130 {
131 printf("Error opening kernel module! rc = %d\n", errno);
132 return 1;
133 }
134
135 /* prepare the request structure */
136 VMMDevReqHostTime req;
137 vmmdevInitRequest((VMMDevRequestHeader*)&req, VMMDevReq_GetHostTime);
138
139 if (!fDaemonize)
140 printf("VirtualBox timesync daemon.\n"
141 "(C) 2005 InnoTek Systemberatung GmbH\n"
142 "\nSync interval: %d seconds.\n", secInterval);
143
144 if (fDaemonize)
145 daemon(1, 0);
146
147 do
148 {
149 /* perform VMM request */
150 if (ioctl(fd, IOCTL_VBOXGUEST_VMMREQUEST, (void*)&req) >= 0)
151 {
152 if (VBOX_SUCCESS(req.header.rc))
153 {
154 /* set the system time */
155 struct timeval tv;
156 tv.tv_sec = req.time / (uint64_t)1000;
157 tv.tv_usec = (req.time % (uint64_t)1000) * 1000;
158 settimeofday(&tv, NULL);
159 }
160 else
161 {
162 printf("Error querying host time! header.rc = %d\n", req.header.rc);
163 }
164 }
165 else
166 {
167 printf("Error performing VMM request! errno = %d\n", errno);
168 }
169 /* wait for the next run */
170 safe_sleep(secInterval);
171
172 } while (1);
173
174 close(fd);
175
176 return 0;
177}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette