Hi!
First of all, thanks for a great program!

I made two patches for it:
1) make hearnet suid and drop privileges right after libpcap initialization.
I had to move libpcap init code above jack

So, you can use hearnet as regular user.

2) Mutex in jack_process is a very bad thing. Moreover, it seems
there's no need for it, as voice->active field serves as a mutex.
Attached patch removes pthread_mutex.
If you think voice->active assumption is a weak one, the problem can
be solved with a pair of jack_ringbuffers: one for free voices and one
for active.


Regards,

Dmitry.
diff -r e33c4561875a Makefile
--- a/Makefile	Thu Oct 05 21:08:43 2006 +0400
+++ b/Makefile	Thu Oct 05 21:16:24 2006 +0400
@@ -9,5 +9,9 @@ clean:
 clean: 
 	rm -f hearnet *.o core*
 
+suid: hearnet
+	chown root hearnet
+	chmod u+s hearnet
+
 dist: all
 	darcs dist -d hearnet-$(version)
diff -r e33c4561875a hearnet
Binary file hearnet has changed
diff -r e33c4561875a hearnet.cpp
--- a/hearnet.cpp	Thu Oct 05 21:08:43 2006 +0400
+++ b/hearnet.cpp	Thu Oct 05 21:14:00 2006 +0400
@@ -178,6 +178,21 @@ int main(int argc, char **argv)/*{{{*/
 
     if (argc > 1)
 	dev = argv[1];
+	
+    // libpcap stuff /*{{{*/
+    pcap_t *hdl_pcap;
+    char perrbuf[PCAP_ERRBUF_SIZE];
+    hdl_pcap = pcap_open_live(dev, BUFSIZ, 0, 0, perrbuf);
+    if (hdl_pcap == NULL)
+    {
+	fprintf(stderr,"pcap_open_live; %s\n", perrbuf);
+	usage();
+    }
+
+    seteuid(getuid());
+ 
+    /*}}}*/
+
     
     snprintf(client_name, sizeof(client_name), "hearnet %s", dev);
     // jack stuff {{{
@@ -219,18 +234,6 @@ int main(int argc, char **argv)/*{{{*/
 	init_voices();
 
 
-    // libpcap stuff /*{{{*/
-    pcap_t *hdl_pcap;
-    char perrbuf[PCAP_ERRBUF_SIZE];
-    hdl_pcap = pcap_open_live(dev, BUFSIZ, 0, 0, perrbuf);
-    if (hdl_pcap == NULL)
-    {
-	fprintf(stderr,"pcap_open_live; %s\n", perrbuf);
-	usage();
-    }
- 
-    /*}}}*/
-
 	timeval tv_start;
 	gettimeofday(&tv_start,0);
 	srand(tv_start.tv_sec);
diff -r 8dea68398052 hearnet.cpp
--- a/hearnet.cpp	Thu Oct 05 21:21:15 2006 +0400
+++ b/hearnet.cpp	Thu Oct 05 21:55:43 2006 +0400
@@ -75,8 +75,6 @@ int process (jack_nframes_t nframes, voi
 {
     jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
 
-    pthread_mutex_lock(&mutex);
-
     memset(out, 0, sizeof(jack_default_audio_sample_t)*nframes);
 	
 	for (int index = 0; index != MAX_VOICES; index++)
@@ -114,8 +112,6 @@ int process (jack_nframes_t nframes, voi
 		}
 	}
 
-    pthread_mutex_unlock(&mutex);
-
     return 0;
 }/*}}}*/
 /** jack callback to set sample rate */
@@ -138,8 +134,6 @@ void shutdown(void *arg)/*{{{*/
 /** packet handler called by pcap_dispatch in main() */
 void packet_handler(u_char * args, const struct pcap_pkthdr *pcap_hdr, const u_char * p)/*{{{*/
 {
-    pthread_mutex_lock(&mutex);
-	
 	voice* new_voice = get_free_voice();
 	if (new_voice)
 	{
@@ -155,8 +149,6 @@ void packet_handler(u_char * args, const
 		new_voice->attacklength = (rand()%20 + 1) / 1000.0f; // 10ms
 		new_voice->active = true;
 	}
-    pthread_mutex_unlock(&mutex);
-		
 }/*}}}*/
 
 void usage(void)/*{{{*/ 

Reply via email to