VirtualBox

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

Last change on this file since 4997 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
File size: 4.2 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
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22#include <getopt.h>
23#include <errno.h>
24#include <sys/fcntl.h>
25#include <sys/ioctl.h>
26#include <sys/time.h>
27#include <time.h>
28#include <VBox/VBoxGuest.h>
29#include <VBox/err.h>
30
31static void usage(void)
32{
33 printf("vboxadd-timesync [--interval <seconds>]\n"
34 " [--daemonize]\n");
35}
36
37static void safe_sleep(time_t seconds)
38{
39 struct timespec requestedSleepTime;
40
41 requestedSleepTime.tv_sec = seconds;
42 requestedSleepTime.tv_nsec = 0;
43
44 for (;;)
45 {
46 int err;
47 struct timespec remainingSleepTime;
48
49 err = nanosleep(&requestedSleepTime, &remainingSleepTime);
50 if (err)
51 {
52 /* if the sleep was interrupted, remember remaining sleep time */
53 if (errno == EINTR)
54 {
55 requestedSleepTime = remainingSleepTime;
56 }
57 else
58 {
59 /* not good... */
60 }
61 }
62 else
63 {
64 /* nanosleep completed and took at least the requested time */
65 break;
66 }
67 }
68}
69
70
71int main(int argc, char *argv[])
72{
73 const struct option options[] =
74 {
75 { "interval", required_argument, NULL, 'i' },
76 { "daemonize", no_argument, NULL, 'd' },
77 { "help", no_argument, NULL, 'h' },
78 { NULL, 0, NULL, 0 }
79 };
80 int secInterval = 10;
81 int fDaemonize = 0;
82 int c, fd;
83 VMMDevReqHostTime req;
84
85 /* command line parsing */
86 for (;;)
87 {
88 c = getopt_long(argc, argv, "", options, NULL);
89 if (c == -1)
90 break;
91 switch (c)
92 {
93 case 'i':
94 {
95 secInterval = atoi(optarg);
96 break;
97 }
98
99 case 'd':
100 {
101 fDaemonize = 1;
102 break;
103 }
104
105 case 'h':
106 {
107 usage();
108 return 0;
109 }
110
111 case ':':
112 case '?':
113 {
114 /* unrecognized option (?) or parameter missing (:) */
115 return 1;
116 }
117
118 default:
119 break;
120 }
121 }
122
123 /* open the driver */
124 fd = open(VBOXGUEST_DEVICE_NAME, O_RDWR, 0);
125 if (fd < 0)
126 {
127 printf("Error opening kernel module! rc = %d\n", errno);
128 return 1;
129 }
130
131 /* prepare the request structure */
132 vmmdevInitRequest((VMMDevRequestHeader*)&req, VMMDevReq_GetHostTime);
133
134 if (!fDaemonize)
135 printf("VirtualBox timesync daemon.\n"
136 "(C) 2005-2007 innotek GmbH\n"
137 "\nSync interval: %d seconds.\n", secInterval);
138
139 if (fDaemonize)
140 daemon(1, 0);
141
142 do
143 {
144 /* perform VMM request */
145 if (ioctl(fd, IOCTL_VBOXGUEST_VMMREQUEST, (void*)&req) >= 0)
146 {
147 if (VBOX_SUCCESS(req.header.rc))
148 {
149 /** Set the system time.
150 * @@todo r=frank: This is too choppy. Adapt time smoother and try
151 * to prevent negative time differences. */
152 struct timeval tv;
153 tv.tv_sec = req.time / (uint64_t)1000;
154 tv.tv_usec = (req.time % (uint64_t)1000) * 1000;
155 settimeofday(&tv, NULL);
156 }
157 else
158 {
159 printf("Error querying host time! header.rc = %d\n", req.header.rc);
160 }
161 }
162 else
163 {
164 printf("Error performing VMM request! errno = %d\n", errno);
165 }
166 /* wait for the next run */
167 safe_sleep(secInterval);
168
169 } while (1);
170
171 close(fd);
172
173 return 0;
174}
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