VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/daemon/vboxadd_timesync.c@ 3539

Last change on this file since 3539 was 2981, checked in by vboxsync, 17 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1/** @file
2 *
3 * VirtualBox timesync daemon for Linux
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek 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(void)
36{
37 printf("vboxadd-timesync [--interval <seconds>]\n"
38 " [--daemonize]\n");
39}
40
41static void safe_sleep(time_t seconds)
42{
43 struct timespec requestedSleepTime;
44
45 requestedSleepTime.tv_sec = seconds;
46 requestedSleepTime.tv_nsec = 0;
47
48 for (;;)
49 {
50 int err;
51 struct timespec remainingSleepTime;
52
53 err = nanosleep(&requestedSleepTime, &remainingSleepTime);
54 if (err)
55 {
56 /* if the sleep was interrupted, remember remaining sleep time */
57 if (errno == EINTR)
58 {
59 requestedSleepTime = remainingSleepTime;
60 }
61 else
62 {
63 /* not good... */
64 }
65 }
66 else
67 {
68 /* nanosleep completed and took at least the requested time */
69 break;
70 }
71 }
72}
73
74
75int main(int argc, char *argv[])
76{
77 const struct option options[] =
78 {
79 { "interval", required_argument, NULL, 'i' },
80 { "daemonize", no_argument, NULL, 'd' },
81 { "help", no_argument, NULL, 'h' },
82 { NULL, 0, NULL, 0 }
83 };
84 int secInterval = 10;
85 int fDaemonize = 0;
86 int c, fd;
87 VMMDevReqHostTime req;
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 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 vmmdevInitRequest((VMMDevRequestHeader*)&req, VMMDevReq_GetHostTime);
137
138 if (!fDaemonize)
139 printf("VirtualBox timesync daemon.\n"
140 "(C) 2005-2007 innotek GmbH\n"
141 "\nSync interval: %d seconds.\n", secInterval);
142
143 if (fDaemonize)
144 daemon(1, 0);
145
146 do
147 {
148 /* perform VMM request */
149 if (ioctl(fd, IOCTL_VBOXGUEST_VMMREQUEST, (void*)&req) >= 0)
150 {
151 if (VBOX_SUCCESS(req.header.rc))
152 {
153 /** Set the system time.
154 * @@todo r=frank: This is too choppy. Adapt time smoother and try
155 * to prevent negative time differences. */
156 struct timeval tv;
157 tv.tv_sec = req.time / (uint64_t)1000;
158 tv.tv_usec = (req.time % (uint64_t)1000) * 1000;
159 settimeofday(&tv, NULL);
160 }
161 else
162 {
163 printf("Error querying host time! header.rc = %d\n", req.header.rc);
164 }
165 }
166 else
167 {
168 printf("Error performing VMM request! errno = %d\n", errno);
169 }
170 /* wait for the next run */
171 safe_sleep(secInterval);
172
173 } while (1);
174
175 close(fd);
176
177 return 0;
178}
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