libxl: Add API to retrieve domain console tty
PATCH 1/1 libxl: Add API to retrieve domain console tty
This api retrieve domain console from xenstore. With this new api, it is easy to implement "virsh console" command in libvirt libxl driver.
tools/libxl/libxl.c +55 -0
@@ -1552,6 +1552,61 @@ out:return rc; } +int libxl_console_get_tty (libxl_ctx *ctx,uint32_t domid,int cons_num, +libxl_console_type type,char **path) +{ +GC_INIT (ctx); +char *dom_path =0 ; +char *tty_path =0 ; +char *tty =0 ; + + dom_path =libxl__xs_get_dompath (gc, domid); +switch (type) { +case LIBXL_CONSOLE_TYPE_SERIAL : + tty_path =libxl__sprintf (gc,"%s/serial/0/tty" , dom_path); +break ; +case LIBXL_CONSOLE_TYPE_PV : +if (cons_num ==0 ) + tty_path =libxl__sprintf (gc,"%s/console/tty" , dom_path); +else + tty_path =libxl__sprintf (gc,"%s/device/console/%d/tty" , dom_path, cons_num); +break ; +default : + goto out; + } + + tty =libxl__xs_read (gc,XBT_NULL , tty_path); + *path =strdup (tty); + +out: +GC_FREE ; +return ERROR_FAIL ; +} + +int libxl_primary_console_get_tty (libxl_ctx *ctx,uint32_t domid_vm,char **path) +{ +GC_INIT (ctx); +uint32_t stubdomid =libxl_get_stubdom_id (ctx, domid_vm); +int rc; +if (stubdomid) + rc =libxl_console_get_tty (ctx, stubdomid, +STUBDOM_CONSOLE_SERIAL ,LIBXL_CONSOLE_TYPE_PV , path); +else { +switch (libxl__domain_type (gc, domid_vm)) { +case LIBXL_DOMAIN_TYPE_HVM : + rc =libxl_console_get_tty (ctx, domid_vm,0 ,LIBXL_CONSOLE_TYPE_SERIAL , path); +break ; +case LIBXL_DOMAIN_TYPE_PV : + rc =libxl_console_get_tty (ctx, domid_vm,0 ,LIBXL_CONSOLE_TYPE_PV , path); +break ; +default : +abort (); + } + } +GC_FREE ; +return rc; +} + static int libxl__append_disk_list_of_type(libxl__gc *gc, uint32_t domid,
tools/libxl/libxl.h +9 -0
@@ -594,6 +594,15 @@ int libxl_console_exec(libxl_ctx *ctx, u* case of HVM guests, and before libxl_run_bootloader in case of PV * guests using pygrub. */ int libxl_primary_console_exec (libxl_ctx *ctx,uint32_t domid_vm); +/* libxl_console_get_tty retrieves the specified domain's console tty path +* and stores it in path. Caller is responsible for freeing the memory. +*/ +int libxl_console_get_tty (libxl_ctx *ctx,uint32_t domid,int cons_num, +libxl_console_type type,char **path); +/* libxl_primary_console_get_tty retrieves the specified domain's console tty +* path and stores it in path. Caller is responsible for freeing the memory. +*/ +int libxl_primary_console_get_tty (libxl_ctx *ctx,uint32_t domid_vm,char **path);/* May be called with info_r == NULL to check for domain's existance */ int libxl_domain_info(libxl_ctx*, libxl_dominfo *info_r,
Quoted 22 lines · Bamvor Jian Zhang
This api retrieve domain console from xenstore. With this new api, it is easy to implement "virsh console" command in libvirt libxl driver.
Signed-off-by Bamvor Jian Zhang <[email protected]>diff -r
f8279258e3c9-ra1dc373628e4tools/libxl/libxl.ctools/libxl/libxl.c +11
-0@@ -1552,6 +1552,61 @@ out:return rc; } +int libxl_console_get_tty (libxl_ctx *ctx,uint32_t domid,int cons_num, +libxl_console_type type,char **path) +{ +GC_INIT (ctx); +char *dom_path =0 ; +char *tty_path =0 ; +char *tty =0 ; + + dom_path =libxl__xs_get_dompath (gc, domid); +switch (type) { +case LIBXL_CONSOLE_TYPE_SERIAL :
tools/libxl/libxl.c out: · line 1575
Quoted 10 lines
tools/libxl/libxl.c +10
-0+ tty_path =libxl__sprintf (gc,"%s/serial/0/tty" , dom_path); +break ; +case LIBXL_CONSOLE_TYPE_PV : +if (cons_num ==0 ) + tty_path =libxl__sprintf (gc,"%s/console/tty" , dom_path); +else + tty_path =libxl__sprintf (gc,"%s/device/console/%d/tty" , dom_path, cons_num); +break ; +default : + goto out;
This should result in ERROR_INVAL.
tools/libxl/libxl.c out: · line 1583
Quoted 8 lines
tools/libxl/libxl.c +8
-0+ } + + tty =libxl__xs_read (gc,XBT_NULL , tty_path); + *path =strdup (tty); + +out: +GC_FREE ; +return ERROR_FAIL ;
There does not appear to be a success path out of this function... You probably want an rc variable set appropriately.
Quoted 33 lines
tools/libxl/libxl.c +26
-0+} + +int libxl_primary_console_get_tty (libxl_ctx *ctx,uint32_t domid_vm,char **path) +{ +GC_INIT (ctx); +uint32_t stubdomid =libxl_get_stubdom_id (ctx, domid_vm); +int rc; +if (stubdomid) + rc =libxl_console_get_tty (ctx, stubdomid, +STUBDOM_CONSOLE_SERIAL ,LIBXL_CONSOLE_TYPE_PV , path); +else { +switch (libxl__domain_type (gc, domid_vm)) { +case LIBXL_DOMAIN_TYPE_HVM : + rc =libxl_console_get_tty (ctx, domid_vm,0 ,LIBXL_CONSOLE_TYPE_SERIAL , path); +break ; +case LIBXL_DOMAIN_TYPE_PV : + rc =libxl_console_get_tty (ctx, domid_vm,0 ,LIBXL_CONSOLE_TYPE_PV , path); +break ; +default : +abort (); + } + } +GC_FREE ; +return rc; +} + static int libxl__append_disk_list_of_type(libxl__gc *gc, uint32_t domid,diff -r
f8279258e3c9-ra1dc373628e4tools/libxl/libxl.h
tools/libxl/libxl.h int libxl_console_exec(libxl_ctx *ctx, u · line 603
Quoted 10 lines
tools/libxl/libxl.h +7
-0@@ -594,6 +594,15 @@ int libxl_console_exec(libxl_ctx *ctx, u* case of HVM guests, and before libxl_run_bootloader in case of PV * guests using pygrub. */ int libxl_primary_console_exec (libxl_ctx *ctx,uint32_t domid_vm); +/* libxl_console_get_tty retrieves the specified domain's console tty path +* and stores it in path. Caller is responsible for freeing the memory. +*/ +int libxl_console_get_tty (libxl_ctx *ctx,uint32_t domid,int cons_num, +libxl_console_type type,char **path); +/* libxl_primary_console_get_tty retrieves the specified domain's console tty +* path and stores it in path.
"retrieves the specified domain's *primary* console tty path? Otherwise this description doesn't differ from the previous function's...
Quoted 6 lines
tools/libxl/libxl.h +2
-0Caller is responsible for freeing the memory. +*/ +int libxl_primary_console_get_tty (libxl_ctx *ctx,uint32_t domid_vm,char **path);/* May be called with info_r == NULL to check for domain's existance */ int libxl_domain_info(libxl_ctx*,
libxl_dominfo*info_r,
Bamvor Jian Zhang writes ("[PATCH] [PATCH] libxl: Add API to retrieve domain console tty"):
Quoted 1 line
This api retrieve domain console from xenstore. With this new api, it is easy to implement "virsh console" command in libvirt libxl driver.
Thanks. This is coming along in the right direction. I have just a few comments.
Quoted 12 lines
Signed-off-by Bamvor Jian Zhang <[email protected]>diff -r
f8279258e3c9-ra1dc373628e4tools/libxl/libxl.ctools/libxl/libxl.c +3
-0@@ -1552,6 +1552,61 @@ out:return rc; } +int libxl_console_get_tty (libxl_ctx *ctx,uint32_t domid,int cons_num, +libxl_console_type type,char **path) +{
...
Quoted 1 line
+
tty_path=libxl__sprintf(gc, "%s/console/tty", dom_path);
You could profitably make use of the GCSPRINTF helper macro here.
tools/libxl/libxl.c out: · line 1572
Quoted 2 lines
tools/libxl/libxl.c +2
-0+else + tty_path =libxl__sprintf (gc,"%s/device/console/%d/tty" , dom_path, cons_num);
This line needs to be reformatted to be less than 75-80 characters. There are a couple of other long lines in your patch too.
tools/libxl/libxl.c out: · line 1580
Quoted 4 lines
tools/libxl/libxl.c +4
-0+ + tty =libxl__xs_read (gc,XBT_NULL , tty_path); + *path =strdup (tty); +
You need to check for errors here. libxl__xs_read is not infallible.
tools/libxl/libxl.c out: · line 1597
Quoted 12 lines
tools/libxl/libxl.c +12
-0+int libxl_primary_console_get_tty (libxl_ctx *ctx,uint32_t domid_vm,char **path) +{ +GC_INIT (ctx); +uint32_t stubdomid =libxl_get_stubdom_id (ctx, domid_vm); +int rc; +if (stubdomid) + rc =libxl_console_get_tty (ctx, stubdomid, +STUBDOM_CONSOLE_SERIAL ,LIBXL_CONSOLE_TYPE_PV , path); +else { +switch (libxl__domain_type (gc, domid_vm)) { +case LIBXL_DOMAIN_TYPE_HVM : + rc =libxl_console_get_tty (ctx, domid_vm,0 ,LIBXL_CONSOLE_TYPE_SERIAL , path);
This recapitules the logic in libxl_primary_console_exec. I think you should refactor this so that the substance of it is in a single function eg
int libxl__primary_console_find(libxl__gc *gc, uint32_t domid_vm
int *num_out, libxl_console_type *type_out);
Also aborting if libxl__domain_type fails is not correct, but if you refactor this and use the existing code in the middle of libxl_primary_console_exec you'll handle that case correctly.
Thanks,
Ian.