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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/* shmtest.c
*
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#ifdef HAVE_SYS_IPC_H
#include <sys/ipc.h>
#endif
#ifdef HAVE_SYS_SHM_H
#include <sys/shm.h>
#endif
#include "util.h"
#include "ttyio.h"
#include "i18n.h"
#ifdef HAVE_DOSISH_SYSTEM
int main( int argc, char **argv )
{
fprintf(stderr, "Sorry, not yet available for DOSish systems\n");
exit(1);
}
#else
static int serverpid = -1;
static void
my_usage(void)
{
fprintf(stderr, "usage: shmtest gpg-command-line\n");
exit(1);
}
const char *
strusage( int level )
{
return default_strusage(level);
}
static void
i18n_init(void)
{
#ifdef ENABLE_NLS
#ifdef HAVE_LC_MESSAGES
setlocale( LC_MESSAGES, "" );
#else
setlocale( LC_ALL, "" );
#endif
bindtextdomain( PACKAGE, G10_LOCALEDIR );
textdomain( PACKAGE );
#endif
}
static void
do_get_string( int mode, const char *keyword, byte *area, size_t areasize )
{
size_t n, len;
char *p=NULL;
int yes=0;
n = area[0] << 8 | area[1];
/* fixme: do some sanity checks here */
if( mode == 1 )
p = tty_get( keyword );
else if( mode == 3 )
p = tty_get_hidden( keyword );
else
yes = tty_get_answer_is_yes( keyword );
if( p ) {
len = strlen(p);
memcpy( area+n+2, p, len );
area[n] = len >> 8;
area[n+1] = len;
m_free(p);
}
else { /* bool */
area[n] = 0;
area[n+1] = 1;
area[n+2] = yes;
}
area[3] = 1; /* we should better use a semaphore */
kill( serverpid, SIGUSR1 );
}
int
main(int argc, char **argv)
{
void *area = NULL;
size_t areasize = 4096;
int shm_id = -1;
FILE *fp;
char buf[200];
char *p, *p2;
size_t n;
int i;
log_set_name("shmtest");
i18n_init();
#ifndef USE_SHM_COPROCESSING
log_info("SHM_COPRPOCESSING is not available\n");
#else
if( argc < 1 )
my_usage();
for(n=0,i=1; i < argc; i++ )
n += strlen(argv[i]) + 1;
p = m_alloc( 100 + n );
strcpy( p, "../g10/gpg --status-fd 1 --run-as-shm-coprocess 0");
for(i=1; i < argc; i++ ) {
strcat(p, " " );
strcat(p, argv[i] );
}
fp = popen( p, "r" );
m_free( p );
if( !fp )
log_error("popen failed: %s\n", strerror(errno));
while ( fgets (buf, sizeof (buf) - 1, fp ) != NULL ) {
size_t len = strlen(buf);
if( len >= 9 && !memcmp( buf, "[GNUPG:] ", 9 ) ) {
int word=0;
int is_info = 0, is_get = 0;
for( p = strtok(buf+9, " \n"); p ; p = strtok(NULL, " \n")) {
word++;
if( word==1 && !strcmp(p,"SHM_INFO") ) {
if( !area )
is_info=1;
else
log_error("duplicate SHM_INFO ignored\n" );
}
else if( is_info && (p2 = strchr(p, '=' )) ) {
int val;
*p2++ = 0;
val = atoi(p2); /* should be atou() for some values */
if( !strcmp(p, "pv" ) ) {
if( atoi(p2) != 1 )
log_fatal("invalid protocol version %d\n", val );
is_info = 2;
}
else if( !strcmp(p, "pid" ) )
serverpid = val;
else if( !strcmp(p, "shmid" ) )
shm_id = val;
}
else if( word == 1 && !strcmp(p,"SHM_GET") )
is_get = 1;
else if( word == 1 && !strcmp(p,"SHM_GET_BOOL") )
is_get = 2;
else if( word == 1 && !strcmp(p,"SHM_GET_HIDDEN") )
is_get = 3;
else if( word == 2 && is_get ) {
do_get_string( is_get, p, area, areasize );
break;
}
else if( word == 1 )
log_info("Status: %s\n", p);
}
if( is_info ) {
if( is_info < 2 )
log_fatal("SHM info without protocol version\n");
if( serverpid == -1 )
log_fatal("SHM info without server's pid\n");
if( shm_id == -1 )
log_fatal("SHM info without id\n");
log_info("Shared memory info: server=%d shm_id=%d\n",
serverpid, shm_id);
area = shmat( shm_id, 0, 0 );
if( area == (void*)-1 )
log_fatal("attach to shared memory failed: %s\n",
strerror(errno));
}
}
else
fputs (buf, stdout);
}
if( pclose(fp) )
log_error("pclose failed\n");
return 0;
#endif
}
#endif
|