Re: [Intel-gfx] [PATCH i-g-t 1/3] lib/{igt_sysfs, igt_aux}: Make available to other users kick_fbcon() (unbind_fbcon()), and added helpers to igt_aux.

2016-10-27 Thread Daniel Vetter
On Thu, Oct 27, 2016 at 12:02:30AM +0300, Marius Vlad wrote:
> On Mon, Oct 24, 2016 at 10:40:37AM +0200, Daniel Vetter wrote:
> > On Thu, Oct 20, 2016 at 10:36:47PM +0300, Marius Vlad wrote:
> > > Previously under unbind_fbcon(), disable/enable framebuffer console.
> > > 
> > > lib/igt_aux: Added helpers to help convert sh scripts to C version. 
> > > libkmod and
> > > procps interface.
> > > 
> > > Signed-off-by: Marius Vlad 
> > > ---
> > >  configure.ac|   2 +
> > >  lib/Makefile.am |   2 +
> > >  lib/igt_aux.c   | 278 
> > > 
> > >  lib/igt_aux.h   |   7 ++
> > >  lib/igt_gvt.c   |  43 +
> > >  lib/igt_sysfs.c |  54 +++
> > >  lib/igt_sysfs.h |   2 +
> > >  7 files changed, 347 insertions(+), 41 deletions(-)
> > 
> > If you go with extracting stuff into helpers I'd go with a new helper
> > library like igt_kmod.c. Or just keep it in-line with tests, extracting
> > code is imo overrated ;-)
> Have no issue putting kmod helpers into their own file. Can't really
> tell which is the best approach though: the only user is drv_module_reload
> yet the test already got ``fatten'' up with the gem subtests (see v3).

tbh helper libraries only used by one testcase aren't all that useful. If
it's only used by this testcase then I'd say don't bother with the
library: It's a lot more work, and with just 1 user you're pretty much
guaranteed to come up with a sub-par library interface. It takes a few
different users of the same feature to see what's really needed in a good
library.
-Daniel

> > 
> > Also pls make sure the docs are correct and look good, there's a bunch of
> > issues with them (like non-matching function names between comment and
> > actual code).
> Sorry, I totally missed your input, will double check on this.
> > -Daniel
> > 
> > > 
> > > diff --git a/configure.ac b/configure.ac
> > > index 735cfd5..2c6e49d 100644
> > > --- a/configure.ac
> > > +++ b/configure.ac
> > > @@ -121,6 +121,8 @@ AC_SUBST(ASSEMBLER_WARN_CFLAGS)
> > >  
> > >  PKG_CHECK_MODULES(DRM, [libdrm])
> > >  PKG_CHECK_MODULES(PCIACCESS, [pciaccess >= 0.10])
> > > +PKG_CHECK_MODULES(KMOD, [libkmod])
> > > +PKG_CHECK_MODULES(PROCPS, [libprocps])
> > >  
> > >  case "$target_cpu" in
> > >   x86*|i?86)
> > > diff --git a/lib/Makefile.am b/lib/Makefile.am
> > > index 4c0893d..e1737bd 100644
> > > --- a/lib/Makefile.am
> > > +++ b/lib/Makefile.am
> > > @@ -34,6 +34,8 @@ AM_CFLAGS += $(CAIRO_CFLAGS)
> > >  libintel_tools_la_LIBADD = \
> > >   $(DRM_LIBS) \
> > >   $(PCIACCESS_LIBS) \
> > > + $(PROCPS_LIBS) \
> > > + $(KMOD_LIBS) \
> > >   $(CAIRO_LIBS) \
> > >   $(LIBUDEV_LIBS) \
> > >   $(LIBUNWIND_LIBS) \
> > > diff --git a/lib/igt_aux.c b/lib/igt_aux.c
> > > index 421f6d4..d150818 100644
> > > --- a/lib/igt_aux.c
> > > +++ b/lib/igt_aux.c
> > > @@ -51,6 +51,9 @@
> > >  #include 
> > >  #include 
> > >  
> > > +#include 
> > > +#include 
> > > +
> > >  #include "drmtest.h"
> > >  #include "i915_drm.h"
> > >  #include "intel_chipset.h"
> > > @@ -1193,6 +1196,281 @@ void igt_set_module_param_int(const char *name, 
> > > int val)
> > >   igt_set_module_param(name, str);
> > >  }
> > >  
> > > +/**
> > > + * igt_pkill:
> > > + * @sig: Signal to send
> > > + * @name: Name of process in the form found in /proc/pid/comm (limited 
> > > to 15
> > > + * chars)
> > > + * @returns: 0 in case the process is not found running or the signal 
> > > has been
> > > + * sent successfully or -1 otherwise.
> > > + *
> > > + * This function sends the signal @sig for a process found in process 
> > > table
> > > + * with name @comm.
> > > + */
> > > +int
> > > +igt_pkill(int sig, const char *comm)
> > > +{
> > > + int err = 0, try = 5;
> > > + PROCTAB *proc;
> > > + proc_t *proc_info;
> > > +
> > > + proc = openproc(PROC_FILLCOM | PROC_FILLSTAT | PROC_FILLARG);
> > > + igt_assert(proc != NULL);
> > > +
> > > + while ((proc_info = readproc(proc, NULL))) {
> > > + if (proc_info &&
> > > + !strncasecmp(proc_info->cmd, comm, sizeof(proc_info->cmd))) 
> > > {
> > > + switch (sig) {
> > > + case SIGTERM:
> > > + case SIGKILL:
> > > + do {
> > > + kill(proc_info->tid, sig);
> > > + } while (kill(proc_info->tid, 0) < 0 && try--);
> > > +
> > > + if (kill(proc_info->tid, 0) < 0)
> > > + err = -1;
> > > + break;
> > > + default:
> > > + if (kill(proc_info->tid, sig) < 0)
> > > + err = -1;
> > > + break;
> > > + }
> > > +
> > > + freeproc(proc_info);
> > > + break;
> > > + }
> > > + freeproc(proc_info);
> > > + }
> > > +
> > > + closeproc(proc);
> > > + return err;
> > > +}
> > > +
> > > 

