4040
4141static char * qemu_active_path ;
4242static char * qemu_next_active_path ;
43- static int qemu_shmid = -1 ;
4443static struct xs_handle * xs ;
4544
4645
47- /* Mark the shared-memory segment for destruction */
48- static void qemu_destroy_buffer (void )
49- {
50- if (qemu_shmid != -1 )
51- shmctl (qemu_shmid , IPC_RMID , NULL );
52- qemu_shmid = -1 ;
53- }
54-
5546/* Get qemu to change buffers. */
5647void qemu_flip_buffer (int domid , int next_active )
5748{
@@ -61,6 +52,33 @@ void qemu_flip_buffer(int domid, int next_active)
6152 struct timeval tv ;
6253 fd_set fdset ;
6354 int rc ;
55+ char * path , * p ;
56+
57+ if (xs == NULL ) {
58+ if ((xs = xs_daemon_open ()) == NULL )
59+ errx (1 , "Couldn't contact xenstore" );
60+ if (!(path = xs_get_domain_path (xs , domid )))
61+ errx (1 , "can't get domain path in store" );
62+ if (!(path = realloc (path , strlen (path )
63+ + strlen ("/logdirty/next-active" ) + 1 )))
64+ errx (1 , "no memory for constructing xenstore path" );
65+ strcat (path , "/logdirty/" );
66+ p = path + strlen (path );
67+
68+ /* Watch for qemu's indication of the active buffer, and request it
69+ * to start writing to buffer 0 */
70+ strcpy (p , "active" );
71+ if (!xs_watch (xs , path , "qemu-active-buffer" ))
72+ errx (1 , "can't set watch in store (%s)\n" , path );
73+ if (!(qemu_active_path = strdup (path )))
74+ errx (1 , "no memory for copying xenstore path" );
75+
76+ strcpy (p , "next-active" );
77+ if (!(qemu_next_active_path = strdup (path )))
78+ errx (1 , "no memory for copying xenstore path" );
79+
80+ free (path );
81+ }
6482
6583 /* Tell qemu that we want it to start writing log-dirty bits to the
6684 * other buffer */
@@ -94,78 +112,4 @@ void qemu_flip_buffer(int domid, int next_active)
94112 goto read_again ;
95113}
96114
97- void * init_qemu_maps (int domid , unsigned int bitmap_size )
98- {
99- key_t key ;
100- char key_ascii [17 ] = {0 ,};
101- void * seg ;
102- char * path , * p ;
103- struct shmid_ds ds_buf ;
104- struct group * gr ;
105-
106- /* Make a shared-memory segment */
107- do {
108- key = rand (); /* No security, just a sequence of numbers */
109- qemu_shmid = shmget (key , 2 * bitmap_size ,
110- IPC_CREAT |IPC_EXCL |S_IRUSR |S_IWUSR |S_IRGRP |S_IWGRP );
111- if (qemu_shmid == -1 && errno != EEXIST )
112- errx (1 , "can't get shmem to talk to qemu-dm" );
113- } while (qemu_shmid == -1 );
114-
115- /* Remember to tidy up after ourselves */
116- atexit (qemu_destroy_buffer );
117-
118- /* Change owner so that qemu can map it. */
119- gr = getgrnam ("qemu_base" );
120- if (!gr )
121- err (1 , "can't get qemu gid" );
122- if (shmctl (qemu_shmid , IPC_STAT , & ds_buf ) < 0 )
123- err (1 , "can't get status of shm area" );
124- ds_buf .shm_perm .gid = gr -> gr_gid + (unsigned short )domid ;
125- if (shmctl (qemu_shmid , IPC_SET , & ds_buf ) < 0 )
126- err (1 , "can't change gid of shm area" );
127-
128- /* Map it into our address space */
129- seg = shmat (qemu_shmid , NULL , 0 );
130- if (seg == (void * ) -1 )
131- errx (1 , "can't map shmem to talk to qemu-dm" );
132- memset (seg , 0 , 2 * bitmap_size );
133-
134- /* Write the size of it into the first 32 bits */
135- * (uint32_t * )seg = bitmap_size ;
136-
137- /* Tell qemu about it */
138- if ((xs = xs_daemon_open ()) == NULL )
139- errx (1 , "Couldn't contact xenstore" );
140- if (!(path = xs_get_domain_path (xs , domid )))
141- errx (1 , "can't get domain path in store" );
142- if (!(path = realloc (path , strlen (path )
143- + strlen ("/logdirty/next-active" ) + 1 )))
144- errx (1 , "no memory for constructing xenstore path" );
145- strcat (path , "/logdirty/" );
146- p = path + strlen (path );
147-
148- strcpy (p , "key" );
149- snprintf (key_ascii , 17 , "%16.16llx" , (unsigned long long ) key );
150- if (!xs_write (xs , XBT_NULL , path , key_ascii , 16 ))
151- errx (1 , "can't write key (%s) to store path (%s)\n" , key_ascii , path );
152-
153- /* Watch for qemu's indication of the active buffer, and request it
154- * to start writing to buffer 0 */
155- strcpy (p , "active" );
156- if (!xs_watch (xs , path , "qemu-active-buffer" ))
157- errx (1 , "can't set watch in store (%s)\n" , path );
158- if (!(qemu_active_path = strdup (path )))
159- errx (1 , "no memory for copying xenstore path" );
160-
161- strcpy (p , "next-active" );
162- if (!(qemu_next_active_path = strdup (path )))
163- errx (1 , "no memory for copying xenstore path" );
164-
165- qemu_flip_buffer (domid , 0 );
166-
167- free (path );
168- return seg ;
169- }
170-
171115/***********************************************************************/
0 commit comments