1 /*
2   Mutex implementation for SnapScan backend
3 
4   Copyright (C) 2000, 2004 Henrik Johansson, Oliver Schwartz
5 
6   This program is free software; you can redistribute it and/or
7   modify it under the terms of the GNU General Public License as
8   published by the Free Software Foundation; either version 2 of the
9   License, or (at your option) any later version.
10 
11   This program is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <https://www.gnu.org/licenses/>.
18 
19   As a special exception, the authors of SANE give permission for
20   additional uses of the libraries contained in this release of SANE.
21 
22   The exception is that, if you link a SANE library with other files
23   to produce an executable, this does not by itself cause the
24   resulting executable to be covered by the GNU General Public
25   License.  Your use of that executable is in no way restricted on
26   account of linking the SANE library code into it.
27 
28   This exception does not, however, invalidate any other reasons why
29   the executable file might be covered by the GNU General Public
30   License.
31 
32   If you submit changes to SANE to the maintainers to be included in
33   a subsequent release, you agree by submitting the changes that
34   those changes may be distributed with this exception intact.
35 
36   If you write modifications of your own for SANE, it is your choice
37   whether to permit this exception to apply to your modifications.
38   If you do not wish that, delete this exception notice.*/
39 
40 #if defined __BEOS__
41 
42 #include <OS.h>
43 #define snapscan_mutex_t sem_id
44 
snapscani_mutex_open(snapscan_mutex_t* a_sem, const char* dev __sane_unused__)45 static int snapscani_mutex_open(snapscan_mutex_t* a_sem, const char* dev __sane_unused__)
46 {
47     *a_sem = create_sem(1, "snapscan_mutex");
48     return 1;
49 }
50 
snapscani_mutex_close(snapscan_mutex_t* a_sem)51 static void snapscani_mutex_close(snapscan_mutex_t* a_sem)
52 {
53     delete_sem(*a_sem);
54 }
55 
snapscani_mutex_lock(snapscan_mutex_t* a_sem)56 static void snapscani_mutex_lock(snapscan_mutex_t* a_sem)
57 {
58     acquire_sem(*a_sem);
59 }
60 
snapscani_mutex_unlock(snapscan_mutex_t* a_sem)61 static void snapscani_mutex_unlock(snapscan_mutex_t* a_sem)
62 {
63     release_sem(*a_sem);
64 }
65 
66 
67 
68 #elif defined USE_PTHREAD || defined HAVE_OS2_H
69 
70 #include <pthread.h>
71 #define snapscan_mutex_t pthread_mutex_t
72 
snapscani_mutex_open(snapscan_mutex_t* sem_id, const char* dev __sane_unused__)73 static int snapscani_mutex_open(snapscan_mutex_t* sem_id, const char* dev __sane_unused__)
74 {
75     pthread_mutex_init(sem_id, NULL);
76     return 1;
77 }
78 
snapscani_mutex_close(snapscan_mutex_t* sem_id)79 static void snapscani_mutex_close(snapscan_mutex_t* sem_id)
80 {
81     pthread_mutex_destroy(sem_id);
82 }
83 
snapscani_mutex_lock(snapscan_mutex_t* sem_id)84 static void snapscani_mutex_lock(snapscan_mutex_t* sem_id)
85 {
86     pthread_mutex_lock(sem_id);
87 }
88 
snapscani_mutex_unlock(snapscan_mutex_t* sem_id)89 static void snapscani_mutex_unlock(snapscan_mutex_t* sem_id)
90 {
91     pthread_mutex_unlock(sem_id);
92 }
93 
94 #else /* defined USE_PTHREAD || defined HAVE_OS2_H */
95 
96 #include <sys/ipc.h>
97 #include <sys/sem.h>
98 #include <sys/types.h>
99 #include <unistd.h>
100 
101 #define snapscan_mutex_t int
102 
103 /* check for union semun */
104 #if defined(HAVE_UNION_SEMUN)
105 /* union semun is defined by including <sys/sem.h> */
106 #else
107 /* according to X/OPEN we have to define it ourselves */
108 union semun {
109    int val;                    /* value for SETVAL */
110    struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
111    unsigned short int *array;  /* array for GETALL, SETALL */
112    struct seminfo *__buf;      /* buffer for IPC_INFO */
113 };
114 #endif /* defined HAVE_UNION_SEMUN */
115 
116 static struct sembuf sem_wait = { 0, -1, 0 };
117 static struct sembuf sem_signal = { 0, 1, 0 };
118 
snapscani_bernstein(const unsigned char* str)119 static unsigned int snapscani_bernstein(const unsigned char* str)
120 {
121     unsigned int hash = 5381; /* some arbitrary number */
122     int c;
123 
124     while (*str)
125     {
126         c = *str++;
127         hash = ((hash << 5) + hash) + c;
128     }
129     return hash;
130 }
131 
snapscani_mutex_open(snapscan_mutex_t* sem_id, const char* dev)132 static int snapscani_mutex_open(snapscan_mutex_t* sem_id, const char* dev)
133 {
134     static const char *me = "snapscani_mutex_open";
135     key_t ipc_key = -1;
136 
137     if (strstr(dev, "libusb:") == dev)
138     {
139         key_t ipc_key = (key_t) snapscani_bernstein((const unsigned char*) dev+7);
140 	DBG (DL_INFO, "%s: using IPC key 0x%08x for device %s\n",
141 	     me, ipc_key, dev);
142     }
143     else
144     {
145       ipc_key = ftok(dev, 0x12);
146 
147 	if (ipc_key == -1)
148 	{
149 	  DBG (DL_MAJOR_ERROR, "%s: could not obtain IPC key for device %s: %s\n", me, dev, strerror(errno));
150 	    return 0;
151 	}
152     }
153 
154     *sem_id = semget( ipc_key, 1, IPC_CREAT | 0660 );
155     if (*sem_id == -1)
156     {
157         DBG (DL_MAJOR_ERROR, "%s: semget failed: %s\n", me, strerror(errno));
158 	return 0;
159     }
160 
161     semop(*sem_id, &sem_signal, 1);
162     return 1;
163 }
164 
snapscani_mutex_close(snapscan_mutex_t* sem_id)165 static void snapscani_mutex_close(snapscan_mutex_t* sem_id)
166 {
167     static union semun dummy_semun_arg;
168     semctl(*sem_id, 0, IPC_RMID, dummy_semun_arg);
169 }
170 
snapscani_mutex_lock(snapscan_mutex_t* sem_id)171 static void snapscani_mutex_lock(snapscan_mutex_t* sem_id)
172 {
173     semop(*sem_id, &sem_wait, 1);
174 }
175 
snapscani_mutex_unlock(snapscan_mutex_t* sem_id)176 static void snapscani_mutex_unlock(snapscan_mutex_t* sem_id)
177 {
178     semop(*sem_id, &sem_signal, 1);
179 }
180 
181 #endif /* defined USE_PTHREAD || defined HAVE_OS2_H */
182