~ubuntu-branches/ubuntu/saucy/uwsgi/saucy

« back to all changes in this revision

Viewing changes to utils.c

  • Committer: Package Import Robot
  • Author(s): Janos Guljas
  • Date: 2012-04-30 17:35:22 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20120430173522-qucwu1au3s9bflhb
Tags: 1.2+dfsg-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
187
187
        }
188
188
 
189
189
        /* stdin */
190
 
        if (dup2(fdin, 0) < 0) {
191
 
                uwsgi_error("dup2()");
192
 
                exit(1);
 
190
        if (fdin != 0) {
 
191
                if (dup2(fdin, 0) < 0) {
 
192
                        uwsgi_error("dup2()");
 
193
                        exit(1);
 
194
                }
 
195
                close(fdin);
193
196
        }
194
197
 
195
198
 
619
622
                                char *uidname = uwsgi.uidname;
620
623
                                if (!uidname) {
621
624
                                        struct passwd *pw = getpwuid(uwsgi.uid);
622
 
                                        uidname = pw->pw_name;
 
625
                                        if (pw)
 
626
                                                uidname = pw->pw_name;
 
627
                                        
623
628
                                }
624
629
                                if (!uidname) uidname = uwsgi_num2str(uwsgi.uid);
625
630
                                if (initgroups(uidname, uwsgi.gid)) {
1013
1018
 
1014
1019
        int ret;
1015
1020
        int interesting_fd;
1016
 
        char uwsgi_signal;
1017
1021
        struct uwsgi_socket *uwsgi_sock = uwsgi.sockets;
1018
1022
 
1019
1023
        thunder_lock;
1032
1036
 
1033
1037
                thunder_unlock;
1034
1038
 
1035
 
                if (read(interesting_fd, &uwsgi_signal, 1) <= 0) {
1036
 
                        if (uwsgi.no_orphans) {
1037
 
                                uwsgi_log_verbose("uWSGI worker %d screams: UAAAAAAH my master died, i will follow him...\n", uwsgi.mywid);
1038
 
                                end_me(0);
1039
 
                        }
1040
 
                        else {
1041
 
                                close(interesting_fd);
1042
 
                        }
1043
 
                }
1044
 
                else {
1045
 
#ifdef UWSGI_DEBUG
1046
 
                        uwsgi_log_verbose("master sent signal %d to worker %d\n", uwsgi_signal, uwsgi.mywid);
1047
 
#endif
1048
 
                        if (uwsgi_signal_handler(uwsgi_signal)) {
1049
 
                                uwsgi_log_verbose("error managing signal %d on worker %d\n", uwsgi_signal, uwsgi.mywid);
1050
 
                        }
1051
 
                }
 
1039
                uwsgi_receive_signal(interesting_fd, "worker", uwsgi.mywid);
1052
1040
 
1053
1041
#ifdef UWSGI_THREADING
1054
1042
                if (uwsgi.threads > 1) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &ret);
1125
1113
                uwsgi.vacuum = 1;
1126
1114
        }
1127
1115
#endif
 
1116
 
 
1117
        if (uwsgi.write_errors_exception_only) {
 
1118
                uwsgi.ignore_sigpipe = 1;
 
1119
                uwsgi.ignore_write_errors = 1;
 
1120
        }
 
1121
 
 
1122
        if (uwsgi.cheaper_count > 0 && uwsgi.cheaper_count >= uwsgi.numproc) {
 
1123
                uwsgi_log("invalid cheaper value: must be lower than processes\n");
 
1124
                exit(1);
 
1125
        }
1128
1126
}
1129
1127
 
