1 /* Copyright (C) 2009 Edward Der-Hua Liu, Hsin-Chu, Taiwan
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation version 2.1
6  * of the License.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17 
18 #include <string.h>
19 
20 #include "hime.h"
21 
22 #include "im-srv.h"
23 
get_hime_addr_atom(Display * display)24 Atom get_hime_addr_atom (Display *display) {
25     return get_atom_by_name (display, "HIME_ADDR_ATOM_%s");
26 }
27 
get_hime_sockpath_atom(Display * display)28 Atom get_hime_sockpath_atom (Display *display) {
29     return get_atom_by_name (display, "HIME_SOCKPATH_ATOM_%s");
30 }
31 
32 // socket name: /tmp/.hime-$USER/socket-:0.0-hime
get_hime_im_srv_sock_path(char * outstr,const int outstrN)33 void get_hime_im_srv_sock_path (char *outstr, const int outstrN) {
34     const char *display = getenv ("DISPLAY");
35     const int uid = getuid ();
36 
37     if (!display || (strcmp (display, ":0") == 0)) {
38         display = ":0.0";
39     }
40 
41     const int DISPLAY_NAME_SIZE = 64;
42     char tdisplay[DISPLAY_NAME_SIZE];
43     strncpy (tdisplay, display, sizeof (tdisplay));
44 
45     if (!strchr (display, ':')) {
46         strcat (tdisplay, ":0");
47     }
48     if (!strchr (display, '.')) {
49         strcat (tdisplay, ".0");
50     }
51 
52     const int DIR_NAME_SIZE = 128;
53     char my_dir[DIR_NAME_SIZE];
54 
55     struct passwd *pw = getpwuid (uid);
56     const gchar *tmpdir = g_get_tmp_dir ();
57     snprintf (my_dir, sizeof (my_dir), "%s/.hime-%s", tmpdir, pw->pw_name);
58     struct stat st;
59 
60     // my_dir doesn't exist, create one
61     if (stat (my_dir, &st) == -1) {
62         mkdir (my_dir, 0700);
63     } else {
64         if (st.st_uid != uid) {
65             fprintf (stderr, "please check the permission of dir %s\n", my_dir);
66             return;
67         }
68     }
69 
70     snprintf (outstr, outstrN,
71               "%s/socket-%s-%s",
72               my_dir, tdisplay, get_hime_xim_name ());
73 }
74