summaryrefslogtreecommitdiffstats
path: root/soundserver/artswrapper.c
blob: 53cc56a457f278e4cb4ba757f06d62458769bb32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>

/*
 * adjust_priority
 *
 * sets realtime priority
 */

#include "config.h"

#ifdef HAVE_REALTIME_SCHED
#include <sched.h>

void adjust_priority()
{
	int sched = sched_getscheduler(0);
	if(sched == SCHED_FIFO || sched == SCHED_RR)
	{
		printf(">> since the scheduling policy is not standard, I assume\n");
		printf("   it has been adjusted to fit the needs of realtime audio\n");
	}
	else
	{
		struct sched_param sp;
		long priority = (sched_get_priority_max(SCHED_FIFO) +
			             sched_get_priority_min(SCHED_FIFO))/2;
		
		sp.sched_priority = priority;

		if(sched_setscheduler(0, SCHED_FIFO, &sp) != -1)
		{
			printf(">> running as realtime process now (priority %ld)\n",
																	priority);
			putenv("STARTED_THROUGH_ARTSWRAPPER=1");
		}
		else
		{
			/* can't set realtime priority */
			putenv("STARTED_THROUGH_ARTSWRAPPER=2");
		}
	}
}
#else
void adjust_priority()
{
	int prio;

	prio = getpriority(PRIO_PROCESS,getpid());
	if(prio > -10)
	{
		setpriority(PRIO_PROCESS,getpid(),-17);
		prio = getpriority(PRIO_PROCESS,getpid());
	}

	/* no system support for realtime priority */
	putenv("STARTED_THROUGH_ARTSWRAPPER=3");

	if(prio > -10) {
		printf(">> synthesizer priority is %d (which is unacceptable,",prio);
		printf(" try running as root)\n");
	}
	else {
		printf(">> synthesizer priority is %d (which is the best\n",prio);
		printf("   we can get out of a non realtime system)\n");
	}
}
#endif

int main(int argc, char **argv)
{
	if(argc == 2)
	{
		if(strcmp(argv[1],"check") == 0)
		{
			/* backward compatibility with old artswrapper */
			printf("okay\n");
			return 0;
		}
	}

	adjust_priority();

	/* drop root privileges if running setuid root
	   (due to realtime priority stuff) */
	if (geteuid() != getuid()) 
	{
#if defined (HAVE_SETEUID) && !defined (HAVE_SETEUID_FAKE) 
		seteuid(getuid());
#else
		setreuid(-1, getuid());
#endif
		if (geteuid() != getuid()) {
			perror("setuid()");
			return 2;
		}
	}

	if(argc == 0)
		return 1;

/*
 * Real-time status is passed to artsd via the environment variable
 * STARTED_THROUGH_ARTSWRAPPER. It has one of the following values:
 *
 * unset - not running as real-time
 * 1     - running as real-time
 * 2     - no privileges to set real-time scheduling
 * 3     - no support for real-time scheduling
 */
	argv[0] = EXECUTE;
	execv(EXECUTE,argv);
	perror(EXECUTE);
	return 1;
}