1130
1128
void env_to_arg(char *src, char *dst) {
1189
1187
 
1190
1188
        struct timeval tv;
1191
1189
        char sftime[64];
 
1190
        char ctime_storage[26];
1192
1191
        time_t now;
1193
1192
 
1194
1193
        if (uwsgi.logdate) {
1201
1200
                }
1202
1201
                else {
1203
1202
                        gettimeofday(&tv, NULL);
1204
 
 
1205
 
                        memcpy(logpkt, ctime((const time_t *) &tv.tv_sec), 24);
 
1203
#ifdef __sun__
 
1204
                        ctime_r((const time_t *) &tv.tv_sec, ctime_storage, 26);
 
1205
#else
 
1206
                        ctime_r((const time_t *) &tv.tv_sec, ctime_storage);
 
1207
#endif
 
1208
                        memcpy(logpkt, ctime_storage, 24);
1206
1209
                        memcpy(logpkt + 24, " - ", 3);
1207
1210
 
1208
1211
                        rlen = 24 + 3;
1238
1241
        struct timeval tv;
1239
1242
        char sftime[64];
1240
1243
        time_t now;
 
1244
        char ctime_storage[26];
1241
1245
 
1242
1246
                if (uwsgi.log_strftime) {
1243
1247
                        now = time(NULL);
1248
1252
                }
1249
1253
                else {
1250
1254
                        gettimeofday(&tv, NULL);
1251
 
 
1252
 
                        memcpy(logpkt, ctime((const time_t *) &tv.tv_sec), 24);
 
1255
#ifdef __sun__
 
1256
                        ctime_r((const time_t *) &tv.tv_sec, ctime_storage, 26);
 
1257
#else
 
1258
                        ctime_r((const time_t *) &tv.tv_sec, ctime_storage);
 
1259
#endif
 
1260
                        memcpy(logpkt, ctime_storage, 24);
1253
1261
                        memcpy(logpkt + 24, " - ", 3);
1254
1262
 
1255
1263
                        rlen = 24 + 3;
1905
1913
        return 0;
1906
1914
}
1907
1915
 
 
1916
int uwsgi_logic_opt_if_reload(char *key, char *value) {
 
1917
        if (uwsgi.is_a_reload) {
 
1918
                add_exported_option(key, value, 0);
 
1919
                return 1;
 
1920
        }
 
1921
        return 0;
 
1922
}
 
1923
 
 
1924
int uwsgi_logic_opt_if_not_reload(char *key, char *value) {
 
1925
        if (!uwsgi.is_a_reload) {
 
1926
                add_exported_option(key, value, 0);
 
1927
                return 1;
 
1928
        }
 
1929
        return 0;
 
1930
}
 
1931
 
1908
1932
int uwsgi_logic_opt_if_file(char *key, char *value) {
1909
1933
 
1910
1934
        if (uwsgi_is_file(uwsgi.logic_opt_data)) {
2692
2716
                                uwsgi_error("dup2()");
2693
2717
                                exit(1);
2694
2718
                        }
 
2719
                        close(devnull);
2695
2720
                }
2696
2721
 
2697
2722
                if (setsid() < 0) {
3169
3194
                uwsgi_string->len = strlen(value);
3170
3195
        }
3171
3196
        uwsgi_string->next = NULL;
 
3197
        uwsgi_string->custom = 0;
3172
3198
 
3173
3199
        return uwsgi_string;
3174
3200
}
3899
3925
                strncat(uwsgi.orig_argv[0], uwsgi.procname_append, uwsgi.max_procname-(amount+1));
3900
3926
        }
3901
3927
 
3902
 
        memset(uwsgi.orig_argv[0]+amount+1, ' ', uwsgi.max_procname-(amount-1));
 
3928
        // fill with spaces...
 
3929
        memset(uwsgi.orig_argv[0]+amount+1, ' ', uwsgi.max_procname-(amount));
 
3930
        // end with \0
 
3931
        memset(uwsgi.orig_argv[0]+amount+1+(uwsgi.max_procname-(amount)), '\0', 1);
 
3932
 
3903
3933
#elif defined(__FreeBSD__)
3904
3934
        if (uwsgi.procname_prefix) {
3905
3935
                if (!uwsgi.procname_append) {
4067
4097
                else {
4068
4098
                        if (!touch->custom) touch->custom = (uint64_t) tr_st.st_mtime;
4069
4099
                        if ((uint64_t) tr_st.st_mtime > touch->custom) {
 
4100
#ifdef UWSGI_DEBUG
 
4101
                                uwsgi_log("[uwsgi-check-touches] modification detected on %s: %llu -> %llu\n", touch->value, (unsigned long long) touch->custom,
 
4102
                                        (unsigned long long) tr_st.st_mtime);
 
4103
#endif
4070
4104
                                touch->custom = (uint64_t) tr_st.st_mtime;
4071
4105
                                return touch->value;
4072
4106
                        }
4149
4183
        return time(NULL);
4150
4184
}
4151
4185
 
 
4186
uint64_t uwsgi_micros() {
 
4187
        struct timeval tv;
 
4188
        gettimeofday(&tv, NULL);
 
4189
        return (tv.tv_sec * 1000000) + tv.tv_usec;
 
4190
}
 
4191
 
4152
4192
void uwsgi_write_pidfile(char *pidfile_name) {
4153
4193
        uwsgi_log("writing pidfile to %s\n", pidfile_name);
4154
4194
        FILE *pidfile = fopen(pidfile_name, "w");
4238
4278
        return ret;
4239
4279
}
4240
4280
 
4241
 
char *uwsgi_expand_path(char *dir, int dir_len, char *ptr) { 
4242
 
        char src[PATH_MAX+1]; 
4243
 
        memcpy(src, dir, dir_len); 
4244
 
        src[dir_len] = 0; 
4245
 
        char *dst = ptr; 
4246
 
        if (!dst) dst = uwsgi_malloc(PATH_MAX+1); 
4247
 
        if (!realpath(src, dst)) { 
4248
 
                uwsgi_error_realpath(src); 
4249
 
                if (!ptr) 
4250
 
                        free(dst); 
4251
 
                return NULL; 
4252
 
        } 
4253
 
        return dst; 
4254
 
 
4281
char *uwsgi_expand_path(char *dir, int dir_len, char *ptr) {
 
4282
        char src[PATH_MAX+1];
 
4283
        memcpy(src, dir, dir_len);
 
4284
        src[dir_len] = 0;
 
4285
        char *dst = ptr;
 
4286
        if (!dst) dst = uwsgi_malloc(PATH_MAX+1);
 
4287
        if (!realpath(src, dst)) {
 
4288
                uwsgi_error_realpath(src);
 
4289
                if (!ptr)
 
4290
                        free(dst);
 
4291
                return NULL;
 
4292
        }
 
4293
        return dst;
 
4294
}