ladishd: fix hidden connection save. Fix for #151
[ladish.git] / alsapid / helper.c
blobcd3760a754f3a24ea07bcf6050ec7b820791ffb2
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains alsapid helper functionality
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #define _GNU_SOURCE
28 #include <string.h> /* GNU basename() */
30 #include <stdio.h>
31 #include <stdlib.h> /* atoll */
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <linux/limits.h>
36 #include "alsapid.h"
38 void alsapid_compose_src_link(int alsa_client_id, char * buffer)
40 sprintf(buffer, "/tmp/alsapid-%lld-%d", (long long)getuid(), alsa_client_id);
43 void alsapid_compose_dst_link(char * buffer)
45 sprintf(buffer, "/proc/%lld", (long long)getpid());
48 bool alsapid_get_pid(int alsa_client_id, pid_t * pid_ptr)
50 char src[PATH_MAX];
51 char dst[PATH_MAX + 1];
52 ssize_t ret;
53 pid_t pid;
55 alsapid_compose_src_link(alsa_client_id, src);
57 ret = readlink(src, dst, PATH_MAX);
58 if (ret == -1)
60 return false;
63 dst[ret] = 0;
65 pid = (pid_t)atoll(basename(dst));
66 if (pid <= 1)
68 return false;
71 *pid_ptr = pid;
72 return true;