forked from OpenXT/input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domains.c
1052 lines (859 loc) · 26.5 KB
/
domains.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014 Citrix Systems, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "project.h"
#define DOMAIN_ACTIVE_ADAPTER_NODE "display/activeAdapter"
extern struct timeval global_last_input_event;
static struct domain domains[NDOMAIN_MAX];
static const int one=1;
static const int zero=0;
static xc_interface *xc_handle = NULL;
static bool destroy_in_fork = false;
static struct domain *__domain_with(int offset, unsigned int size, const void *arg)
{
int i;
for (i = 0; i < NDOMAIN_MAX; i++)
if (domains[i].client != NULL &&
!memcmp((char*)&domains[i] + offset, arg, size))
return &domains[i];
return NULL;
}
#define domain_with(_field_, _arg_) \
__domain_with( \
offsetof(struct domain, _field_), \
sizeof ( ((struct domain *) NULL)->_field_ ), \
(_arg_) \
)
void iterate_domains(void (*callback)(struct domain *,void*), void* opaque)
{
int i;
for (i = 0; i < NDOMAIN_MAX; i++)
{
if (domains[i].client != NULL)
callback(&domains[i], opaque);
}
}
void check_diverts_for_the_dead(struct domain* d)
{
int i;
for (i = 0; i < NDOMAIN_MAX; ++i)
{
if (domains[i].divert_info != NULL)
divert_domain_gone(domains[i].divert_info, d);
}
}
int domains_count(void)
{
int i, n = 0;
for (i = 0; i < NDOMAIN_MAX; i++)
if (domains[i].client != NULL)
n++;
return n;
}
void
domain_set_slot(struct domain *d, int slot)
{
d->slot = slot;
xenstore_dom_write_int(d->domid, d->slot, "switcher/slot");
}
static struct domain *empty_domain()
{
int i;
for (i=0;i<NDOMAIN_MAX;++i)
if (domains[i].client==NULL) return &domains[i];
return NULL;
}
struct domain *domain_with_domid(int domid)
{
return domain_with(domid,&domid);
}
struct domain * domain_with_slot(int slot)
{
return domain_with(slot,&slot);
}
struct domain *domain_with_uuid(const char *uuid)
{
int i;
for (i=0; i<NDOMAIN_MAX; ++i) {
if (domains[i].client != NULL && domains[i].uuid && !strcmp(domains[i].uuid, uuid)) {
return &domains[i];
}
}
return NULL;
}
struct domain * domain_uivm(void)
{
return domain_with_slot(0);
}
struct domain * domain_pvm(void)
{
return domain_with(is_pvm,&one);
}
static void reset_prev_keyb_domain(struct domain *d)
{
int i = 0;
if ((d == NULL) || (d->domid == -1))
return;
for (i = 0; i < NDOMAIN_MAX; i++)
{
if ((domains[i].client != NULL) && (domains[i].prev_keyb_domid == d->domid))
{
info("reset previous keyboard domain for domain %d\n", domains[i].domid);
domains[i].prev_keyb_domain_ptr = NULL;
domains[i].prev_keyb_domid = -1;
}
}
}
int domain_cant_print_screen(struct domain *d)
{
char tmp[1024];
char path[1024];
int rc = 0;
if (d)
{
sprintf(path, "/vm/%s/policies/print-screen-disallowed", d->uuid);
rc = db_read(tmp, 1024, path) && strcmp(tmp, "true") == 0;
info("print-screen path:%s value:%s disallow:%d", path, tmp, rc);
}
return rc;
}
static char**
domain_get_whitelist_drivers()
{
static char *driver[64] = { 0 };
static int driver_read = 0;
char buf[256] = { 0 }, *beg, *end;
unsigned int dri = 0;
if (driver_read)
return driver;
if (!db_read(buf, sizeof (buf), "/display-driver-whitelist")) {
warning("error reading display driver whitelist!");
return NULL;
}
beg = buf;
end = strchr(beg, ',');
while (*beg && dri < sizeof (driver)-1) {
int sz =
(end != NULL)
? (end-beg)
: (int)strlen(beg);
if (sz > 0) {
driver[dri] = malloc(sz+1);
strncpy(driver[dri], beg, sz);
driver[dri][sz] = 0;
++dri;
}
if (!end)
break;
else {
beg = end + 1;
end = strchr(beg, ',');
}
}
driver[dri] = NULL;
driver_read = 1;
dri = 0;
while (driver[dri]) {
info("display driver whitelist: %d = %s", dri, driver[dri]);
++dri;
}
return driver;
}
static void
domain_active_adapter_node_watch(const char *path, void *opaque)
{
struct domain *d = opaque;
char *buff = NULL;
int i, disabled_surface = 0;
char **whitelist_drivers = NULL;
buff = xenstore_dom_read(d->domid, "switcher/have_gpu");
if (buff && strlen(buff) && strtol(buff, NULL, 0) == 1)
goto out;
whitelist_drivers = domain_get_whitelist_drivers();
buff = xenstore_read(path);
if (!buff || strlen(buff) == 0)
goto out;
for (i = 0; whitelist_drivers[i]; ++i)
{
if (strcasestr(buff, whitelist_drivers[i]))
break;
}
if (whitelist_drivers[i] == NULL)
{
/* Only disable the surface if it's not handled as a surfman VGPU
* Take care of calling surfman only one */
if (d->vgpu_enabled == -1)
com_citrix_xenclient_surfman_has_vgpu_(xcbus_conn, SURFMAN_SERVICE,
SURFMAN_PATH, d->domid,
&d->vgpu_enabled);
if (!d->vgpu_enabled)
disabled_surface = 1;
}
if (d->disabled_surface != disabled_surface)
{
d->disabled_surface = disabled_surface;
if (d->disabled_surface)
{
/* Write "" to xenstore so the blanker will pic the best
* resolution (nvidia pass through scenario). */
xenstore_dom_write(d->domid, "", "switcher/display_size");
}
switcher_switch_graphic(disabled_surface ? domain_uivm() : 0, 0);
}
out:
free(buff);
}
/*
* The blanker in the VM will write the number of adapters of the VM
* to the display/activeAdapter node.
* Then, we set a watch on each of this adapters
*/
static void
domain_active_adapter_watch(const char *path, void *opaque)
{
struct domain *d = opaque;
char *buff = NULL, *endptr, node_name[32];
long int val = 0;
int i;
buff = xenstore_dom_read(d->domid, DOMAIN_ACTIVE_ADAPTER_NODE);
if (!buff || strlen(buff) == 0)
{
info("No active adapter node for domain %d", d->domid);
return;
}
val = strtol(buff, &endptr, 10);
// no active adapters for this domain yet
if (endptr == buff)
return;
// graphics driver unloading for a SVM with a secondary GPU
if (0 == val && d->has_secondary_gpu)
switcher_switch_graphic(d, 0);
info("domain %d has %d active adapter(s)", d->domid, val);
d->num_active_adapters = val;
for (i = 0; i < val; ++i)
{
sprintf(node_name, "%s/%d", DOMAIN_ACTIVE_ADAPTER_NODE, i);
info ("watching node %s\n", node_name);
if (!xenstore_dom_watch(d->domid, domain_active_adapter_node_watch, d, node_name))
warning("failed to install xenstore watch! %s", node_name);
}
}
static void domain_surface_disabled_detect_init(struct domain *d)
{
char *buff = NULL;
char perm_buff[16];
d->vgpu_enabled = -1;
// create node only if it does not exist yet
buff = xenstore_dom_read(d->domid, DOMAIN_ACTIVE_ADAPTER_NODE);
if (!buff)
{
sprintf(perm_buff, "n0 b%d", d->domid);
// apparently the lib requires the permissions to be separated by 0
perm_buff[2] = 0;
xenstore_dom_write(d->domid, "", DOMAIN_ACTIVE_ADAPTER_NODE);
xenstore_dom_chmod(d->domid, perm_buff, 2, DOMAIN_ACTIVE_ADAPTER_NODE);
}
else
free(buff);
if (!xenstore_dom_watch(d->domid, domain_active_adapter_watch, d, DOMAIN_ACTIVE_ADAPTER_NODE))
warning("failed to install xenstore watch! %s", DOMAIN_ACTIVE_ADAPTER_NODE);
}
static void release_xs_watches(struct domain *d)
{
int i;
xenstore_dom_watch(d->domid, NULL, NULL, DOMAIN_ACTIVE_ADAPTER_NODE);
/* arbitrary assumption of maximum of 8 active adapters per domain */
for (i = 0; i < d->num_active_adapters; ++i) {
char node[256];
snprintf(node, sizeof(node), "%s/%d", DOMAIN_ACTIVE_ADAPTER_NODE, i);
xenstore_dom_watch(d->domid, NULL, NULL, node);
}
xenstore_dom_watch(d->domid, NULL, NULL, "power-state");
xenstore_dom_watch(d->domid, NULL, NULL, "switcher/command");
xenstore_dom_watch(d->domid, NULL, NULL, "attr/desktopDimensions");
}
/**
* FIXME: It's really ugly but I have no solution ...
* if dmbus_disconnect_client is called to release fd
**/
void domain_gone(struct domain *d)
{
xc_dominfo_t info;
int ret;
if (!d)
return;
if (!destroy_in_fork)
info("cleaning up domain %d",d->domid);
else
info ("cleaning up in fork domain %d", d->domid);
if (d->vkbd_backend) {
xen_vkbd_backend_release(d);
}
release_xs_watches(d);
if (d->is_pvm && d->sstate != 0)
focus_domain_gone(d);
if (!destroy_in_fork)
reset_prev_keyb_domain(d);
free(d->uuid);
destroy_divert_info(&d->divert_info);
d->client = NULL;
d->slot = -1;
d->is_pvm = 0;
d->prev_keyb_domain_ptr = NULL;
d->prev_keyb_domid = -1;
if (!destroy_in_fork)
{
input_domain_gone(d);
switcher_domain_gone(d);
}
check_diverts_for_the_dead(d);
}
#if 0
static void switcher_led_timer(void *opaque)
{
static struct timer_t *t = NULL;
struct timeval tv;
static int onoff = 0;
opaque = opaque;
if (onoff == 1 && (!mouse_grab_domain || !keyboard_grab_domain ||
mouse_grab_domain == keyboard_grab_domain))
{
if (t)
{
free_timer(t);
t = NULL;
}
return;
}
input_leds(onoff);
onoff = !onoff;
if (t == NULL)
t = set_new_timer(switcher_led_timer, NULL);
gettimeofday(&tv, NULL);
tv.tv_usec += 500 * 1000;
if (tv.tv_usec > 1000000) {
tv.tv_usec -= 1000000;
tv.tv_sec++;
}
set_timer(t, &tv);
}
#endif
static void domain_power_state(const char *path, void *opaque)
{
struct domain *d = opaque;
char *tmp = 0;
int state = 0;
if (!d)
{
error("No domain in switcher_power_state!\n");
return;
}
tmp = xenstore_read(path);
if (!tmp || strlen(tmp) == 0) {
free(tmp);
return;
}
state = strtol(tmp, NULL, 10);
free(tmp);
info("domain id=%d power-state now %d",d->domid,state);
if (state == 3)
{
info("domain id=%d entered s3",d->domid);
d->is_in_s3 = 1;
gettimeofday(&d->time_of_s3, NULL);
/* If a domain goes to sleep, then reset its previous keyboard domain
so that on wakeup, the domain itself will have the keyboard. */
info("reset previous keyboard domain for domain %d\n", d->domid);
d->prev_keyb_domain_ptr = NULL;
d->prev_keyb_domid = -1;
/* If a domain goes to sleep, then reset the previous keyboard domain
for all domains that have given the keyboard to this domain. */
reset_prev_keyb_domain(d);
switcher_s3(d);
}
}
int get_idle_time()
{
struct timeval now;
int i, latest_input_activity=0, sleeping_vm_count=0, guest_vm_count=0, uivm_domid = -1;
struct domain *uivm = domain_uivm();
if (uivm != NULL)
uivm_domid = uivm->domid;
/* Calculate latest_input_activity considering all domains except uivm */
for (i = 0; i < NDOMAIN_MAX; i++)
{
if ((domains[i].client != NULL) && (domains[i].domid != uivm_domid))
{
guest_vm_count++;
if(domains[i].is_in_s3)
{
domains[i].last_input_event = domains[i].time_of_s3;
sleeping_vm_count++;
}
latest_input_activity = MAX(latest_input_activity,domains[i].last_input_event.tv_sec);
}
}
/* Check if all guest vms are asleep or only the uivm is running. */
if (sleeping_vm_count == guest_vm_count)
{
/* Dont crash if there is no uivm running. */
if (uivm != NULL)
latest_input_activity = MAX(latest_input_activity,uivm->last_input_event.tv_sec);
gettimeofday(&now,NULL);
return (now.tv_sec - latest_input_activity);
}
else
return 0;
}
int get_last_input_time()
{
struct timeval now;
gettimeofday(&now,NULL);
return (now.tv_sec - global_last_input_event.tv_sec);
}
#if 0
static void domain_slot_watch(const char *path, void *opaque)
{
struct domain *d = opaque;
struct domain *d_slot;
int slot;
char *tmp;
path = path;
if (!(tmp = xenstore_dom_read(d->domid, "switcher/slot")))
return;
slot = strtol(tmp, NULL, 10);
free(tmp);
if ((d_slot = domain_with(slot, &slot)))
{
error("There is already a domain on this slot, %d\n", d_slot->domid);
return;
}
d->slot = slot;
info("Domain %d is on slot %d\n", d->domid, slot);
}
#endif
static void domain_command(const char *path, void *opaque)
{
struct domain *d = opaque;
char *tmp;
int slot;
int domid;
tmp = xenstore_read(path);
if (!tmp)
return;
if (!*tmp) {
free(tmp);
return;
}
info("Command \"%s\", domid %d\n", tmp, d->domid);
if (strcmp(tmp, "switch") == 0)
{
switcher_switch(d, 0, 0);
}
else if (sscanf(tmp, "switch slot %d", &slot) == 1)
{
struct domain *s = domain_with_slot(slot);
if (!s)
{
error("slot %d doesn't exist", slot);
return;
}
info("try to switch to slot %d", slot);
switcher_switch(s, 0, 0);
}
else if (sscanf(tmp, "switch domid %d", &domid) == 1)
{
struct domain *s = domain_with(domid, &domid);
if (!s)
{
error("domain %d doesn't exist", domid);
return;
}
switcher_switch(s, 0, 0);
}
else if (strcmp("keyboard take", tmp) == 0)
{
input_give_keyboard(d);
}
else if (sscanf(tmp, "keyboard take %d", &domid) == 1)
{
struct domain *src_domain = domain_with(domid, &domid);
if (!src_domain)
{
error("domain %d doesn't exist", domid);
return;
}
input_give_keyboard_from_domain(src_domain, d);
}
else if (strcmp("keyboard release", tmp) == 0)
{
input_return_keyboard(d);
}
else if (sscanf(tmp, "keyboard release %d", &domid) == 1)
{
struct domain *dest_domain = domain_with(domid, &domid);
if (!dest_domain)
{
error("domain %d doesn't exist", domid);
return;
}
input_return_keyboard_to_domain(dest_domain, d);
}
xenstore_write("", path);
free(tmp);
}
void
domain_read_uuid(struct domain *d)
{
char *tmp;
if (!(tmp = xenstore_dom_read(d->domid, "vm")))
return;
d->uuid = xenstore_read("%s/uuid", tmp);
free(tmp);
}
static void
domain_read_is_pv_domain(struct domain *d)
{
xc_dominfo_t info;
int ret;
d->is_pv_domain = 0;
ret = xc_domain_getinfo(xc_handle, d->domid, 1, &info);
if (ret != 1)
return;
if (info.domid != (uint32_t) d->domid)
return;
d->is_pv_domain = !info.hvm;
}
static void
domain_read_has_secondary_gpu(struct domain *d)
{
char gpu_db_str[128];
char gpu;
int rc = 0;
d->has_secondary_gpu = 0;
if (d->is_pvm)
return;
sprintf(gpu_db_str, "/vm/%s/gpu", d->uuid);
rc = db_read(&gpu, 1, gpu_db_str);
if (rc != TRUE)
return;
d->has_secondary_gpu = (gpu != 0);
}
static int
domain_read_slot(struct domain *d)
{
char slot_str[10];
char path[128];
int rc = 0;
sprintf(path, "/vm/%s/slot", d->uuid);
rc = db_read(slot_str, 1, path);
if (rc != TRUE)
return -1;
return strtol(slot_str, NULL, 10);
}
int add_domainstart_callback(void (*callback)(struct domain *))
{
struct callbacklist* newitem;
newitem = (struct callbacklist*) malloc(sizeof(struct callbacklist));
if (newitem)
{
newitem->next=domainstart_callback;
newitem->callback=callback;
domainstart_callback=newitem;
return 0;
}
return -1;
}
static void domain_calculate_abs_scaling(const char *path, void *opaque)
{
struct domain *d = opaque;
char *buff = NULL;
int xres = 0, yres = 0;
if (d == NULL || path == NULL)
return;
d->rel_x_mult = MAX_MOUSE_ABS_X / DEFAULT_RESOLUTION_X;
d->rel_y_mult = MAX_MOUSE_ABS_Y / DEFAULT_RESOLUTION_Y;
buff = xenstore_read(path);
if (!buff || strlen(buff) == 0)
{
info("No desktopDimensions node for domain %d", d->domid);
/* These might have been previously set, so need to clear the value. */
d->desktop_xres = d->desktop_yres = 0;
free(buff);
return;
}
if ((sscanf(buff, "%d %d", &xres, &yres) != 2) || (xres <= 0) || (yres <= 0))
{
info("Invalid desktopDimensions node for domain %d, value is %s", d->domid, buff);
/* These might have been previously set, so need to clear the value. */
d->desktop_xres = d->desktop_yres = 0;
free(buff);
return;
}
info("Found valid desktopDimensions node for domain %d, xres is %d, yres is %d", d->domid, xres, yres);
free(buff);
d->rel_x_mult = (double) MAX_MOUSE_ABS_X / (double) xres;
d->rel_y_mult = (double) MAX_MOUSE_ABS_Y / (double) yres;
/* If the desktop dimensions have changed, need to adjust the mouse position. */
input_domain_handle_resolution_change(d, xres, yres);
d->desktop_xres = xres;
d->desktop_yres = yres;
}
static void switcher_domid(struct domain *d, uint32_t domid)
{
char perm[8];
char path[64];
char perms[128];
char *tmp = 0;
int stubdom_domid;
int slot;
struct domain *d_pvm;
if (domain_with(domid,&domid))
{
error("domain %d already exists", domid);
return;
}
d->domid = domid;
domain_read_uuid(d);
domain_read_is_pv_domain(d);
slot = domain_read_slot(d);
if (domain_with(slot,&slot) || (slot == -1))
{
error("slot %d already taken (wanted by domain %d)",slot,domid);
return;
}
d->slot = slot;
info("New domain %d (slot %d)", domid, slot);
/* init xenstore nodes and permissions for midori secure window and status report */
if (slot == 0)
{
xenstore_dom_write(domid, "http://1.0.0.0/auth.html", "login/url");
xenstore_dom_write(domid, "3", "login/state");
sprintf(perm, "n%d", domid);
sprintf(path, "/local/domain/%d/report/state", domid);
xenstore_write_int(3, path);
xenstore_chmod(perm, 1, path);
sprintf(path, "/local/domain/%d/report/url", domid);
xenstore_write("http://1.0.0.0/create_report.html", path);
xenstore_chmod (perm, 1, path);
}
xenstore_dom_write(domid, "", "switcher/command");
sprintf(perms, "r%d", domid);
xenstore_dom_chmod(domid, perms, 1, "switcher/command");
if (!xenstore_dom_watch(domid, domain_command, d, "switcher/command"))
warning("failed to install xenstore watch! switcher/command");
d->rel_x_mult = MAX_MOUSE_ABS_X / DEFAULT_RESOLUTION_X;
d->rel_y_mult = MAX_MOUSE_ABS_Y / DEFAULT_RESOLUTION_Y;
d->desktop_xres = d->desktop_yres = 0;
if (!xenstore_dom_watch(d->domid, domain_calculate_abs_scaling, d, "attr/desktopDimensions"))
warning("failed to install xenstore watch! attr/desktopDimensions");
d->is_pvm = 0;
d->keyboard_led_code = 0;
xenstore_dom_write_int(d->domid, 0, "switcher/have_gpu");
if (!xenstore_dom_watch(domid, domain_power_state, d, "power-state"))
warning("failed to install xenstore watch! power-state");
domain_power_state("power-state",d);
d_pvm = domain_with(is_pvm, &one);
domain_surface_disabled_detect_init(d);
xen_vkbd_backend_create(d);
domain_read_has_secondary_gpu(d);
domain_mouse_switch_config(d);
focus_update_domain(d);
/* call our callbacks*/
struct callbacklist* c = domainstart_callback;
while (c)
{
c->callback(d);
c = c->next;
}
/* we are ready to receive switch commands for this domain */
xenstore_dom_write(domid, "true", "switcher/ready");
}
void handle_switcher_abs(void *priv, struct msg_switcher_abs *msg, size_t msglen)
{
struct domain *d = priv;
d->abs_enabled = msg->enabled;
info("PV mouse driver reports abs support is %s for domid:%d", (d->abs_enabled)?"on":"off",d->domid);
}
void switcher_pvm_domid(struct domain *d, uint32_t domid)
{
switcher_domid(d, domid);
info("Domain %d is a pvm", d->domid);
d->is_pvm = 1;
xenstore_dom_write_int(d->domid, 1, "switcher/have_gpu");
focus_update_domain(d);
info("pvm connected, switching to pvm");
switcher_switch(d, 0, 0);
}
void handle_switcher_leds(void *priv, struct msg_switcher_leds *msg, size_t msglen)
{
struct domain *d = priv;
d->keyboard_led_code = msg->led_code;
input_led_code(msg->led_code, d->domid);
}
void handle_switcher_shutdown(void *priv, struct msg_switcher_shutdown *msg, size_t msglen)
{
struct domain *d = priv;
int state = msg->reason;
info("Domain %d, slot %d, is_pvm %d, reports shutdown code %d",
d->domid,d->slot,d->is_pvm,state);
d->sstate = state;
switch (state) {
case SWITCHER_SHUTDOWN_REBOOT:
/*The pvm is rebooting, we need to block
execution of the init_raster code for a bit*/
focus_expect_death(d);
break;
case SWITCHER_SHUTDOWN_S3:
case SWITCHER_SHUTDOWN_S4:
case SWITCHER_SHUTDOWN_S5:
/*The pvm is dieing, we need */
/*to activate the raster code */
/*in the switch*/
focus_dont_expect_death(d);
break;
}
warning("not reached");
}
void domain_wake_from_s3(struct domain *d)
{
unsigned long s_state = 0;
int handle;
if (!d)
return;
if (!d->is_in_s3) return;
if (host_pmop_in_progress()) {
info("NOT resuming domain %d from S3 - host power operation in progress");
return;
}
info("Resuming domain %d from S3", d->domid);
if (xc_handle != NULL)
{
xc_get_hvm_param(xc_handle, d->domid, HVM_PARAM_ACPI_S_STATE, &s_state);
if (s_state == 3)
xc_set_hvm_param(xc_handle, d->domid, HVM_PARAM_ACPI_S_STATE, 0);
d->is_in_s3 = 0;
d->sstate = 5;
} else {
error("Failed to open xen control interface");
}
// Waking up a PVM from S3 will trigger the PVM guest driver to re-initialize
// the graphic device. Therefore, we might as well switch directly to it since
// it is displayable until we find a way to recover the device once put in S3.
if (d->is_pvm) {
switcher_switch(d, 0, 0);
}
}
/*
** Gather the info from the database into the domain struct
** do that every 1 seconds so we can change setting on runtime.
**
** /switcher/<slot>/left|right <slot>|prev
*/
void domain_mouse_switch_config(void *opaque)
{
struct domain *d = opaque;
char *obj_path = NULL;
GError *error = NULL;
GValue value1 = G_VALUE_INIT;
GValue value2 = G_VALUE_INIT;
char *str = NULL;
int values[2] = {-1, -1};
if (!com_citrix_xenclient_xenmgr_find_vm_by_uuid_ ( xcbus_conn, "com.citrix.xenclient.xenmgr", "/", d->uuid, &obj_path )) {
return;
}
/* #!$?! we really should autogen these */
DBusGProxy *vm_proxy = xcdbus_get_proxy(xcbus_conn, "com.citrix.xenclient.xenmgr", obj_path, "org.freedesktop.DBus.Properties");
if (!dbus_g_proxy_call(
vm_proxy, "Get", &error,
G_TYPE_STRING, "com.citrix.xenclient.xenmgr.vm", G_TYPE_STRING, "seamless-mouse-left", DBUS_TYPE_INVALID,
G_TYPE_VALUE, &value1, DBUS_TYPE_INVALID ))
{
return;
}
if (G_VALUE_HOLDS(&value1, G_TYPE_INT)) {
// slot
values[0] = g_value_get_int(&value1);
}
if (!dbus_g_proxy_call(
vm_proxy, "Get", &error,
G_TYPE_STRING, "com.citrix.xenclient.xenmgr.vm", G_TYPE_STRING, "seamless-mouse-right", DBUS_TYPE_INVALID,
G_TYPE_VALUE, &value2, DBUS_TYPE_INVALID ))
{
return;
}
if (G_VALUE_HOLDS(&value2, G_TYPE_INT)) {
values[1] = g_value_get_int(&value2);
}
info ("configuring seamless mouse for uuid=%s left=%d right=%d", d->uuid, values[0], values[1]);
d->mouse_switch.left = values[0];
d->mouse_switch.right = values[1];
}
struct domain *domain_create(dmbus_client_t client, int domid, DeviceType type)
{
struct domain *d=empty_domain();
if (!d) {
error("too many domains");
return d;
}
memset( d, 0, sizeof(struct domain) );
d->domid=-1;
d->slot=-1;
d->is_in_s3=0;
d->prev_keyb_domain_ptr = NULL;