1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It is one of the source files comprising zwgc, the Zephyr WindowGram
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_zephyr_c[] = "$Id$";
20 #include <zephyr/mit-copyright.h>
22 /****************************************************************************/
24 /* Module containing code dealing with zephyr: */
26 /****************************************************************************/
28 #include <zephyr/zephyr.h>
29 #include <sys/socket.h>
30 #include "new_string.h"
34 #include "subscriptions.h"
35 #include "variables.h"
38 #ifndef X_DISPLAY_MISSING
43 extern int zwgc_debug;
46 static int zephyr_inited = 0;
47 static unsigned short zephyr_port = 0;
52 * string get_zwgc_port_number_filename()
53 * Effects: Returns the filename that the zwgc port # is/should be
54 * stored in, based on the user's uid & the environment
55 * variable WGFILE. The returned string points into a
56 * static buffer that may change on further calls to this
57 * routine or getenv. The returned string should not be
58 * modified in any way.
61 static string get_zwgc_port_number_filename()
63 static char buffer[40];
67 if (temp = getenv("WGFILE"))
70 sprintf(buffer, "/tmp/wg.%d", getuid());
76 * Write out the port number to the wg file.
81 char *name = get_zwgc_port_number_filename();
84 port_file = fopen(name, "w");
86 fprintf(port_file, "%d\n", zephyr_port);
89 fprintf(stderr, "zwgc: error while opening %s for writing: ", name);
98 static void handle_zephyr_input(notice_handler)
99 void (*notice_handler)();
102 struct sockaddr_in from;
103 int complete_packets_ready;
107 if ( (complete_packets_ready=ZPending()) < 0 )
108 FATAL_TRAP( errno, "while calling ZPending()" );
110 if (complete_packets_ready==0)
113 notice = malloc(sizeof(ZNotice_t));
115 TRAP( ZReceiveNotice(notice, &from), "while getting zephyr notice" );
117 notice->z_auth = ZCheckAuthentication(notice, &from);
118 notice_handler(notice);
127 void zephyr_init(notice_handler)
128 void (*notice_handler)();
136 * Initialize zephyr. If error, print error message & exit.
138 FATAL_TRAP( ZInitialize(), "while initializing Zephyr" );
139 FATAL_TRAP( ZOpenPort(&zephyr_port), "while opening Zephyr port" );
142 * Save away our port number in a special place so that zctl and
143 * other clients can send us control messages: <<<>>>
145 temp = get_zwgc_port_number_filename();
146 port_file = fopen(temp, "r");
148 fprintf(stderr, "zwgc: windowgram file already exists. If you are\n");
149 fprintf(stderr, "zwgc: not already running zwgc, delete %s\n", temp);
150 fprintf(stderr, "zwgc: and try again.\n");
155 /* Set hostname and tty for locations. If we support X, use the
156 * display string for the default tty name. */
157 if (location_override)
158 tty = location_override;
159 #ifndef X_DISPLAY_MISSING
161 tty = DisplayString(dpy);
163 error_code = ZInitLocationInfo(NULL, tty);
164 TRAP( error_code, "while initializing location information" );
167 * Retrieve the user's desired exposure level (from the zephyr variable
168 * "exposure"), convert it to the proper internal form then
169 * set the user's location using it. If the exposure level is
170 * not one of the allowed ones, print an error and treat it as
173 if (temp = ZGetVariable("exposure")) {
174 if (!(exposure = ZParseExposureLevel(temp))) {
175 ERROR2("invalid exposure level %s, using exposure level none instead.\n", temp);
176 exposure = EXPOSE_NONE;
179 exposure = EXPOSE_OPSTAFF;
180 error_code = ZSetLocation(exposure); /* <<<>>> */
181 if (error_code != ZERR_LOGINFAIL)
182 TRAP( error_code, "while setting location" );
185 * If the exposure level isn't EXPOSE_NONE, turn on recieving notices.
186 * (this involves reading in the subscription file, etc.)
188 if (string_Neq(exposure, EXPOSE_NONE))
192 * Set $realm to our realm and $user to our zephyr username:
194 var_set_variable("realm", ZGetRealm());
195 var_set_variable("user", ZGetSender());
200 mux_add_input_source(ZGetFD(), (void (*)())handle_zephyr_input,
210 void finalize_zephyr() /* <<<>>> */
216 * Remove the file containing our port # since it is no longer needed:
219 temp = get_zwgc_port_number_filename();
222 fprintf(stderr, "zwgc: error while trying to delete %s: ", temp);
227 * Cancel our subscriptions, unset our location, and close our zephyr
232 TRAP( ZUnsetLocation(), "while unsetting location" );
233 TRAP( ZCancelSubscriptions(0), "while canceling subscriptions" );
236 (void) ZUnsetLocation();
237 (void) ZCancelSubscriptions(0);