int daemonize (glusterfs_ctx_t *ctx) { int ret = 0; cmd_args_t *cmd_args = NULL; cmd_args = &ctx->cmd_args; ret = glusterfs_pidfile_setup (ctx); if (ret) return ret; if (cmd_args->no_daemon_mode) goto postfork; if (cmd_args->debug_mode) goto postfork; ret = os_daemon (0, 0); if (ret == -1) { gf_log ("daemonize", GF_LOG_ERROR, "Daemonization failed: %s", strerror(errno)); return ret; } postfork: ret = glusterfs_pidfile_update (ctx); if (ret) return ret; glusterfs_signals_setup (ctx); return ret; }
int os_daemonize(const char *pid_file) { #if defined(__uClinux__) || defined(__sun__) return -1; #else /* defined(__uClinux__) || defined(__sun__) */ if (os_daemon(0, 0)) { perror("daemon"); return -1; } if (pid_file) { FILE *f = fopen(pid_file, "w"); if (f) { fprintf(f, "%u\n", getpid()); fclose(f); } } return -0; #endif /* defined(__uClinux__) || defined(__sun__) */ }
int main(int argc, char *argv[]) { int option, rc; struct sockaddr_in localAddr, servAddr; struct hostent *h; int n; for(n=0;n<CLIMB;n++) climb[n]=0.0; switch (gpsd_units()) { case imperial: altfactor = METERS_TO_FEET; altunits = "ft"; speedfactor = MPS_TO_MPH; speedunits = "mph"; break; case nautical: altfactor = METERS_TO_FEET; altunits = "ft"; speedfactor = MPS_TO_KNOTS; speedunits = "knots"; break; case metric: altfactor = 1; altunits = "m"; speedfactor = MPS_TO_KPH; speedunits = "kph"; break; default: /* leave the default alone */ break; } /* Process the options. Print help if requested. */ while ((option = getopt(argc, argv, "Vhl:su:")) != -1) { switch (option) { case 'V': (void)fprintf(stderr, "lcdgs revision " REVISION "\n"); exit(EXIT_SUCCESS); case 'h': default: usage(argv[0]); break; case 'l': switch ( optarg[0] ) { case 'd': deg_type = deg_dd; continue; case 'm': deg_type = deg_ddmm; continue; case 's': deg_type = deg_ddmmss; continue; default: (void)fprintf(stderr, "Unknown -l argument: %s\n", optarg); } break; case 's': sleep(10); continue; case 'u': switch ( optarg[0] ) { case 'i': altfactor = METERS_TO_FEET; altunits = "ft"; speedfactor = MPS_TO_MPH; speedunits = "mph"; continue; case 'n': altfactor = METERS_TO_FEET; altunits = "ft"; speedfactor = MPS_TO_KNOTS; speedunits = "knots"; continue; case 'm': altfactor = 1; altunits = "m"; speedfactor = MPS_TO_KPH; speedunits = "kph"; continue; default: (void)fprintf(stderr, "Unknown -u argument: %s\n", optarg); } } } /* Grok the server, port, and device. */ if (optind < argc) { gpsd_source_spec(argv[optind], &source); } else gpsd_source_spec(NULL, &source); /* Daemonize... */ if (os_daemon(0, 0) != 0) (void)fprintf(stderr, "lcdgps: daemonization failed: %s\n", strerror(errno)); /* Open the stream to gpsd. */ if (gps_open(source.server, source.port, &gpsdata) != 0) { (void)fprintf( stderr, "lcdgps: no gpsd running or network error: %d, %s\n", errno, gps_errstr(errno)); exit(EXIT_FAILURE); } /* Connect to LCDd */ h = gethostbyname(LCDDHOST); if (h==NULL) { printf("%s: unknown host '%s'\n",argv[0],LCDDHOST); exit(EXIT_FAILURE); } servAddr.sin_family = h->h_addrtype; memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); servAddr.sin_port = htons(LCDDPORT); /* create socket */ sd = socket(AF_INET, SOCK_STREAM, 0); if (BAD_SOCKET(sd)) { perror("cannot open socket "); exit(EXIT_FAILURE); } /* bind any port number */ localAddr.sin_family = AF_INET; localAddr.sin_addr.s_addr = htonl(INADDR_ANY); localAddr.sin_port = htons(0); /* coverity[uninit_use_in_call] */ rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr)); if (rc == -1) { printf("%s: cannot bind port TCP %d\n",argv[0],LCDDPORT); perror("error "); exit(EXIT_FAILURE); } /* connect to server */ /* coverity[uninit_use_in_call] */ rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr)); if (rc == -1) { perror("cannot connect "); exit(EXIT_FAILURE); } /* Do the initial field label setup. */ reset_lcd(); /* Here's where updates go. */ unsigned int flags = WATCH_ENABLE; if (source.device != NULL) flags |= WATCH_DEVICE; (void)gps_stream(&gpsdata, flags, source.device); for (;;) { /* heart of the client */ if (!gps_waiting(&gpsdata, 50000000)) { (void)fprintf(stderr, "lcdgps: error while waiting\n"); exit(EXIT_FAILURE); } else { (void)gps_read(&gpsdata); update_lcd(&gpsdata); } } }