1 | /* $Id: vboxmslnk.c 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Guest Additions Mouse Driver for Solaris: user space loader tool.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <VBox/version.h>
|
---|
38 | #include <iprt/buildconfig.h>
|
---|
39 |
|
---|
40 | #include <errno.h>
|
---|
41 | #include <fcntl.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <string.h>
|
---|
45 | #include <stropts.h>
|
---|
46 | #include <unistd.h>
|
---|
47 | #include <libgen.h>
|
---|
48 | #include <getopt.h>
|
---|
49 |
|
---|
50 | #define VBOXMSLNK_MUXID_FILE "/system/volatile/vboxmslnk.muxid"
|
---|
51 |
|
---|
52 | static const char *g_pszProgName;
|
---|
53 |
|
---|
54 |
|
---|
55 | static void vboxmslnk_fatal(const char *fmt, ...)
|
---|
56 | {
|
---|
57 | va_list ap;
|
---|
58 |
|
---|
59 | va_start(ap, fmt);
|
---|
60 | (void) vfprintf(stderr, fmt, ap);
|
---|
61 | if (fmt[strlen(fmt) - 1] != '\n')
|
---|
62 | (void) fprintf(stderr, " The error reported was: %s\n", strerror(errno));
|
---|
63 | va_end(ap);
|
---|
64 |
|
---|
65 | exit(EXIT_FAILURE);
|
---|
66 | }
|
---|
67 |
|
---|
68 | static void vboxmslnk_start(bool fNoLogo)
|
---|
69 | {
|
---|
70 | /* Open our pointer integration driver (vboxms). */
|
---|
71 | int hVBoxMS = open("/dev/vboxms", O_RDWR);
|
---|
72 | if (hVBoxMS < 0)
|
---|
73 | vboxmslnk_fatal("Failed to open /dev/vboxms - please make sure that the node exists and that\n"
|
---|
74 | "you have permission to open it.");
|
---|
75 |
|
---|
76 | /* Open the Solaris virtual mouse driver (consms). */
|
---|
77 | int hConsMS = open("/dev/mouse", O_RDWR);
|
---|
78 | if (hConsMS < 0)
|
---|
79 | vboxmslnk_fatal("Failed to open /dev/mouse - please make sure that the node exists and that\n"
|
---|
80 | "you have permission to open it.");
|
---|
81 |
|
---|
82 | /* Link vboxms to consms from below. What this means is that vboxms is
|
---|
83 | * added to the list of input sources multiplexed by consms, and vboxms
|
---|
84 | * will receive any control messages (such as information about guest
|
---|
85 | * resolution changes) sent to consms. The link can only be broken
|
---|
86 | * explicitly using the connection ID returned from the IOCtl. */
|
---|
87 | int idConnection = ioctl(hConsMS, I_PLINK, hVBoxMS);
|
---|
88 | if (idConnection < 0)
|
---|
89 | vboxmslnk_fatal("Failed to add /dev/vboxms (the pointer integration driver) to /dev/mouse\n"
|
---|
90 | "(the Solaris virtual master mouse).");
|
---|
91 |
|
---|
92 | (void) close(hVBoxMS);
|
---|
93 | (void) close(hConsMS);
|
---|
94 |
|
---|
95 | if (!fNoLogo)
|
---|
96 | (void) printf("Successfully enabled pointer integration. Connection ID number to the\n"
|
---|
97 | "Solaris virtual master mouse is:\n");
|
---|
98 | (void) printf("%d\n", idConnection);
|
---|
99 |
|
---|
100 | /* Save the connection ID (aka mux ID) so it can be retrieved later. */
|
---|
101 | FILE *fp = fopen(VBOXMSLNK_MUXID_FILE, "w");
|
---|
102 | if (fp == NULL)
|
---|
103 | vboxmslnk_fatal("Failed to open %s for writing the connection ID.", VBOXMSLNK_MUXID_FILE);
|
---|
104 | int rc = fprintf(fp, "%d\n", idConnection);
|
---|
105 | if (rc <= 0)
|
---|
106 | vboxmslnk_fatal("Failed to write the connection ID to %s.", VBOXMSLNK_MUXID_FILE);
|
---|
107 | (void) fclose(fp);
|
---|
108 | }
|
---|
109 |
|
---|
110 | static void vboxmslnk_stop()
|
---|
111 | {
|
---|
112 | /* Open the Solaris virtual mouse driver (consms). */
|
---|
113 | int hConsMS = open("/dev/mouse", O_RDWR);
|
---|
114 | if (hConsMS < 0)
|
---|
115 | vboxmslnk_fatal("Failed to open /dev/mouse - please make sure that the node exists and that\n"
|
---|
116 | "you have permission to open it.");
|
---|
117 |
|
---|
118 | /* Open the vboxmslnk.muxid file and retrieve the saved mux ID. */
|
---|
119 | FILE *fp = fopen(VBOXMSLNK_MUXID_FILE, "r");
|
---|
120 | if (fp == NULL)
|
---|
121 | vboxmslnk_fatal("Failed to open %s for reading the connection ID.", VBOXMSLNK_MUXID_FILE);
|
---|
122 | int idConnection;
|
---|
123 | int rc = fscanf(fp, "%d\n", &idConnection);
|
---|
124 | if (rc <= 0)
|
---|
125 | vboxmslnk_fatal("Failed to read the connection ID from %s.", VBOXMSLNK_MUXID_FILE);
|
---|
126 | (void) fclose(fp);
|
---|
127 | (void) unlink(VBOXMSLNK_MUXID_FILE);
|
---|
128 |
|
---|
129 | /* Unlink vboxms from consms so that vboxms is able to be unloaded. */
|
---|
130 | rc = ioctl(hConsMS, I_PUNLINK, idConnection);
|
---|
131 | if (rc < 0)
|
---|
132 | vboxmslnk_fatal("Failed to disconnect /dev/vboxms (the pointer integration driver) from\n"
|
---|
133 | "/dev/mouse (the Solaris virtual master mouse).");
|
---|
134 | (void) close(hConsMS);
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void vboxmslnk_usage()
|
---|
138 | {
|
---|
139 | (void) printf("Usage:\n"
|
---|
140 | " %s [--nologo] <--start | --stop>\n"
|
---|
141 | " %s [-V|--version]\n\n"
|
---|
142 | " -V|--version print the tool version.\n"
|
---|
143 | " --nologo do not display the logo text and only output the connection\n"
|
---|
144 | " ID number needed to disable pointer integration\n"
|
---|
145 | " again.\n"
|
---|
146 | " --start Connect the VirtualBox pointer integration kernel module\n"
|
---|
147 | " to the Solaris mouse driver kernel module.\n"
|
---|
148 | " --stop Disconnect the VirtualBox pointer integration kernel module\n"
|
---|
149 | " from the Solaris mouse driver kernel module.\n"
|
---|
150 | " -h|--help display this help text.\n",
|
---|
151 | g_pszProgName, g_pszProgName);
|
---|
152 | exit(EXIT_FAILURE);
|
---|
153 | }
|
---|
154 |
|
---|
155 | int main(int argc, char *argv[])
|
---|
156 | {
|
---|
157 | bool fShowVersion = false, fNoLogo = false, fStart = false, fStop = false;
|
---|
158 | int c;
|
---|
159 |
|
---|
160 | g_pszProgName = basename(argv[0]);
|
---|
161 |
|
---|
162 | static const struct option vboxmslnk_lopts[] = {
|
---|
163 | {"version", no_argument, 0, 'V' },
|
---|
164 | {"nologo", no_argument, 0, 'n' },
|
---|
165 | {"start", no_argument, 0, 's' },
|
---|
166 | {"stop", no_argument, 0, 't' },
|
---|
167 | {"help", no_argument, 0, 'h' },
|
---|
168 | { 0, 0, 0, 0}
|
---|
169 | };
|
---|
170 |
|
---|
171 | while ((c = getopt_long(argc, argv, "Vh", vboxmslnk_lopts, NULL)) != -1)
|
---|
172 | {
|
---|
173 | switch (c)
|
---|
174 | {
|
---|
175 | case 'V':
|
---|
176 | fShowVersion = true;
|
---|
177 | break;
|
---|
178 | case 'n':
|
---|
179 | fNoLogo = true;
|
---|
180 | break;
|
---|
181 | case 's':
|
---|
182 | fStart = true;
|
---|
183 | break;
|
---|
184 | case 't':
|
---|
185 | fStop = true;
|
---|
186 | break;
|
---|
187 | case 'h':
|
---|
188 | default:
|
---|
189 | vboxmslnk_usage();
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | if ( (!fStart && !fStop && !fShowVersion)
|
---|
194 | || (fStart && fStop)
|
---|
195 | || (fShowVersion && (fNoLogo || fStart || fStop)))
|
---|
196 | vboxmslnk_usage();
|
---|
197 |
|
---|
198 | if (fShowVersion)
|
---|
199 | {
|
---|
200 | (void) printf("%sr%u\n", VBOX_VERSION_STRING, RTBldCfgRevision());
|
---|
201 | exit(EXIT_SUCCESS);
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (!fNoLogo)
|
---|
205 | (void) printf(VBOX_PRODUCT
|
---|
206 | " Guest Additions utility for enabling Solaris pointer\nintegration Version "
|
---|
207 | VBOX_VERSION_STRING "\n"
|
---|
208 | "Copyright (C) " VBOX_C_YEAR " " VBOX_VENDOR "\n\n");
|
---|
209 |
|
---|
210 | if (fStart)
|
---|
211 | vboxmslnk_start(fNoLogo);
|
---|
212 |
|
---|
213 | if (fStop)
|
---|
214 | vboxmslnk_stop();
|
---|
215 |
|
---|
216 | exit(EXIT_SUCCESS);
|
---|
217 | }
|
---|