[vlc-devel] [PATCH 1/3] libvlccore: add a couple of functions to power off the system

Rémi Denis-Courmont remi at remlab.net
Wed Apr 17 17:09:34 CEST 2013


Le mercredi 17 avril 2013 15:34:05, Ludovic Fauvet a écrit :
> Only Windows is fully supported ATM since it requires a bit more work on
> other platforms.
> ---
>  include/vlc_os.h   |  29 +++++++++++++++
>  src/Makefile.am    |   8 +++++
>  src/libvlccore.sym |   2 ++
>  src/posix/os.c     |  40 +++++++++++++++++++++
>  src/win32/os.c     | 102
> +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 181
> insertions(+)
>  create mode 100644 include/vlc_os.h
>  create mode 100644 src/posix/os.c
>  create mode 100644 src/win32/os.c

Uninformative name. Mind you the whole src/win32/ is about the OS (and so is 
src/posix/).

> 
> diff --git a/include/vlc_os.h b/include/vlc_os.h
> new file mode 100644
> index 0000000..7081ec5
> --- /dev/null
> +++ b/include/vlc_os.h
> @@ -0,0 +1,29 @@
> +/*************************************************************************
> **** + * vlc_os.h: Operating System helpers
> +
> **************************************************************************
> *** + * Copyright (C) 2013 VLC authors and VideoLAN
> + *
> + * Authors: Ludovic Fauvet <etix at videolan.org>
> + *
> + * This program 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 program 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 program; if not, write to the Free Software
> Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA
> 02110-1301, USA. +
> **************************************************************************
> ***/ +
> +#ifndef VLC_OS_H
> +#define VLC_OS_H 1
> +
> +VLC_API bool vlc_can_power_off( void ) VLC_USED;
> +VLC_API bool vlc_power_off( void );

This does not respect the object-action function naming convention.</fenrir>

> +
> +#endif
> diff --git a/src/Makefile.am b/src/Makefile.am
> index af4c10b..c5a0a9f 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -68,6 +68,7 @@ pluginsinclude_HEADERS = \
>  	../include/vlc_mtime.h \
>  	../include/vlc_network.h \
>  	../include/vlc_objects.h \
> +	../include/vlc_os.h \
>  	../include/vlc_picture.h \
>  	../include/vlc_picture_fifo.h \
>  	../include/vlc_picture_pool.h \
> @@ -253,6 +254,7 @@ SOURCES_libvlc_darwin = \
>  	darwin/specific.c \
>  	posix/rand.c \
>  	darwin/netconf.c \
> +	posix/os.c \
>  	$(NULL)
> 
>  SOURCES_libvlc_android = \
> @@ -266,6 +268,7 @@ SOURCES_libvlc_android = \
>  	posix/linux_specific.c \
>  	posix/specific.c \
>  	posix/rand.c \
> +	posix/os.c \
>  	$(NULL)
> 
>  SOURCES_libvlc_linux = \
> @@ -279,6 +282,7 @@ SOURCES_libvlc_linux = \
>  	posix/linux_specific.c \
>  	posix/specific.c \
>  	posix/rand.c \
> +	posix/os.c \
>  	$(NULL)
> 
>  SOURCES_libvlc_win32 = \
> @@ -290,6 +294,7 @@ SOURCES_libvlc_win32 = \
>  	win32/specific.c \
>  	win32/winsock.c \
>  	win32/rand.c \
> +	win32/os.c \
>  	$(NULL)
> 
>  SOURCES_libvlc_symbian = \
> @@ -297,6 +302,7 @@ SOURCES_libvlc_symbian = \
>  	symbian/dirs.c \
>  	win32/plugin.c \
>  	posix/rand.c \
> +	posix/os.c \
>  	$(NULL)
> 
>  SOURCES_libvlc_os2 = \
> @@ -307,6 +313,7 @@ SOURCES_libvlc_os2 = \
>  	os2/thread.c \
>  	os2/specific.c \
>  	os2/rand.c \
> +	posix/os.c \
>  	$(NULL)
> 
>  SOURCES_libvlc_other = \
> @@ -318,6 +325,7 @@ SOURCES_libvlc_other = \
>  	posix/plugin.c \
>  	posix/specific.c \
>  	posix/rand.c \
> +	posix/os.c \
>  	$(NULL)
> 
>  SOURCES_libvlc_common = \
> diff --git a/src/libvlccore.sym b/src/libvlccore.sym
> index ddae89b..77f9569 100644
> --- a/src/libvlccore.sym
> +++ b/src/libvlccore.sym
> @@ -468,6 +468,7 @@ vlc_b64_decode_binary
>  vlc_b64_decode_binary_to_buffer
>  vlc_b64_encode
>  vlc_b64_encode_binary
> +vlc_can_power_off
>  vlc_cancel
>  vlc_clone
>  VLC_CompileBy
> @@ -541,6 +542,7 @@ vlc_object_hold
>  vlc_object_release
>  vlc_object_get_name
>  vlc_object_alive
> +vlc_power_off
>  vlc_rand_bytes
>  vlc_drand48
>  vlc_lrand48
> diff --git a/src/posix/os.c b/src/posix/os.c
> new file mode 100644
> index 0000000..ae7ccd2
> --- /dev/null
> +++ b/src/posix/os.c
> @@ -0,0 +1,40 @@
> +/*************************************************************************
> **** + * os.c: Operating System helpers
> +
> **************************************************************************
> *** + * Copyright (C) 2013 VLC authors and VideoLAN
> + *
> + * Authors: Ludovic Fauvet <etix at videolan.org>
> + *
> + * This program 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 program 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 program; if not, write to the Free Software
> Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA
> 02110-1301, USA. +
> **************************************************************************
> ***/ +
> +#ifdef HAVE_CONFIG_H
> +# include "config.h"
> +#endif
> +
> +#include <vlc_common.h>
> +#include <vlc_os.h>
> +
> +bool vlc_can_power_off( void )

