3 * Copyright 2010 Maarten Lankhorst for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/port.h"
22 #include "wine/library.h"
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_SCHED_H
27 #include <sys/sched.h>
29 #include <sys/resource.h>
31 #if defined(HAVE_SETRLIMIT) && defined(__linux__) && defined(SONAME_LIBDBUS_1) && defined(HAVE_SCHED_H)
36 #include <dbus/dbus.h>
41 #define RLIMIT_RTTIME 15
44 #define FUNCPTR(fn) static typeof(fn) *p ##fn
46 FUNCPTR(dbus_error_init
);
47 FUNCPTR(dbus_error_free
);
48 FUNCPTR(dbus_bus_get
);
49 FUNCPTR(dbus_message_new_method_call
);
50 FUNCPTR(dbus_message_append_args
);
51 FUNCPTR(dbus_connection_send_with_reply_and_block
);
52 FUNCPTR(dbus_message_unref
);
53 FUNCPTR(dbus_set_error_from_message
);
56 static int translate_error( unsigned tid
, const char *name
)
58 if (!strcmp( name
, DBUS_ERROR_NO_MEMORY
))
60 if (!strcmp( name
, DBUS_ERROR_SERVICE_UNKNOWN
) ||
61 !strcmp( name
, DBUS_ERROR_NAME_HAS_NO_OWNER
))
63 if (!strcmp( name
, DBUS_ERROR_ACCESS_DENIED
) ||
64 !strcmp( name
, DBUS_ERROR_AUTH_FAILED
))
68 fprintf( stderr
, "%04x: Could not map error \"%s\"\n", tid
, name
);
72 static void init_dbus(void)
74 #define FUNCPTR(fn) p ##fn = wine_dlsym( libdbus, #fn, NULL, 0 );
76 void *libdbus
= wine_dlopen( SONAME_LIBDBUS_1
, RTLD_NOW
, error
, sizeof( error
) );
77 FUNCPTR(dbus_error_init
);
78 FUNCPTR(dbus_error_free
);
79 FUNCPTR(dbus_bus_get
);
80 FUNCPTR(dbus_message_new_method_call
);
81 FUNCPTR(dbus_message_append_args
);
82 FUNCPTR(dbus_connection_send_with_reply_and_block
);
83 FUNCPTR(dbus_message_unref
);
84 FUNCPTR(dbus_set_error_from_message
);
88 static DBusConnection
*get_dbus(void)
90 static DBusConnection
*bus
;
96 pdbus_error_init( &error
);
98 bus
= pdbus_bus_get( DBUS_BUS_SYSTEM
, &error
);
102 int rtkit_make_realtime( pid_t process
, pid_t thread
, int priority
)
105 DBusMessage
*m
= NULL
, *r
= NULL
;
106 dbus_uint64_t pid
= process
;
107 dbus_uint64_t tid
= thread
;
108 dbus_uint32_t rtprio
= priority
;
116 pdbus_error_init( &error
);
117 m
= pdbus_message_new_method_call( "org.freedesktop.RealtimeKit1",
118 "/org/freedesktop/RealtimeKit1",
119 "org.freedesktop.RealtimeKit1",
120 "MakeThreadRealtimeWithPID" );
127 ret
= pdbus_message_append_args( m
, DBUS_TYPE_UINT64
, &pid
,
128 DBUS_TYPE_UINT64
, &tid
,
129 DBUS_TYPE_UINT32
, &rtprio
,
136 r
= pdbus_connection_send_with_reply_and_block( bus
, m
, -1, &error
);
139 ret
= translate_error( tid
, error
.name
);
142 if (pdbus_set_error_from_message( &error
, r
))
143 ret
= translate_error( tid
, error
.name
);
148 pdbus_message_unref( m
);
150 pdbus_message_unref( r
);
151 pdbus_error_free( &error
);
153 fprintf( stderr
, "%04x: Setting realtime priority of %u returns %i\n", (int)tid
, rtprio
, ret
);
157 int rtkit_undo_realtime( pid_t thread
)
159 struct sched_param parm
;
161 memset( &parm
, 0, sizeof( parm
) );
162 ret
= sched_setscheduler( thread
, SCHED_OTHER
, &parm
);
170 int rtkit_make_realtime( pid_t process
, pid_t thread
, int priority
)
175 int rtkit_undo_realtime( pid_t thread
)