Re: [Intel-gfx] [PATCH i-g-t 1/3] lib/{igt_sysfs, igt_aux}: Make available to other users kick_fbcon() (unbind_fbcon()), and added helpers to igt_aux.

2016-10-26 Thread Marius Vlad
On Mon, Oct 24, 2016 at 10:40:37AM +0200, Daniel Vetter wrote:
> On Thu, Oct 20, 2016 at 10:36:47PM +0300, Marius Vlad wrote:
> > Previously under unbind_fbcon(), disable/enable framebuffer console.
> > 
> > lib/igt_aux: Added helpers to help convert sh scripts to C version. libkmod 
> > and
> > procps interface.
> > 
> > Signed-off-by: Marius Vlad 
> > ---
> >  configure.ac|   2 +
> >  lib/Makefile.am |   2 +
> >  lib/igt_aux.c   | 278 
> > 
> >  lib/igt_aux.h   |   7 ++
> >  lib/igt_gvt.c   |  43 +
> >  lib/igt_sysfs.c |  54 +++
> >  lib/igt_sysfs.h |   2 +
> >  7 files changed, 347 insertions(+), 41 deletions(-)
> 
> If you go with extracting stuff into helpers I'd go with a new helper
> library like igt_kmod.c. Or just keep it in-line with tests, extracting
> code is imo overrated ;-)
Have no issue putting kmod helpers into their own file. Can't really
tell which is the best approach though: the only user is drv_module_reload
yet the test already got ``fatten'' up with the gem subtests (see v3).
> 
> Also pls make sure the docs are correct and look good, there's a bunch of
> issues with them (like non-matching function names between comment and
> actual code).
Sorry, I totally missed your input, will double check on this.
> -Daniel
> 
> > 
> > diff --git a/configure.ac b/configure.ac
> > index 735cfd5..2c6e49d 100644
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -121,6 +121,8 @@ AC_SUBST(ASSEMBLER_WARN_CFLAGS)
> >  
> >  PKG_CHECK_MODULES(DRM, [libdrm])
> >  PKG_CHECK_MODULES(PCIACCESS, [pciaccess >= 0.10])
> > +PKG_CHECK_MODULES(KMOD, [libkmod])
> > +PKG_CHECK_MODULES(PROCPS, [libprocps])
> >  
> >  case "$target_cpu" in
> > x86*|i?86)
> > diff --git a/lib/Makefile.am b/lib/Makefile.am
> > index 4c0893d..e1737bd 100644
> > --- a/lib/Makefile.am
> > +++ b/lib/Makefile.am
> > @@ -34,6 +34,8 @@ AM_CFLAGS += $(CAIRO_CFLAGS)
> >  libintel_tools_la_LIBADD = \
> > $(DRM_LIBS) \
> > $(PCIACCESS_LIBS) \
> > +   $(PROCPS_LIBS) \
> > +   $(KMOD_LIBS) \
> > $(CAIRO_LIBS) \
> > $(LIBUDEV_LIBS) \
> > $(LIBUNWIND_LIBS) \
> > diff --git a/lib/igt_aux.c b/lib/igt_aux.c
> > index 421f6d4..d150818 100644
> > --- a/lib/igt_aux.c
> > +++ b/lib/igt_aux.c
> > @@ -51,6 +51,9 @@
> >  #include 
> >  #include 
> >  
> > +#include 
> > +#include 
> > +
> >  #include "drmtest.h"
> >  #include "i915_drm.h"
> >  #include "intel_chipset.h"
> > @@ -1193,6 +1196,281 @@ void igt_set_module_param_int(const char *name, int 
> > val)
> > igt_set_module_param(name, str);
> >  }
> >  
> > +/**
> > + * igt_pkill:
> > + * @sig: Signal to send
> > + * @name: Name of process in the form found in /proc/pid/comm (limited to 
> > 15
> > + * chars)
> > + * @returns: 0 in case the process is not found running or the signal has 
> > been
> > + * sent successfully or -1 otherwise.
> > + *
> > + * This function sends the signal @sig for a process found in process table
> > + * with name @comm.
> > + */
> > +int
> > +igt_pkill(int sig, const char *comm)
> > +{
> > +   int err = 0, try = 5;
> > +   PROCTAB *proc;
> > +   proc_t *proc_info;
> > +
> > +   proc = openproc(PROC_FILLCOM | PROC_FILLSTAT | PROC_FILLARG);
> > +   igt_assert(proc != NULL);
> > +
> > +   while ((proc_info = readproc(proc, NULL))) {
> > +   if (proc_info &&
> > +   !strncasecmp(proc_info->cmd, comm, sizeof(proc_info->cmd))) 
> > {
> > +   switch (sig) {
> > +   case SIGTERM:
> > +   case SIGKILL:
> > +   do {
> > +   kill(proc_info->tid, sig);
> > +   } while (kill(proc_info->tid, 0) < 0 && try--);
> > +
> > +   if (kill(proc_info->tid, 0) < 0)
> > +   err = -1;
> > +   break;
> > +   default:
> > +   if (kill(proc_info->tid, sig) < 0)
> > +   err = -1;
> > +   break;
> > +   }
> > +
> > +   freeproc(proc_info);
> > +   break;
> > +   }
> > +   freeproc(proc_info);
> > +   }
> > +
> > +   closeproc(proc);
> > +   return err;
> > +}
> > +
> > +/**
> > + * igt_kill:
> > + * @sig: Signal to send.
> > + * @pid: Process pid to send.
> > + * @returns: 0 in case of success or -1 otherwise.
> > + *
> > + * This function is identical to igt_pkill() only that it searches the 
> > process
> > + * table using @pid instead of comm name.
> > + *
> > + */
> > +int
> > +igt_kill(int sig, pid_t pid)
> > +{
> > +   int err = 0, try = 5;
> > +   PROCTAB *proc;
> > +   proc_t *proc_info;
> > +
> > +   proc = openproc(PROC_PID | PROC_FILLSTAT | PROC_FILLARG);
> > +   igt_assert(proc != NULL);
> > +
> > +   while ((proc_info = readproc(proc, NULL))) {
> > +   if (proc_info && 

Re: [Intel-gfx] [PATCH i-g-t 1/3] lib/{igt_sysfs, igt_aux}: Make available to other users kick_fbcon() (unbind_fbcon()), and added helpers to igt_aux.

2016-10-24 Thread Marius Vlad
On Thu, Oct 20, 2016 at 09:09:17PM +0100, Chris Wilson wrote:
> On Thu, Oct 20, 2016 at 10:36:47PM +0300, Marius Vlad wrote:
> > +int
> > +igt_pkill(int sig, const char *comm)
> > +{
> > +   int err = 0, try = 5;
> > +   PROCTAB *proc;
> > +   proc_t *proc_info;
> > +
> > +   proc = openproc(PROC_FILLCOM | PROC_FILLSTAT | PROC_FILLARG);
> > +   igt_assert(proc != NULL);
> > +
> > +   while ((proc_info = readproc(proc, NULL))) {
> > +   if (proc_info &&
> 
> proc_info cannot be NULL, you've already tested for that.
True. Removed.
> 
> > +   !strncasecmp(proc_info->cmd, comm, sizeof(proc_info->cmd))) 
> > {
> > +   switch (sig) {
> > +   case SIGTERM:
> > +   case SIGKILL:
> > +   do {
> > +   kill(proc_info->tid, sig);
> > +   } while (kill(proc_info->tid, 0) < 0 && try--);
> > +
> > +   if (kill(proc_info->tid, 0) < 0)
> > +   err = -1;
> 
> Not convinced this is good behaviour for an API, to repeatedly call
> kill(SIGTERM) until bored. If the function didn't take a int sig and was
> called igt_terminate_process(const char *name), then repeating a few
> SIGTERM; before sending SIGKILL makes sense. But as it it, named like
> kill() I expect this to send exactly one signal.

I got mixed feelings about this as well. I'll keep it simple.

> 
> > +/**
> > + * igt_kill:
> > + * @sig: Signal to send.
> > + * @pid: Process pid to send.
> > + * @returns: 0 in case of success or -1 otherwise.
> > + *
> > + * This function is identical to igt_pkill() only that it searches the 
> > process
> > + * table using @pid instead of comm name.
> 
> There's a function called kill() that does exactly that, you even use it
> here ;)
True.
> 
> > +int
> > +igt_rmmod(const char *mod_name, bool force)
> > +{
> > +   struct kmod_ctx *ctx;
> > +   struct kmod_module *kmod;
> > +   int err, flags = 0;
> > +
> > +   ctx = kmod_new(NULL, NULL);
> > +   igt_assert(ctx != NULL);
> > +
> > +   err = kmod_module_new_from_name(ctx, mod_name, );
> > +   if (err < 0) {
> > +   igt_info("Could not use module %s (%s)\n", mod_name,
> > +   strerror(-err));
> > +   err = -1;
> > +   goto out;
> > +   }
> > +
> > +   if (igt_module_in_use(kmod)) {
> > +   igt_info("Module %s is in use\n", mod_name);
> > +   err = -1;
> > +   goto out;
> > +   }
> 
> Pointless (this is redundant).
Indeed.
> 
> > +
> > +   if (force) {
> > +   flags |= KMOD_REMOVE_FORCE;
> 
> Will it not be wiser (future proof) just to pass flags from the caller?
I'll pass it directly.
> 
> > +   }
> > +
> > +   err = kmod_module_remove_module(kmod, flags);
> > +   if (err < 0) {
> > +   igt_info("Could not remove module %s (%s)\n", mod_name,
> > +   strerror(-err));
> > +   err = -1;
> > +   }
> > +
> > +out:
> > +   kmod_module_unref(kmod);
> > +   kmod_unref(ctx);
> > +
> > +   return err;
> > +}
> 
> -- 
> Chris Wilson, Intel Open Source Technology Centre


signature.asc
Description: PGP signature
___
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx


Re: [Intel-gfx] [PATCH i-g-t 1/3] lib/{igt_sysfs, igt_aux}: Make available to other users kick_fbcon() (unbind_fbcon()), and added helpers to igt_aux.

2016-10-24 Thread Daniel Vetter
On Thu, Oct 20, 2016 at 10:36:47PM +0300, Marius Vlad wrote:
> Previously under unbind_fbcon(), disable/enable framebuffer console.
> 
> lib/igt_aux: Added helpers to help convert sh scripts to C version. libkmod 
> and
> procps interface.
> 
> Signed-off-by: Marius Vlad 
> ---
>  configure.ac|   2 +
>  lib/Makefile.am |   2 +
>  lib/igt_aux.c   | 278 
> 
>  lib/igt_aux.h   |   7 ++
>  lib/igt_gvt.c   |  43 +
>  lib/igt_sysfs.c |  54 +++
>  lib/igt_sysfs.h |   2 +
>  7 files changed, 347 insertions(+), 41 deletions(-)

If you go with extracting stuff into helpers I'd go with a new helper
library like igt_kmod.c. Or just keep it in-line with tests, extracting
code is imo overrated ;-)

Also pls make sure the docs are correct and look good, there's a bunch of
issues with them (like non-matching function names between comment and
actual code).
-Daniel

> 
> diff --git a/configure.ac b/configure.ac
> index 735cfd5..2c6e49d 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -121,6 +121,8 @@ AC_SUBST(ASSEMBLER_WARN_CFLAGS)
>  
>  PKG_CHECK_MODULES(DRM, [libdrm])
>  PKG_CHECK_MODULES(PCIACCESS, [pciaccess >= 0.10])
> +PKG_CHECK_MODULES(KMOD, [libkmod])
> +PKG_CHECK_MODULES(PROCPS, [libprocps])
>  
>  case "$target_cpu" in
>   x86*|i?86)
> diff --git a/lib/Makefile.am b/lib/Makefile.am
> index 4c0893d..e1737bd 100644
> --- a/lib/Makefile.am
> +++ b/lib/Makefile.am
> @@ -34,6 +34,8 @@ AM_CFLAGS += $(CAIRO_CFLAGS)
>  libintel_tools_la_LIBADD = \
>   $(DRM_LIBS) \
>   $(PCIACCESS_LIBS) \
> + $(PROCPS_LIBS) \
> + $(KMOD_LIBS) \
>   $(CAIRO_LIBS) \
>   $(LIBUDEV_LIBS) \
>   $(LIBUNWIND_LIBS) \
> diff --git a/lib/igt_aux.c b/lib/igt_aux.c
> index 421f6d4..d150818 100644
> --- a/lib/igt_aux.c
> +++ b/lib/igt_aux.c
> @@ -51,6 +51,9 @@
>  #include 
>  #include 
>  
> +#include 
> +#include 
> +
>  #include "drmtest.h"
>  #include "i915_drm.h"
>  #include "intel_chipset.h"
> @@ -1193,6 +1196,281 @@ void igt_set_module_param_int(const char *name, int 
> val)
>   igt_set_module_param(name, str);
>  }
>  
> +/**
> + * igt_pkill:
> + * @sig: Signal to send
> + * @name: Name of process in the form found in /proc/pid/comm (limited to 15
> + * chars)
> + * @returns: 0 in case the process is not found running or the signal has 
> been
> + * sent successfully or -1 otherwise.
> + *
> + * This function sends the signal @sig for a process found in process table
> + * with name @comm.
> + */
> +int
> +igt_pkill(int sig, const char *comm)
> +{
> + int err = 0, try = 5;
> + PROCTAB *proc;
> + proc_t *proc_info;
> +
> + proc = openproc(PROC_FILLCOM | PROC_FILLSTAT | PROC_FILLARG);
> + igt_assert(proc != NULL);
> +
> + while ((proc_info = readproc(proc, NULL))) {
> + if (proc_info &&
> + !strncasecmp(proc_info->cmd, comm, sizeof(proc_info->cmd))) 
> {
> + switch (sig) {
> + case SIGTERM:
> + case SIGKILL:
> + do {
> + kill(proc_info->tid, sig);
> + } while (kill(proc_info->tid, 0) < 0 && try--);
> +
> + if (kill(proc_info->tid, 0) < 0)
> + err = -1;
> + break;
> + default:
> + if (kill(proc_info->tid, sig) < 0)
> + err = -1;
> + break;
> + }
> +
> + freeproc(proc_info);
> + break;
> + }
> + freeproc(proc_info);
> + }
> +
> + closeproc(proc);
> + return err;
> +}
> +
> +/**
> + * igt_kill:
> + * @sig: Signal to send.
> + * @pid: Process pid to send.
> + * @returns: 0 in case of success or -1 otherwise.
> + *
> + * This function is identical to igt_pkill() only that it searches the 
> process
> + * table using @pid instead of comm name.
> + *
> + */
> +int
> +igt_kill(int sig, pid_t pid)
> +{
> + int err = 0, try = 5;
> + PROCTAB *proc;
> + proc_t *proc_info;
> +
> + proc = openproc(PROC_PID | PROC_FILLSTAT | PROC_FILLARG);
> + igt_assert(proc != NULL);
> +
> + while ((proc_info = readproc(proc, NULL))) {
> + if (proc_info && proc_info->tid == pid) {
> + switch (sig) {
> + case SIGTERM:
> + case SIGKILL:
> + do {
> + kill(proc_info->tid, sig);
> + } while (kill(proc_info->tid, 0) < 0 && try--);
> +
> + if (kill(proc_info->tid, 0) < 0)
> + err = -1;
> + break;
> + default:
> + if 

Re: [Intel-gfx] [PATCH i-g-t 1/3] lib/{igt_sysfs, igt_aux}: Make available to other users kick_fbcon() (unbind_fbcon()), and added helpers to igt_aux.

2016-10-20 Thread Chris Wilson
On Thu, Oct 20, 2016 at 10:36:47PM +0300, Marius Vlad wrote:
> +int
> +igt_pkill(int sig, const char *comm)
> +{
> + int err = 0, try = 5;
> + PROCTAB *proc;
> + proc_t *proc_info;
> +
> + proc = openproc(PROC_FILLCOM | PROC_FILLSTAT | PROC_FILLARG);
> + igt_assert(proc != NULL);
> +
> + while ((proc_info = readproc(proc, NULL))) {
> + if (proc_info &&

proc_info cannot be NULL, you've already tested for that.

> + !strncasecmp(proc_info->cmd, comm, sizeof(proc_info->cmd))) 
> {
> + switch (sig) {
> + case SIGTERM:
> + case SIGKILL:
> + do {
> + kill(proc_info->tid, sig);
> + } while (kill(proc_info->tid, 0) < 0 && try--);
> +
> + if (kill(proc_info->tid, 0) < 0)
> + err = -1;

Not convinced this is good behaviour for an API, to repeatedly call
kill(SIGTERM) until bored. If the function didn't take a int sig and was
called igt_terminate_process(const char *name), then repeating a few
SIGTERM; before sending SIGKILL makes sense. But as it it, named like
kill() I expect this to send exactly one signal.

> +/**
> + * igt_kill:
> + * @sig: Signal to send.
> + * @pid: Process pid to send.
> + * @returns: 0 in case of success or -1 otherwise.
> + *
> + * This function is identical to igt_pkill() only that it searches the 
> process
> + * table using @pid instead of comm name.

There's a function called kill() that does exactly that, you even use it
here ;)

> +int
> +igt_rmmod(const char *mod_name, bool force)
> +{
> + struct kmod_ctx *ctx;
> + struct kmod_module *kmod;
> + int err, flags = 0;
> +
> + ctx = kmod_new(NULL, NULL);
> + igt_assert(ctx != NULL);
> +
> + err = kmod_module_new_from_name(ctx, mod_name, );
> + if (err < 0) {
> + igt_info("Could not use module %s (%s)\n", mod_name,
> + strerror(-err));
> + err = -1;
> + goto out;
> + }
> +
> + if (igt_module_in_use(kmod)) {
> + igt_info("Module %s is in use\n", mod_name);
> + err = -1;
> + goto out;
> + }

Pointless (this is redundant).

> +
> + if (force) {
> + flags |= KMOD_REMOVE_FORCE;

Will it not be wiser (future proof) just to pass flags from the caller?

> + }
> +
> + err = kmod_module_remove_module(kmod, flags);
> + if (err < 0) {
> + igt_info("Could not remove module %s (%s)\n", mod_name,
> + strerror(-err));
> + err = -1;
> + }
> +
> +out:
> + kmod_module_unref(kmod);
> + kmod_unref(ctx);
> +
> + return err;
> +}

-- 
Chris Wilson, Intel Open Source Technology Centre
___
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx


[Intel-gfx] [PATCH i-g-t 1/3] lib/{igt_sysfs, igt_aux}: Make available to other users kick_fbcon() (unbind_fbcon()), and added helpers to igt_aux.

2016-10-20 Thread Marius Vlad
Previously under unbind_fbcon(), disable/enable framebuffer console.

lib/igt_aux: Added helpers to help convert sh scripts to C version. libkmod and
procps interface.

Signed-off-by: Marius Vlad 
---
 configure.ac|   2 +
 lib/Makefile.am |   2 +
 lib/igt_aux.c   | 278 
 lib/igt_aux.h   |   7 ++
 lib/igt_gvt.c   |  43 +
 lib/igt_sysfs.c |  54 +++
 lib/igt_sysfs.h |   2 +
 7 files changed, 347 insertions(+), 41 deletions(-)

diff --git a/configure.ac b/configure.ac
index 735cfd5..2c6e49d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -121,6 +121,8 @@ AC_SUBST(ASSEMBLER_WARN_CFLAGS)
 
 PKG_CHECK_MODULES(DRM, [libdrm])
 PKG_CHECK_MODULES(PCIACCESS, [pciaccess >= 0.10])
+PKG_CHECK_MODULES(KMOD, [libkmod])
+PKG_CHECK_MODULES(PROCPS, [libprocps])
 
 case "$target_cpu" in
x86*|i?86)
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 4c0893d..e1737bd 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -34,6 +34,8 @@ AM_CFLAGS += $(CAIRO_CFLAGS)
 libintel_tools_la_LIBADD = \
$(DRM_LIBS) \
$(PCIACCESS_LIBS) \
+   $(PROCPS_LIBS) \
+   $(KMOD_LIBS) \
$(CAIRO_LIBS) \
$(LIBUDEV_LIBS) \
$(LIBUNWIND_LIBS) \
diff --git a/lib/igt_aux.c b/lib/igt_aux.c
index 421f6d4..d150818 100644
--- a/lib/igt_aux.c
+++ b/lib/igt_aux.c
@@ -51,6 +51,9 @@
 #include 
 #include 
 
+#include 
+#include 
+
 #include "drmtest.h"
 #include "i915_drm.h"
 #include "intel_chipset.h"
@@ -1193,6 +1196,281 @@ void igt_set_module_param_int(const char *name, int val)
igt_set_module_param(name, str);
 }
 
+/**
+ * igt_pkill:
+ * @sig: Signal to send
+ * @name: Name of process in the form found in /proc/pid/comm (limited to 15
+ * chars)
+ * @returns: 0 in case the process is not found running or the signal has been
+ * sent successfully or -1 otherwise.
+ *
+ * This function sends the signal @sig for a process found in process table
+ * with name @comm.
+ */
+int
+igt_pkill(int sig, const char *comm)
+{
+   int err = 0, try = 5;
+   PROCTAB *proc;
+   proc_t *proc_info;
+
+   proc = openproc(PROC_FILLCOM | PROC_FILLSTAT | PROC_FILLARG);
+   igt_assert(proc != NULL);
+
+   while ((proc_info = readproc(proc, NULL))) {
+   if (proc_info &&
+   !strncasecmp(proc_info->cmd, comm, sizeof(proc_info->cmd))) 
{
+   switch (sig) {
+   case SIGTERM:
+   case SIGKILL:
+   do {
+   kill(proc_info->tid, sig);
+   } while (kill(proc_info->tid, 0) < 0 && try--);
+
+   if (kill(proc_info->tid, 0) < 0)
+   err = -1;
+   break;
+   default:
+   if (kill(proc_info->tid, sig) < 0)
+   err = -1;
+   break;
+   }
+
+   freeproc(proc_info);
+   break;
+   }
+   freeproc(proc_info);
+   }
+
+   closeproc(proc);
+   return err;
+}
+
+/**
+ * igt_kill:
+ * @sig: Signal to send.
+ * @pid: Process pid to send.
+ * @returns: 0 in case of success or -1 otherwise.
+ *
+ * This function is identical to igt_pkill() only that it searches the process
+ * table using @pid instead of comm name.
+ *
+ */
+int
+igt_kill(int sig, pid_t pid)
+{
+   int err = 0, try = 5;
+   PROCTAB *proc;
+   proc_t *proc_info;
+
+   proc = openproc(PROC_PID | PROC_FILLSTAT | PROC_FILLARG);
+   igt_assert(proc != NULL);
+
+   while ((proc_info = readproc(proc, NULL))) {
+   if (proc_info && proc_info->tid == pid) {
+   switch (sig) {
+   case SIGTERM:
+   case SIGKILL:
+   do {
+   kill(proc_info->tid, sig);
+   } while (kill(proc_info->tid, 0) < 0 && try--);
+
+   if (kill(proc_info->tid, 0) < 0)
+   err = -1;
+   break;
+   default:
+   if (kill(proc_info->tid, sig) < 0)
+   err = -1;
+   break;
+   }
+   freeproc(proc_info);
+   break;
+   }
+   freeproc(proc_info);
+   }
+
+   closeproc(proc);
+   return err;
+}
+
+static bool
+igt_module_in_use(struct kmod_module *kmod)
+{
+   int err;
+
+   err = kmod_module_get_initstate(kmod);
+
+   /* if compiled builtin or does not exist */
+   if (err == KMOD_MODULE_BUILTIN || err < 0)
+   return