This is under-designed. First, getting privileges for power management may 
require blocking and/or user interaction. Second, it might return some kind of 
cookie that needs to be reused for the actual actions later (especially if 
user interaction was involved).

> +{
> +    // STUB
> +    return false;
> +}
> +
> +bool vlc_power_off( void )
> +{
> +    // STUB
> +    return false;
> +}
> diff --git a/src/win32/os.c b/src/win32/os.c
> new file mode 100644
> index 0000000..e938d95
> --- /dev/null
> +++ b/src/win32/os.c
> @@ -0,0 +1,102 @@
> +/*************************************************************************
> **** + * os.c: Operating System helpers
> +
> **************************************************************************
> *** + * Copyright (C) 2013 VLC authors and VideoLAN
> + *
> + * Authors: Ludovic Fauvet <etix at videolan.org>
> + *
> + * This program 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 program 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 program; if not, write to the Free Software
> Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA
> 02110-1301, USA. +
> **************************************************************************
> ***/ +
> +#ifdef HAVE_CONFIG_H
> +# include "config.h"
> +#endif
> +
> +#include <vlc_common.h>
> +#include <vlc_os.h>
> +#include <windows.h>
> +#include <reason.h>
> +
> +static void adjust_poweroff_privileges( HANDLE *hToken, bool enable )
> +{
> +    TOKEN_PRIVILEGES tkp;
> +
> +    LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME,
> +            &tkp.Privileges[0].Luid );
> +
> +    tkp.PrivilegeCount = 1;
> +    tkp.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED : 0;
> +
> +    AdjustTokenPrivileges( *hToken, FALSE, &tkp, 0,
> +            (PTOKEN_PRIVILEGES)NULL, 0 );
> +}
> +
> +static HANDLE acquire_poweroff_privileges( void )
> +{
> +    HANDLE hToken;
> +
> +    if( !OpenProcessToken( GetCurrentProcess(),
> +                TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
> +        return NULL;
> +
> +    adjust_poweroff_privileges( &hToken, true );
> +
> +    if( GetLastError() != ERROR_SUCCESS )
> +    {
> +        CloseHandle( hToken );
> +        return NULL;
> +    }
> +
> +    return hToken;
> +}
> +
> +bool vlc_can_power_off( void )
> +{
> +    HANDLE hToken;
> +
> +    // Try to acquire the shutdown privileges
> +    if( ( hToken = acquire_poweroff_privileges() ) != NULL )
> +    {
> +        // Remove the aquired privilege since we do not
> +        // need it immediately.
> +        adjust_poweroff_privileges( &hToken, false );
> +        CloseHandle( hToken );
> +        return true;
> +    }
> +
> +    return false;
> +}
> +
> +bool vlc_power_off( void )
> +{
> +    HANDLE hToken;
> +
> +    // Acquire the necessary privileges to power off
> +    if( ( hToken = acquire_poweroff_privileges() ) == NULL )
> +        return false;
> +
> +    CloseHandle( hToken );
> +
> +    //TODO Support suspend / hibernation
> +
> +    if( !ExitWindowsEx( EWX_SHUTDOWN | EWX_FORCEIFHUNG,
> +                SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
> +                SHTDN_REASON_MINOR_UPGRADE |
> +                SHTDN_REASON_FLAG_PLANNED ) )
> +        return false;
> +
> +    // Shutdown is successful
> +    return true;
> +}

-- 
Rémi Denis-Courmont
http://www.remlab.net/



More information about the vlc-devel mailing list