@@ -977,10 +977,53 @@ static void virCHMonitorStopEventLoop(virCHMonitorPtr mon)
977977 g_atomic_int_set (& mon -> event_loop_stop , 1 );
978978}
979979
980+ static int
981+ virCHMonitorPostInitialize (virCHMonitorPtr mon , virDomainObjPtr vm ,
982+ virCHDriverPtr driver )
983+ {
984+ g_autoptr (virCHDriverConfig ) cfg = virCHDriverGetConfig (driver );
985+ virCHDomainObjPrivatePtr priv = vm -> privateData ;
986+ int pings = 0 ;
987+ int rv ;
988+
989+ if ((rv = virPidFileReadPath (priv -> pidfile , & vm -> pid )) < 0 ) {
990+ virReportSystemError (- rv ,
991+ _ ("Domain %s didn't show up" ),
992+ vm -> def -> name );
993+ return -1 ;
994+ }
995+ VIR_DEBUG ("cloud-hypervisor vm=%p name=%s running with pid=%lld" ,
996+ vm , vm -> def -> name , (long long )vm -> pid );
997+
998+ mon -> pid = vm -> pid ;
999+
1000+ /* get a curl handle */
1001+ mon -> handle = curl_easy_init ();
1002+
1003+ /* Try pinging the VMM to make sure it is ready */
1004+ while (virCHMonitorPingVMM (mon )) {
1005+ if (++ pings < MONITOR_TMP_FAIL_RETRIES ) {
1006+ g_usleep (100 * 1000 );
1007+ continue ;
1008+ }
1009+ VIR_WARN ("Failed to ping VMM after %d retries" , pings );
1010+ return -1 ;
1011+ }
1012+
1013+ if (virDomainObjSave (vm , driver -> xmlopt , cfg -> stateDir ) < 0 )
1014+ return -1 ;
1015+
1016+ if (virCHMonitorStartEventLoop (mon ) < 0 )
1017+ return -1 ;
1018+
1019+ return 0 ;
1020+ }
1021+
9801022virCHMonitorPtr
9811023virCHMonitorOpen (virDomainObjPtr vm , virCHDriverPtr driver )
9821024{
9831025 g_autoptr (virCHDriverConfig ) cfg = virCHDriverGetConfig (driver );
1026+ virCHDomainObjPrivatePtr priv = vm -> privateData ;
9841027 virCHMonitorPtr mon = NULL ;
9851028
9861029 /* Hold an extra reference because we can't allow 'vm' to be
@@ -1000,7 +1043,14 @@ virCHMonitorOpen(virDomainObjPtr vm, virCHDriverPtr driver)
10001043 goto error ;
10011044
10021045 mon -> socketpath = g_strdup_printf ("%s/%s-socket" , cfg -> stateDir , vm -> def -> name );
1003- mon -> handle = curl_easy_init ();
1046+ /* XXX If we ever gonna change pid file pattern, come up with
1047+ * some intelligence here to deal with old paths. */
1048+ if (!(priv -> pidfile = virPidFileBuildPath (cfg -> stateDir , vm -> def -> name )))
1049+ goto error ;
1050+
1051+ if (virCHMonitorPostInitialize (mon , vm , driver ) < 0 )
1052+ goto error ;
1053+
10041054 virObjectRef (mon );
10051055 mon -> vm = virObjectRef (vm );
10061056
@@ -1018,8 +1068,6 @@ virCHMonitorNew(virDomainObjPtr vm, virCHDriverPtr driver)
10181068 virCHMonitorPtr mon = NULL ;
10191069 virCommandPtr cmd = NULL ;
10201070 int monitor_fds [2 ];
1021- int pings = 0 ;
1022- int rv ;
10231071 int i ;
10241072
10251073 if (virCHMonitorInitialize () < 0 )
@@ -1063,29 +1111,13 @@ virCHMonitorNew(virDomainObjPtr vm, virCHDriverPtr driver)
10631111 virCommandDaemonize (cmd );
10641112 virCommandRequireHandshake (cmd );
10651113
1066- rv = virCommandRun (cmd , NULL );
1067-
1068- /* wait for cloud-hypervisor process to show up */
1069- if (rv == 0 ) {
1070- if ((rv = virPidFileReadPath (priv -> pidfile , & vm -> pid )) < 0 ) {
1071- virReportSystemError (- rv ,
1072- _ ("Domain %s didn't show up" ),
1073- vm -> def -> name );
1074- goto cleanup ;
1075- }
1076- VIR_DEBUG ("cloud-hypervisor vm=%p name=%s running with pid=%lld" ,
1077- vm , vm -> def -> name , (long long )vm -> pid );
1078- } else {
1114+ if (virCommandRun (cmd , NULL ) != 0 ) {
10791115 VIR_DEBUG ("cloud-hypervisor vm=%p name=%s failed to spawn" ,
10801116 vm , vm -> def -> name );
10811117 goto cleanup ;
10821118 }
1083- mon -> pid = vm -> pid ;
1084-
1085- VIR_DEBUG ("Writing early domain status to disk" );
1086- if (virDomainObjSave (vm , driver -> xmlopt , cfg -> stateDir ) < 0 )
1087- goto cleanup ;
10881119
1120+ /* wait for cloud-hypervisor process to show up */
10891121 VIR_DEBUG ("Waiting for handshake from child" );
10901122 if (virCommandHandshakeWait (cmd ) < 0 ) {
10911123 /* Read errors from child that occurred between fork and exec. */
@@ -1097,25 +1129,8 @@ virCHMonitorNew(virDomainObjPtr vm, virCHDriverPtr driver)
10971129 if (virCommandHandshakeNotify (cmd ) < 0 )
10981130 goto cleanup ;
10991131
1100- /* get a curl handle */
1101- mon -> handle = curl_easy_init ();
1102-
1103- /* Try pinging the VMM to make sure it is ready */
1104- while (virCHMonitorPingVMM (mon )) {
1105- if (++ pings < MONITOR_TMP_FAIL_RETRIES ) {
1106- g_usleep (100 * 1000 );
1107- continue ;
1108- }
1109- VIR_WARN ("Failed to ping VMM after %d retries" , pings );
1110- goto cleanup ;
1111- }
1112-
1113- if (virDomainObjSave (vm , driver -> xmlopt , cfg -> stateDir ) < 0 )
1114- goto cleanup ;
1115-
1116- if (virCHMonitorStartEventLoop (mon ) < 0 ) {
1132+ if (virCHMonitorPostInitialize (mon , vm , driver ) < 0 )
11171133 goto cleanup ;
1118- }
11191134
11201135 /* now has its own reference */
11211136 virObjectRef (mon );
0 commit comments